-
Notifications
You must be signed in to change notification settings - Fork 175
/
config.go
89 lines (75 loc) · 2.5 KB
/
config.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package requester
import (
"math"
"time"
)
type Config struct {
BatchInterval time.Duration // minimum interval between requests
BatchThreshold uint // maximum batch size for one request
RetryInitial time.Duration // interval after which we retry request for an entity
RetryFunction RetryFunc // function determining growth of retry interval
RetryMaximum time.Duration // maximum interval for retrying request for an entity
RetryAttempts uint // maximum amount of request attemps per entity
}
type RetryFunc func(time.Duration) time.Duration
func RetryConstant() RetryFunc {
return func(interval time.Duration) time.Duration {
return interval
}
}
func RetryLinear(increase time.Duration) RetryFunc {
return func(interval time.Duration) time.Duration {
return interval + increase
}
}
func RetryGeometric(factor float64) RetryFunc {
return func(interval time.Duration) time.Duration {
return time.Duration(float64(interval) * factor)
}
}
func RetryExponential(exponent float64) RetryFunc {
return func(interval time.Duration) time.Duration {
return time.Duration(math.Pow(float64(interval), exponent))
}
}
type OptionFunc func(*Config)
// WithBatchInterval sets a custom interval at which we scan for pending items
// and batch them for requesting.
func WithBatchInterval(interval time.Duration) OptionFunc {
return func(cfg *Config) {
cfg.BatchInterval = interval
}
}
// WithBatchThreshold sets a custom threshold for the maximum size of a batch.
// If we have the given amount of pending items, we immediately send a batch.
func WithBatchThreshold(threshold uint) OptionFunc {
return func(cfg *Config) {
cfg.BatchThreshold = threshold
}
}
// WithRetryInitial sets the initial interval for dispatching a request for the
// second time.
func WithRetryInitial(interval time.Duration) OptionFunc {
return func(cfg *Config) {
cfg.RetryInitial = interval
}
}
// WithRetryFunction sets the function at which the retry interval increases.
func WithRetryFunction(retry RetryFunc) OptionFunc {
return func(cfg *Config) {
cfg.RetryFunction = retry
}
}
// WithRetryMaximum sets the maximum retry interval at which we will retry.
func WithRetryMaximum(interval time.Duration) OptionFunc {
return func(cfg *Config) {
cfg.RetryMaximum = interval
}
}
// WithRetryAttempts sets the number of attempts we will make before we give
// up on retrying. Use zero for infinite retries.
func WithRetryAttempts(attempts uint) OptionFunc {
return func(cfg *Config) {
cfg.RetryAttempts = attempts
}
}