-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimeout.go
More file actions
65 lines (55 loc) · 1.34 KB
/
Copy pathtimeout.go
File metadata and controls
65 lines (55 loc) · 1.34 KB
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
package rhttp
import (
"context"
"io"
"net/http"
"time"
)
// Timeout returns a middleware that applies a timeout to requests.
// If the request's context already has a shorter deadline, it is respected.
// A non-positive duration disables the middleware (it becomes a no-op).
func Timeout(d time.Duration) Middleware {
if d <= 0 {
return func(next http.RoundTripper) http.RoundTripper {
return next
}
}
return func(next http.RoundTripper) http.RoundTripper {
return timeoutRoundTripper{next: next, timeout: d}
}
}
type timeoutRoundTripper struct {
next http.RoundTripper
timeout time.Duration
}
func (t timeoutRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
ctx := req.Context()
// Only apply timeout if ctx doesn't have a shorter deadline
if deadline, ok := ctx.Deadline(); ok {
if time.Until(deadline) <= t.timeout {
return t.next.RoundTrip(req)
}
}
ctx, cancel := context.WithTimeout(ctx, t.timeout)
req = req.Clone(ctx)
resp, err := t.next.RoundTrip(req)
if err != nil {
cancel()
return resp, err
}
if resp.Body == nil {
cancel()
return resp, nil
}
resp.Body = &cancelBody{ReadCloser: resp.Body, cancel: cancel}
return resp, nil
}
type cancelBody struct {
io.ReadCloser
cancel context.CancelFunc
}
func (b *cancelBody) Close() error {
err := b.ReadCloser.Close()
b.cancel()
return err
}