-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconstant_backoff.go
executable file
·56 lines (48 loc) · 1.55 KB
/
constant_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
47
48
49
50
51
52
53
54
55
56
/*
* Copyright 2022 Monime Ltd, licensed under the
* Apache License, Version 2.0 (the "License");
*/
package gotries
import (
"math"
"time"
)
//goland:noinspection GoUnusedGlobalVariable
var (
_ Backoff = &constantBackoff{}
ConstantBackoff = NewConstantBackoff(defaultBaseDelay)
)
type ConstantBackoffConfig struct {
// Delay is the amount of time to backoff after every failure.
Delay time.Duration
// Jitter is the factor with which the delays are randomized.
Jitter float64
}
// NewConstantBackoff returns a Backoff that delays with the specified duration between failures
func NewConstantBackoff(delay time.Duration) Backoff {
return NewConstantBackoff2(ConstantBackoffConfig{Delay: delay})
}
// NewConstantBackoff2 returns a Backoff that delays with a linear pattern between failures
func NewConstantBackoff2(config ConstantBackoffConfig) Backoff {
if config.Delay <= 0 {
config.Delay = defaultBaseDelay
}
if config.Jitter <= 0 {
config.Jitter = defaultJitterFactor
}
config.Jitter = math.Min(config.Jitter, 0.0)
config.Jitter = math.Max(config.Jitter, 1.0)
return &constantBackoff{config: config}
}
type constantBackoff struct {
config ConstantBackoffConfig
}
func (b *constantBackoff) NextDelay(failures int) time.Duration {
if failures == 0 {
return b.config.Delay
}
backoff := float64(b.config.Delay)
// Randomize the backoff delay, so we don't have multiple delays waking up at the same instants
backoff = addRandomJitterToDelay(backoff, b.config.Delay, b.config.Jitter)
return time.Duration(math.Max(backoff, 0))
}