-
Notifications
You must be signed in to change notification settings - Fork 36
/
timeout_retry_strategy.go
63 lines (52 loc) · 1.14 KB
/
timeout_retry_strategy.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
57
58
59
60
61
62
63
package retrystrategy
import (
"time"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
)
type Clock interface {
Sleep(time.Duration)
Now() time.Time
}
type timeoutRetryStrategy struct {
timeout time.Duration
delay time.Duration
retryable Retryable
timeService Clock
logger boshlog.Logger
logTag string
}
func NewTimeoutRetryStrategy(
timeout time.Duration,
delay time.Duration,
retryable Retryable,
timeService Clock,
logger boshlog.Logger,
) RetryStrategy {
return &timeoutRetryStrategy{
timeout: timeout,
delay: delay,
retryable: retryable,
timeService: timeService,
logger: logger,
logTag: "timeoutRetryStrategy",
}
}
func (s *timeoutRetryStrategy) Try() error {
var err error
var shouldRetry bool
now := s.timeService.Now()
deadlineMinusDelay := now.Add(s.timeout).Add(-1 * s.delay)
for i := 0; true; i++ {
s.logger.Debug(s.logTag, "Making attempt #%d", i)
shouldRetry, err = s.retryable.Attempt()
if !shouldRetry {
return err
}
now = s.timeService.Now()
if now.After(deadlineMinusDelay) {
return err
}
s.timeService.Sleep(s.delay)
}
return err
}