-
Notifications
You must be signed in to change notification settings - Fork 0
/
option.go
65 lines (55 loc) · 1.53 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package httpclient
import (
"net/http"
"time"
)
// Option defines `Client` option
type Option func(*Client)
// OptionDelay sets HTTP client delay between attempts
func OptionDelay(delay time.Duration) Option {
return func(client *Client) {
client.Retryer.Delay = delay
}
}
// OptionTimeout sets HTTP client timeout
func OptionTimeout(timeout time.Duration) Option {
return func(client *Client) {
client.Client.Timeout = timeout
}
}
// OptionAttempts sets retry attempts
func OptionAttempts(attempts uint) Option {
return func(client *Client) {
client.Retryer.Attempts = attempts
}
}
// OptionHTTPClient sets HTTP client
func OptionHTTPClient(httpClient *http.Client) Option {
return func(client *Client) {
client.Client = httpClient
}
}
// OptionBackoff sets `Client` retry backoff
func OptionBackoff(fn func(n uint, delay time.Duration) time.Duration) Option {
return func(client *Client) {
client.Retryer.Backoff = fn
}
}
// OptionJitter sets `Client` retry jitter
func OptionJitter(fn func(backoff time.Duration) time.Duration) Option {
return func(client *Client) {
client.Retryer.Jitter = fn
}
}
// OptionAddPrehook adds a prehook to `Client`
func OptionAddPrehook(prehook func(request *http.Request)) Option {
return func(client *Client) {
client.Prehooks = append(client.Prehooks, prehook)
}
}
// OptionAddPosthook adds a posthook to `Client`
func OptionAddPosthook(posthook func(response *http.Response, err error)) Option {
return func(client *Client) {
client.Posthooks = append(client.Posthooks, posthook)
}
}