-
Notifications
You must be signed in to change notification settings - Fork 0
/
backoff.go
52 lines (44 loc) · 1.2 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
47
48
49
50
51
52
package main
import (
"time"
"github.com/cenkalti/backoff/v4"
"github.com/sirupsen/logrus"
)
type backOffConfig struct {
InitialInterval time.Duration `yaml:"initial-interval"`
Multiplier float64 `yaml:"multiplier"`
MaxInterval time.Duration `yaml:"max-interval"`
MaxElapsedTime time.Duration `yaml:"max-elapsed-time"`
}
func newBackOffConfig() backOffConfig {
return backOffConfig{
InitialInterval: 1 * time.Second,
Multiplier: 1.1,
MaxInterval: 10 * time.Second,
MaxElapsedTime: 0,
}
}
func (cfg *backOffConfig) New() backoff.BackOff {
b := backoff.NewExponentialBackOff()
b.InitialInterval = cfg.InitialInterval
b.Multiplier = cfg.Multiplier
b.MaxInterval = cfg.MaxInterval
b.MaxElapsedTime = cfg.MaxElapsedTime
b.Reset()
return b
}
// metadataRetry is a utility function retry metadata-receiving functions
func metadataRetry(operation backoff.Operation) error {
bo := backoff.NewExponentialBackOff()
bo.InitialInterval = 100 * time.Millisecond
bo.MaxInterval = 1 * time.Second
bo.MaxElapsedTime = 10 * time.Second
bo.Reset()
return backoff.Retry(func() error {
if err := operation(); err != nil {
logrus.Debug(err)
return err
}
return nil
}, bo)
}