...
1 package backoff
2
3 import (
4 "time"
5
6 "github.com/lestrrat-go/option"
7 )
8
9 type identInterval struct{}
10 type identJitterFactor struct{}
11 type identMaxInterval struct{}
12 type identMaxRetries struct{}
13 type identMinInterval struct{}
14 type identMultiplier struct{}
15 type identRNG struct{}
16
17
18
19
20 type ControllerOption interface {
21 ConstantOption
22 ExponentialOption
23 CommonOption
24 controllerOption()
25 }
26
27 type controllerOption struct {
28 Option
29 }
30
31 func (*controllerOption) exponentialOption() {}
32 func (*controllerOption) controllerOption() {}
33 func (*controllerOption) constantOption() {}
34
35
36 type ConstantOption interface {
37 Option
38 constantOption()
39 }
40
41 type constantOption struct {
42 Option
43 }
44
45 func (*constantOption) constantOption() {}
46
47
48 type ExponentialOption interface {
49 Option
50 exponentialOption()
51 }
52
53 type exponentialOption struct {
54 Option
55 }
56
57 func (*exponentialOption) exponentialOption() {}
58
59
60 type CommonOption interface {
61 ExponentialOption
62 ConstantOption
63 }
64
65 type commonOption struct {
66 Option
67 }
68
69 func (*commonOption) constantOption() {}
70 func (*commonOption) exponentialOption() {}
71
72
73
74
75
76
77
78
79 func WithMaxRetries(v int) ControllerOption {
80 return &controllerOption{option.New(identMaxRetries{}, v)}
81 }
82
83
84
85
86 func WithInterval(v time.Duration) ConstantOption {
87 return &constantOption{option.New(identInterval{}, v)}
88 }
89
90
91
92 func WithMaxInterval(v time.Duration) ExponentialOption {
93 return &exponentialOption{option.New(identMaxInterval{}, v)}
94 }
95
96
97
98 func WithMinInterval(v time.Duration) ExponentialOption {
99 return &exponentialOption{option.New(identMinInterval{}, v)}
100 }
101
102
103
104
105
106
107
108 func WithMultiplier(v float64) ExponentialOption {
109 return &exponentialOption{option.New(identMultiplier{}, v)}
110 }
111
112
113
114
115
116
117
118 func WithJitterFactor(v float64) CommonOption {
119 return &commonOption{option.New(identJitterFactor{}, v)}
120 }
121
122
123
124
125 func WithRNG(v Random) CommonOption {
126 return &commonOption{option.New(identRNG{}, v)}
127 }
128
View as plain text