-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrappers.go
79 lines (68 loc) · 2.14 KB
/
wrappers.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package retry
import (
"fmt"
"time"
)
// Wrapper of strategy.
type Wrapper interface {
Wrap(s Strategy) Strategy
}
// MaxRetriesWrapper wraps the strategy with the max retrying stopper.
type MaxRetriesWrapper struct {
MaxRetries int
Strategy Strategy
}
// MaxRetries wraps the strategy with the max retrying stopper.
func MaxRetries(n int, strategy Strategy) MaxRetriesWrapper {
return MaxRetriesWrapper{MaxRetries: n, Strategy: strategy}
}
// MaxRetries wraps the strategy with the max retrying stopper.
// func WithMaxRetries(n int, strategy Strategy) MaxRetriesWrapper {
// return MaxRetriesWrapper{MaxRetries: n, Strategy: strategy}
// }
// Wrap other Strategy.
func (w MaxRetriesWrapper) Wrap(s Strategy) Strategy {
return MaxRetriesWrapper{
MaxRetries: w.MaxRetries,
Strategy: s,
}
}
// Iterator returns an iterator that iterate over the inherited iterator and stops when the count of retries will be exhausted.
func (w MaxRetriesWrapper) Iterator() Iterator {
n := 0
iter := w.Strategy.Iterator()
return func() (time.Duration, error) {
if n >= w.MaxRetries {
return StopDelay, fmt.Errorf("maximum retries elapsed: %d", w.MaxRetries)
}
n++
return iter()
}
}
// MaxElapsedTimeWrapper wraps the strategy with the max elapsed time stopper.
type MaxElapsedTimeWrapper struct {
MaxElapsedTime time.Duration
Strategy Strategy
}
// MaxElapsedTime wraps the strategy with the max elapsed time stopper.
func MaxElapsedTime(d time.Duration, strategy Strategy) MaxElapsedTimeWrapper {
return MaxElapsedTimeWrapper{MaxElapsedTime: d, Strategy: strategy}
}
// Wrap other Strategy.
func (w MaxElapsedTimeWrapper) Wrap(s Strategy) Strategy {
return MaxElapsedTimeWrapper{
MaxElapsedTime: w.MaxElapsedTime,
Strategy: s,
}
}
// Iterator returns an iterator that iterate over the inherited iterator and stops when the time be elapsed.
func (w MaxElapsedTimeWrapper) Iterator() Iterator {
start := time.Now()
iter := w.Strategy.Iterator()
return func() (time.Duration, error) {
if time.Since(start) > w.MaxElapsedTime {
return StopDelay, fmt.Errorf("retrying time elapsed: %s", w.MaxElapsedTime.String())
}
return iter()
}
}