-
Notifications
You must be signed in to change notification settings - Fork 6
/
simple.go
52 lines (41 loc) · 895 Bytes
/
simple.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 retry
// Retry forever. No waiting.
type AlwaysRetryStrategy struct{}
func (s *AlwaysRetryStrategy) Next() bool {
return true
}
func (s *AlwaysRetryStrategy) HasNext() bool {
return true
}
// Try up to a fixed number of times
type CountStrategy struct {
Tries int
count int
}
func (s *CountStrategy) Next() bool {
if s.count < s.Tries {
s.count++
return true
}
return false
}
func (s *CountStrategy) HasNext() bool {
return s.count < s.Tries
}
func (s *CountStrategy) Reset() {
s.count = 0
}
// Always retry until canceled. Use And to combine this
// with other strategies to make them cancelable
type CancelableRetryStrategy struct {
canceled bool
}
func (s *CancelableRetryStrategy) Next() bool {
return !s.canceled
}
func (s *CancelableRetryStrategy) HasNext() bool {
return !s.canceled
}
func (s *CancelableRetryStrategy) Cancel() {
s.canceled = true
}