mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-14 23:16:49 +00:00
Refactoring the timeout retries into a more general 'retry policy' with support for retrying on HTTP 429 (Too Many Requests) and other responses with a `Retry-After` header The delay between requests is controlled by a combination of a 'backoff' (currently only a simple linear backoff), and honouring the `Retry-After` value (longest delay wins) This makes the 'rate limiter' an optional argument as well, removing the use of `nil` when one isn't supplied Signed-off-by: David Ackroyd <daveo.ackroyd@gmail.com>
23 lines
496 B
Go
23 lines
496 B
Go
package request
|
|
|
|
import (
|
|
"time"
|
|
)
|
|
|
|
// DefaultBackoff is a default strategy for backoff after a retryable request failure.
|
|
func DefaultBackoff() Backoff {
|
|
return LinearBackoff(100*time.Millisecond, time.Second)
|
|
}
|
|
|
|
// LinearBackoff applies a backoff increasing by a base amount with each retry capped at a maximum duration.
|
|
func LinearBackoff(base, max time.Duration) Backoff {
|
|
return func(n int) time.Duration {
|
|
d := base * time.Duration(n)
|
|
if d > max {
|
|
return max
|
|
}
|
|
|
|
return d
|
|
}
|
|
}
|