-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackoff.go
46 lines (41 loc) · 1.21 KB
/
backoff.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
package retrygo
import (
"math/rand"
"time"
)
// Constant returns a RetryPolicy that always returns the same interval
// between retries.
//
// Sleep formula: interval
func Constant(interval time.Duration) RetryPolicy {
return func(ri RetryInfo) (bool, time.Duration) {
return true, interval
}
}
// Linear returns a RetryPolicy that increases the interval between retries
// linearly.
//
// Sleep formula: interval * fails
func Linear(interval time.Duration) RetryPolicy {
return func(ri RetryInfo) (bool, time.Duration) {
return true, interval * time.Duration(ri.Fails)
}
}
// Exponential returns a RetryPolicy that increases the interval between
// retries exponentially.
//
// Sleep formula: interval * 2^fails
func Exponential(interval time.Duration) RetryPolicy {
return func(ri RetryInfo) (bool, time.Duration) {
return true, interval * time.Duration(1<<uint(ri.Fails-1))
}
}
// Jitter returns a RetryPolicy that adds a random non-negative jitter to the interval
// between retries.
//
// Sleep formula: interval + rand.Int63n(interval)
func Jitter(interval time.Duration) RetryPolicy {
return func(ri RetryInfo) (bool, time.Duration) {
return true, interval + time.Duration(rand.Int63n(int64(interval)))
}
}