-
Notifications
You must be signed in to change notification settings - Fork 1
/
timeout.go
73 lines (58 loc) · 1.53 KB
/
timeout.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package timeout
import (
"bytes"
"context"
"net/http"
"time"
)
// Version is this package's version.
var Version = "0.1.0"
// DefaultTimeoutHandler is a convenient timeout handler which
// simply returns "504 Service timeout".
var DefaultTimeoutHandler = http.HandlerFunc(
func(res http.ResponseWriter, req *http.Request) {
res.WriteHeader(http.StatusGatewayTimeout)
res.Write([]byte("Service timeout"))
})
type timeoutWriter struct {
rw http.ResponseWriter
status int
buf *bytes.Buffer
}
func (tw timeoutWriter) Header() http.Header {
return tw.rw.Header()
}
func (tw *timeoutWriter) WriteHeader(status int) {
tw.status = status
}
func (tw *timeoutWriter) Write(b []byte) (int, error) {
if tw.status == 0 {
tw.status = http.StatusOK
}
return tw.buf.Write(b)
}
// Handler wraps the http.Handler h with timeout support.
func Handler(h http.Handler, timeout time.Duration, timeoutHandler http.Handler) http.Handler {
return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
background := req.Context()
tCtx, tCancel := context.WithTimeout(background, timeout)
cCtx, cCancel := context.WithCancel(background)
req.WithContext(cCtx)
defer tCancel()
tw := &timeoutWriter{rw: res, buf: bytes.NewBuffer(nil)}
go func() {
h.ServeHTTP(tw, req)
cCancel()
}()
select {
case <-cCtx.Done():
res.WriteHeader(tw.status)
res.Write(tw.buf.Bytes())
case <-tCtx.Done():
if err := tCtx.Err(); err == context.DeadlineExceeded {
cCancel()
timeoutHandler.ServeHTTP(res, req)
}
}
})
}