Skip to content

Commit 298c6a2

Browse files
authored
Merge pull request #154 from fastly/dgryski/send-poll-fn
fsthtp: add SendPollIntervalFn for more fine-grained control of polli…
2 parents 67539a6 + 7d609f2 commit 298c6a2

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

fsthttp/request.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ type Request struct {
9090
// value is 1ms, and the maximum value is 1s.
9191
SendPollInterval time.Duration
9292

93+
// SendPollIntervalFn allows more fine-grained control of the send poll interval.
94+
//
95+
// SendPollIntervalFn must be a function which takes an iteration number i and returns
96+
// the delay for the i'th polling interval.
97+
SendPollIntervalFn func(i int) time.Duration
98+
9399
// DecompressResponseOptions control the auto decompress response behaviour.
94100
DecompressResponseOptions DecompressResponseOptions
95101

@@ -276,6 +282,7 @@ func (req *Request) Clone() *Request {
276282
RemoteAddr: req.RemoteAddr,
277283
TLSInfo: req.TLSInfo,
278284
SendPollInterval: req.SendPollInterval,
285+
SendPollIntervalFn: req.SendPollIntervalFn,
279286
DecompressResponseOptions: req.DecompressResponseOptions,
280287
ManualFramingMode: req.ManualFramingMode,
281288
}
@@ -432,8 +439,13 @@ func (req *Request) Send(ctx context.Context, backend string) (*Response, error)
432439
}
433440

434441
func newResponseFromABIPending(ctx context.Context, req *Request, backend string, abiPending *fastly.PendingRequest, errc chan error) (*Response, error) {
435-
pollInterval := safePollInterval(req.SendPollInterval)
436-
abiResp, abiRespBody, err := pendingToABIResponse(ctx, errc, abiPending, pollInterval)
442+
pollIntervalFn := req.SendPollIntervalFn
443+
if pollIntervalFn == nil {
444+
pollIntervalFn = func(n int) time.Duration { return req.SendPollInterval }
445+
}
446+
447+
abiResp, abiRespBody, err := pendingToABIResponse(ctx, errc, abiPending, pollIntervalFn)
448+
437449
if err != nil {
438450
return nil, fmt.Errorf("poll: %w", err)
439451
}
@@ -446,7 +458,10 @@ func newResponseFromABIPending(ctx context.Context, req *Request, backend string
446458
return resp, nil
447459
}
448460

449-
func pendingToABIResponse(ctx context.Context, errc chan error, abiPending *fastly.PendingRequest, pollInterval time.Duration) (*fastly.HTTPResponse, *fastly.HTTPBody, error) {
461+
func pendingToABIResponse(ctx context.Context, errc chan error, abiPending *fastly.PendingRequest, pollIntervalFn func(int) time.Duration) (*fastly.HTTPResponse, *fastly.HTTPBody, error) {
462+
var iter int
463+
var pollInterval time.Duration
464+
450465
for {
451466
select {
452467
case <-ctx.Done():
@@ -465,6 +480,8 @@ func pendingToABIResponse(ctx context.Context, errc chan error, abiPending *fast
465480
if done {
466481
return abiResp, abiRespBody, nil
467482
}
483+
pollInterval = safePollInterval(pollIntervalFn(iter))
484+
iter++
468485
time.Sleep(pollInterval)
469486
}
470487
}

0 commit comments

Comments
 (0)