This repository has been archived by the owner on Feb 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
retry.go
58 lines (49 loc) · 1.52 KB
/
retry.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
package components
import (
"context"
"net/http"
"time"
"github.com/asecurityteam/transport"
)
var (
defaultRetryCodes = []int{500, 501, 502, 503, 504, 505, 506, 507, 508, 509, 510, 511}
defaultRetryLimit = 3
defaultRetryBackoff = 50 * time.Millisecond
)
// RetryConfig enables automated retires for status codes.
type RetryConfig struct {
Codes []int `description:"HTTP status codes that trigger a retry."`
Limit int `description:"Maximum retry attempts."`
Backoff time.Duration `description:"Time to wait between requests."`
}
// Name of the configuration root.
func (*RetryConfig) Name() string {
return "retry"
}
// RetryComponent implements the settings.Component interface.
type RetryComponent struct{}
// Retry satisfies the NewComponent signature.
func Retry(_ context.Context, _ string, _ string, _ string) (interface{}, error) {
return &RetryComponent{}, nil
}
// Settings generates a config populated with defaults.
func (*RetryComponent) Settings() *RetryConfig {
return &RetryConfig{
Codes: defaultRetryCodes,
Backoff: defaultRetryBackoff,
Limit: defaultRetryLimit,
}
}
// New generates the middleware.
func (*RetryComponent) New(_ context.Context, conf *RetryConfig) (func(http.RoundTripper) http.RoundTripper, error) { // nolint
return transport.NewRetrier(
transport.NewPercentJitteredBackoffPolicy(
transport.NewFixedBackoffPolicy(conf.Backoff),
.20,
),
transport.NewLimitedRetryPolicy(
conf.Limit,
transport.NewStatusCodeRetryPolicy(conf.Codes...),
),
), nil
}