-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbackoff.go
44 lines (37 loc) · 1.05 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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package dbw
import (
"math"
"math/rand"
"time"
)
// Backoff defines an interface for providing a back off for retrying
// transactions. See DoTx(...)
type Backoff interface {
Duration(attemptNumber uint) time.Duration
}
// ConstBackoff defines a constant backoff for retrying transactions. See
// DoTx(...)
type ConstBackoff struct {
DurationMs time.Duration
}
// Duration is the constant backoff duration based on the retry attempt
func (b ConstBackoff) Duration(attempt uint) time.Duration {
return time.Millisecond * time.Duration(b.DurationMs)
}
// ExpBackoff defines an exponential backoff for retrying transactions. See DoTx(...)
type ExpBackoff struct {
testRand float64
}
// Duration is the exponential backoff duration based on the retry attempt
func (b ExpBackoff) Duration(attempt uint) time.Duration {
var r float64
switch {
case b.testRand > 0:
r = b.testRand
default:
r = rand.Float64()
}
return time.Millisecond * time.Duration(math.Exp2(float64(attempt))*5*(r+0.5))
}