-
Notifications
You must be signed in to change notification settings - Fork 10
/
hedged.go
342 lines (286 loc) · 8.26 KB
/
hedged.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package hedgedhttp
import (
"context"
"errors"
"fmt"
"net/http"
"sync"
"time"
)
const infiniteTimeout = 30 * 24 * time.Hour // domain specific infinite
// Client represents a hedged HTTP client.
type Client struct {
rt http.RoundTripper
stats *Stats
}
// Config for the [Client].
type Config struct {
// Transport of the [Client].
// Default is nil which results in [net/http.DefaultTransport].
Transport http.RoundTripper
// Upto says how much requests to make.
// Default is zero which means no hedged requests will be made.
Upto int
// Delay before 2 consecutive hedged requests.
Delay time.Duration
// Next returns the upto and delay for each HTTP that will be hedged.
// Default is nil which results in (Upto, Delay) result.
Next NextFn
_ struct{} // enforce explicit field names.
}
// NextFn represents a function that is called for each HTTP request for retrieving hedging options.
type NextFn func() (upto int, delay time.Duration)
// New returns a new Client for the given config.
func New(cfg Config) (*Client, error) {
switch {
case cfg.Delay < 0:
return nil, errors.New("hedgedhttp: timeout cannot be negative")
case cfg.Upto < 0:
return nil, errors.New("hedgedhttp: upto cannot be negative")
}
if cfg.Transport == nil {
cfg.Transport = http.DefaultTransport
}
rt, stats, err := NewRoundTripperAndStats(cfg.Delay, cfg.Upto, cfg.Transport)
if err != nil {
return nil, err
}
// TODO(cristaloleg): this should be removed after internals cleanup.
rt2, ok := rt.(*hedgedTransport)
if !ok {
panic(fmt.Sprintf("want *hedgedTransport got %T", rt))
}
rt2.next = cfg.Next
c := &Client{
rt: rt2,
stats: stats,
}
return c, nil
}
// Stats returns statistics for the given client, see [Stats] methods.
func (c *Client) Stats() *Stats {
return c.stats
}
// Do does the same as [RoundTrip], this method is presented to align with [net/http.Client].
func (c *Client) Do(req *http.Request) (*http.Response, error) {
return c.rt.RoundTrip(req)
}
// RoundTrip implements [net/http.RoundTripper] interface.
func (c *Client) RoundTrip(req *http.Request) (*http.Response, error) {
return c.rt.RoundTrip(req)
}
// NewClient returns a new http.Client which implements hedged requests pattern.
// Given Client starts a new request after a timeout from previous request.
// Starts no more than upto requests.
func NewClient(timeout time.Duration, upto int, client *http.Client) (*http.Client, error) {
newClient, _, err := NewClientAndStats(timeout, upto, client)
if err != nil {
return nil, err
}
return newClient, nil
}
// NewClientAndStats returns a new http.Client which implements hedged requests pattern
// And Stats object that can be queried to obtain client's metrics.
// Given Client starts a new request after a timeout from previous request.
// Starts no more than upto requests.
func NewClientAndStats(timeout time.Duration, upto int, client *http.Client) (*http.Client, *Stats, error) {
if client == nil {
client = &http.Client{
Timeout: 5 * time.Second,
}
}
newTransport, metrics, err := NewRoundTripperAndStats(timeout, upto, client.Transport)
if err != nil {
return nil, nil, err
}
client.Transport = newTransport
return client, metrics, nil
}
// NewRoundTripper returns a new http.RoundTripper which implements hedged requests pattern.
// Given RoundTripper starts a new request after a timeout from previous request.
// Starts no more than upto requests.
func NewRoundTripper(timeout time.Duration, upto int, rt http.RoundTripper) (http.RoundTripper, error) {
newRT, _, err := NewRoundTripperAndStats(timeout, upto, rt)
if err != nil {
return nil, err
}
return newRT, nil
}
// NewRoundTripperAndStats returns a new http.RoundTripper which implements hedged requests pattern
// And Stats object that can be queried to obtain client's metrics.
// Given RoundTripper starts a new request after a timeout from previous request.
// Starts no more than upto requests.
func NewRoundTripperAndStats(timeout time.Duration, upto int, rt http.RoundTripper) (http.RoundTripper, *Stats, error) {
switch {
case timeout < 0:
return nil, nil, errors.New("hedgedhttp: timeout cannot be negative")
case upto < 0:
return nil, nil, errors.New("hedgedhttp: upto cannot be negative")
}
if rt == nil {
rt = http.DefaultTransport
}
if timeout == 0 {
timeout = time.Nanosecond // smallest possible timeout if not set
}
hedged := &hedgedTransport{
rt: rt,
timeout: timeout,
upto: upto,
metrics: &Stats{},
}
return hedged, hedged.metrics, nil
}
type hedgedTransport struct {
rt http.RoundTripper
timeout time.Duration
upto int
next NextFn
metrics *Stats
}
func (ht *hedgedTransport) RoundTrip(req *http.Request) (*http.Response, error) {
mainCtx := req.Context()
upto, timeout := ht.upto, ht.timeout
if ht.next != nil {
upto, timeout = ht.next()
}
// no hedged requests, just a regular one.
if upto <= 0 {
return ht.rt.RoundTrip(req)
}
// rollback to default timeout.
if timeout < 0 {
timeout = ht.timeout
}
errOverall := []error{}
resultCh := make(chan indexedResp, upto)
errorCh := make(chan error, upto)
ht.metrics.requestedRoundTripsInc()
resultIdx := -1
cancels := make([]func(), upto)
defer runInPool(func() {
for i, cancel := range cancels {
if i != resultIdx && cancel != nil {
ht.metrics.canceledSubRequestsInc()
cancel()
}
}
})
for sent := 0; len(errOverall) < upto; sent++ {
if sent < upto {
idx := sent
subReq, cancel := reqWithCtx(req, mainCtx, idx != 0)
cancels[idx] = cancel
runInPool(func() {
ht.metrics.actualRoundTripsInc()
resp, err := ht.rt.RoundTrip(subReq)
if err != nil {
ht.metrics.failedRoundTripsInc()
errorCh <- err
} else {
resultCh <- indexedResp{idx, resp}
}
})
}
// all request sent - effectively disabling timeout between requests
if sent == upto {
timeout = infiniteTimeout
}
resp, err := waitResult(mainCtx, resultCh, errorCh, timeout)
switch {
case resp.Resp != nil:
resultIdx = resp.Index
if resultIdx == 0 {
ht.metrics.originalRequestWinsInc()
} else {
ht.metrics.hedgedRequestWinsInc()
}
return resp.Resp, nil
case mainCtx.Err() != nil:
ht.metrics.canceledByUserRoundTripsInc()
return nil, mainCtx.Err()
case err != nil:
errOverall = append(errOverall, err)
}
}
// all request have returned errors
return nil, errors.Join(errOverall...)
}
func waitResult(ctx context.Context, resultCh <-chan indexedResp, errorCh <-chan error, timeout time.Duration) (indexedResp, error) {
// try to read result first before blocking on all other channels
select {
case res := <-resultCh:
return res, nil
default:
timer := getTimer(timeout)
defer returnTimer(timer)
select {
case res := <-resultCh:
return res, nil
case reqErr := <-errorCh:
return indexedResp{}, reqErr
case <-ctx.Done():
return indexedResp{}, ctx.Err()
case <-timer.C:
return indexedResp{}, nil // it's not a request timeout, it's timeout BETWEEN consecutive requests
}
}
}
type indexedResp struct {
Index int
Resp *http.Response
}
func reqWithCtx(r *http.Request, ctx context.Context, isHedged bool) (*http.Request, context.CancelFunc) {
ctx, cancel := context.WithCancel(ctx)
if isHedged {
ctx = context.WithValue(ctx, hedgedRequest{}, struct{}{})
}
req := r.WithContext(ctx)
return req, cancel
}
type hedgedRequest struct{}
// IsHedgedRequest reports when a request is hedged.
func IsHedgedRequest(r *http.Request) bool {
val := r.Context().Value(hedgedRequest{})
return val != nil
}
var taskQueue = make(chan func())
func runInPool(task func()) {
select {
case taskQueue <- task:
// submitted, everything is ok
default:
go func() {
// do the given task
task()
const cleanupDuration = 10 * time.Second
cleanupTicker := time.NewTicker(cleanupDuration)
defer cleanupTicker.Stop()
for {
select {
case t := <-taskQueue:
t()
cleanupTicker.Reset(cleanupDuration)
case <-cleanupTicker.C:
return
}
}
}()
}
}
var timerPool = sync.Pool{New: func() interface{} {
return time.NewTimer(time.Second)
}}
func getTimer(duration time.Duration) *time.Timer {
timer := timerPool.Get().(*time.Timer)
timer.Reset(duration)
return timer
}
func returnTimer(timer *time.Timer) {
timer.Stop()
select {
case <-timer.C:
default:
}
timerPool.Put(timer)
}