-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathoption.go
43 lines (39 loc) · 1.21 KB
/
option.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
package httpretry
// Option is a function type to modify the RetryRoundtripper configuration
type Option func(*RetryRoundtripper)
// WithMaxRetryCount sets the maximum number of retries if an http request was not successful.
//
// Default: 5
func WithMaxRetryCount(maxRetryCount int) Option {
if maxRetryCount < 0 {
maxRetryCount = 0
}
return func(roundtripper *RetryRoundtripper) {
roundtripper.MaxRetryCount = maxRetryCount
}
}
// WithRetryPolicy sets the user defined retry policy.
//
// Default: RetryPolicy checks for some common errors that are likely not retryable and for status codes
// that should be retried.
//
// For example:
// - url parsing errors
// - too many redirects
// - certificate errors
// - BadGateway
// - ServiceUnavailable
// - etc.
func WithRetryPolicy(retryPolicy RetryPolicy) Option {
return func(roundtripper *RetryRoundtripper) {
roundtripper.ShouldRetry = retryPolicy
}
}
// WithBackoffPolicy sets the user defined backoff policy.
//
// Default: ExponentialBackoff(1*time.Second, 30*time.Second, 200*time.Millisecond)
func WithBackoffPolicy(backoffPolicy BackoffPolicy) Option {
return func(roundtripper *RetryRoundtripper) {
roundtripper.CalculateBackoff = backoffPolicy
}
}