-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbackoff.go
More file actions
38 lines (31 loc) · 832 Bytes
/
backoff.go
File metadata and controls
38 lines (31 loc) · 832 Bytes
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
package gocbcorex
import (
"math"
"time"
)
// BackoffCalculator is used by retry strategies to calculate backoff durations.
type BackoffCalculator func(retryAttempts uint32) time.Duration
func ExponentialBackoff(min, max time.Duration, backoffFactor float64) BackoffCalculator {
var minBackoff float64 = 1000000 // 1 Millisecond
var maxBackoff float64 = 500000000 // 500 Milliseconds
var factor float64 = 2
if min > 0 {
minBackoff = float64(min)
}
if max > 0 {
maxBackoff = float64(max)
}
if backoffFactor > 0 {
factor = backoffFactor
}
return func(retryAttempts uint32) time.Duration {
backoff := minBackoff * (math.Pow(factor, float64(retryAttempts)))
if backoff > maxBackoff {
backoff = maxBackoff
}
if backoff < minBackoff {
backoff = minBackoff
}
return time.Duration(backoff)
}
}