| 1 | package provider |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "io" |
| 7 | "net/http" |
| 8 | "net/http/httptrace" |
| 9 | "sync" |
| 10 | "time" |
| 11 | ) |
| 12 | |
| 13 | const requestSendTimeout = 300 * time.Second |
| 14 | |
| 15 | // RequestSendTimeoutError is a local send-stage failure, not caller cancellation. |
| 16 | // It does not unwrap context.Canceled or implement net.Error, so no generic |
| 17 | // classifier retries it; SendWithRetry alone resends it once. |
| 18 | type RequestSendTimeoutError struct { |
| 19 | Budget time.Duration |
| 20 | } |
| 21 | |
| 22 | func (e *RequestSendTimeoutError) Error() string { |
| 23 | return fmt.Sprintf("request_send_timeout: request did not finish sending within %s", e.Budget) |
| 24 | } |
| 25 | |
| 26 | // requestSendGuard owns only the pre-response send lifetime. Successful writes |
| 27 | // hand timeout ownership to Transport's ResponseHeaderTimeout. Each RoundTrip |
| 28 | // (redirect) and subsequent GetConn (internal retry) can start a new send stage. |
| 29 | // No connection deadline is changed: an HTTP/2 sibling stream remains usable. |
| 30 | type requestSendGuard struct { |
| 31 | mu sync.Mutex |
| 32 | budget time.Duration |
| 33 | cancel context.CancelCauseFunc |
| 34 | timer *time.Timer |
| 35 | generation uint64 |
| 36 | round uint64 |
| 37 | started time.Time |
| 38 | elapsed time.Duration |
| 39 | closed bool |
| 40 | timedOut bool |
| 41 | } |
| 42 | |
| 43 | func (g *requestSendGuard) begin() uint64 { |
| 44 | g.mu.Lock() |
| 45 | defer g.mu.Unlock() |
| 46 | g.round++ |
| 47 | return g.round |
| 48 | } |
| 49 | |
| 50 | func (g *requestSendGuard) arm(round uint64) { |
| 51 | g.mu.Lock() |
| 52 | defer g.mu.Unlock() |
| 53 | if g.closed || g.timedOut || g.timer != nil || round != g.round { |
| 54 | return |
| 55 | } |
| 56 | g.generation++ |
| 57 | generation := g.generation |
| 58 | g.started = time.Now() |
| 59 | g.elapsed = 0 |
| 60 | g.timer = time.AfterFunc(g.budget, func() { g.expire(generation) }) |
| 61 | } |
| 62 | |
| 63 | func (g *requestSendGuard) expire(generation uint64) { |
| 64 | g.mu.Lock() |
| 65 | defer g.mu.Unlock() |
| 66 | if g.closed || g.timedOut || g.timer == nil || generation != g.generation { |
| 67 | return |
| 68 | } |
| 69 | g.elapsed = time.Since(g.started) |
| 70 | g.timedOut = true |
| 71 | g.cancel(&RequestSendTimeoutError{Budget: g.budget}) |
| 72 | } |
| 73 | |
| 74 | func (g *requestSendGuard) stopLocked() { |
| 75 | if g.timer != nil { |
| 76 | g.timer.Stop() |
| 77 | g.timer = nil |
| 78 | g.generation++ // A callback already queued by AfterFunc must be inert. |
| 79 | if !g.timedOut { |
| 80 | g.elapsed = time.Since(g.started) |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | func (g *requestSendGuard) written(round uint64, info httptrace.WroteRequestInfo) { |
| 86 | if info.Err != nil { |
| 87 | return |
| 88 | } |
| 89 | g.mu.Lock() |
| 90 | defer g.mu.Unlock() |
| 91 | if round != g.round { |
| 92 | return |
| 93 | } |
| 94 | g.stopLocked() |
| 95 | } |
| 96 | |
| 97 | func (g *requestSendGuard) finish() time.Duration { |
| 98 | g.mu.Lock() |
| 99 | defer g.mu.Unlock() |
| 100 | g.closed = true |
| 101 | g.stopLocked() |
| 102 | return g.elapsed |
| 103 | } |
| 104 | |
| 105 | type requestSendTransport struct { |
| 106 | base http.RoundTripper |
| 107 | guard *requestSendGuard |
| 108 | } |
| 109 | |
| 110 | func (t requestSendTransport) RoundTrip(req *http.Request) (*http.Response, error) { |
| 111 | g := t.guard |
| 112 | round := g.begin() |
| 113 | g.arm(round) |
| 114 | ctx := httptrace.WithClientTrace(req.Context(), &httptrace.ClientTrace{ |
| 115 | GetConn: func(string) { g.arm(round) }, |
| 116 | WroteRequest: func(info httptrace.WroteRequestInfo) { g.written(round, info) }, |
| 117 | }) |
| 118 | resp, err := t.base.RoundTrip(req.WithContext(ctx)) |
| 119 | // An early final response is also a handoff to response/body handling. |
| 120 | // Do not close the guard yet: Client may follow a redirect next. |
| 121 | g.mu.Lock() |
| 122 | g.stopLocked() |
| 123 | g.mu.Unlock() |
| 124 | return resp, err |
| 125 | } |
| 126 | |
| 127 | // releaseResponseBody unregisters the caller's cancellation once streaming has |
| 128 | // finished. It never cancels: on HTTP/2 that races the stream's teardown, and |
| 129 | // the next request can find the pooled connection not yet idle. |
| 130 | type releaseResponseBody struct { |
| 131 | io.ReadCloser |
| 132 | release func() bool |
| 133 | once sync.Once |
| 134 | } |
| 135 | |
| 136 | func (b *releaseResponseBody) done() { b.once.Do(func() { b.release() }) } |
| 137 | func (b *releaseResponseBody) Read(p []byte) (int, error) { |
| 138 | n, err := b.ReadCloser.Read(p) |
| 139 | if err != nil { |
| 140 | b.done() |
| 141 | } |
| 142 | return n, err |
| 143 | } |
| 144 | func (b *releaseResponseBody) Close() error { |
| 145 | err := b.ReadCloser.Close() |
| 146 | b.done() |
| 147 | return err |
| 148 | } |
| 149 |