2021-02-23 18:40:22 -07:00
|
|
|
// +build ignore
|
|
|
|
|
2021-02-19 22:49:21 -07:00
|
|
|
package gemini
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TimeoutHandler returns a Handler that runs h with the given time limit.
|
|
|
|
//
|
|
|
|
// The new Handler calls h.ServeGemini to handle each request, but
|
|
|
|
// if a call runs for longer than its time limit, the handler responds with a
|
|
|
|
// 40 Temporary Failure error. After such a timeout, writes by h to its
|
|
|
|
// ResponseWriter will return ErrHandlerTimeout.
|
|
|
|
func TimeoutHandler(h Handler, dt time.Duration) Handler {
|
|
|
|
return &timeoutHandler{
|
|
|
|
h: h,
|
|
|
|
dt: dt,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type timeoutHandler struct {
|
|
|
|
h Handler
|
|
|
|
dt time.Duration
|
|
|
|
}
|
|
|
|
|
2021-02-20 13:49:07 -07:00
|
|
|
func (t *timeoutHandler) ServeGemini(ctx context.Context, w ResponseWriter, r *Request) {
|
2021-02-20 13:52:54 -07:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, t.dt)
|
2021-02-20 11:37:08 -07:00
|
|
|
defer cancel()
|
2021-02-19 22:49:21 -07:00
|
|
|
|
|
|
|
done := make(chan struct{})
|
|
|
|
tw := &timeoutWriter{}
|
|
|
|
go func() {
|
2021-02-20 13:49:07 -07:00
|
|
|
t.h.ServeGemini(ctx, tw, r)
|
2021-02-19 22:49:21 -07:00
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
tw.mu.Lock()
|
|
|
|
defer tw.mu.Unlock()
|
|
|
|
if !tw.wroteHeader {
|
|
|
|
tw.status = StatusSuccess
|
|
|
|
}
|
|
|
|
w.WriteHeader(tw.status, tw.meta)
|
|
|
|
w.Write(tw.b.Bytes())
|
|
|
|
case <-ctx.Done():
|
|
|
|
tw.mu.Lock()
|
|
|
|
defer tw.mu.Unlock()
|
|
|
|
w.WriteHeader(StatusTemporaryFailure, "Timeout")
|
|
|
|
tw.timedOut = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type timeoutWriter struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
b bytes.Buffer
|
2021-02-20 14:15:26 -07:00
|
|
|
status Status
|
2021-02-19 22:49:21 -07:00
|
|
|
meta string
|
|
|
|
mediatype string
|
|
|
|
wroteHeader bool
|
|
|
|
timedOut bool
|
|
|
|
}
|
|
|
|
|
2021-02-21 16:52:04 -07:00
|
|
|
func (w *timeoutWriter) SetMediaType(mediatype string) {
|
2021-02-19 22:49:21 -07:00
|
|
|
w.mu.Lock()
|
|
|
|
defer w.mu.Unlock()
|
|
|
|
w.mediatype = mediatype
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *timeoutWriter) Write(b []byte) (int, error) {
|
|
|
|
w.mu.Lock()
|
|
|
|
defer w.mu.Unlock()
|
|
|
|
if w.timedOut {
|
|
|
|
return 0, ErrHandlerTimeout
|
|
|
|
}
|
|
|
|
if !w.wroteHeader {
|
|
|
|
w.writeHeaderLocked(StatusSuccess, w.mediatype)
|
|
|
|
}
|
|
|
|
return w.b.Write(b)
|
|
|
|
}
|
|
|
|
|
2021-02-20 14:15:26 -07:00
|
|
|
func (w *timeoutWriter) WriteHeader(status Status, meta string) {
|
2021-02-19 22:49:21 -07:00
|
|
|
w.mu.Lock()
|
|
|
|
defer w.mu.Unlock()
|
|
|
|
if w.timedOut {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.writeHeaderLocked(status, meta)
|
|
|
|
}
|
|
|
|
|
2021-02-20 14:15:26 -07:00
|
|
|
func (w *timeoutWriter) writeHeaderLocked(status Status, meta string) {
|
2021-02-19 22:49:21 -07:00
|
|
|
if w.wroteHeader {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.status = status
|
|
|
|
w.meta = meta
|
|
|
|
w.wroteHeader = true
|
|
|
|
}
|
2021-02-21 14:06:56 -07:00
|
|
|
|
|
|
|
func (w *timeoutWriter) Flush() error {
|
|
|
|
return nil
|
|
|
|
}
|
2021-02-23 15:32:23 -07:00
|
|
|
|
|
|
|
func (w *timeoutWriter) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|