File tree Expand file tree Collapse file tree 4 files changed +18
-0
lines changed
Expand file tree Collapse file tree 4 files changed +18
-0
lines changed Original file line number Diff line number Diff line change @@ -46,6 +46,9 @@ type Message struct {
4646 // Defaults to 10 seconds.
4747 RetryMax time.Duration `json:"retry_max"`
4848
49+ // Jitter eases contention by randomizing backoff steps
50+ Jitter bool `json:"jitter"`
51+
4952 // Data to save Unsafe cast
5053 Data []byte
5154}
Original file line number Diff line number Diff line change @@ -9,6 +9,7 @@ type Options struct {
99 retryFactor float64
1010 retryMin time.Duration
1111 retryMax time.Duration
12+ jitter bool
1213
1314 timeout time.Duration
1415}
@@ -22,6 +23,7 @@ func newDefaultOptions() Options {
2223 retryMin : 100 * time .Millisecond ,
2324 retryMax : 10 * time .Second ,
2425 timeout : 60 * time .Minute ,
26+ jitter : false ,
2527 }
2628}
2729
@@ -32,6 +34,7 @@ type AllowOption struct {
3234 RetryFactor * float64
3335 RetryMin * time.Duration
3436 RetryMax * time.Duration
37+ Jitter * bool
3538 Timeout * time.Duration
3639}
3740
@@ -63,6 +66,10 @@ func NewOptions(opts ...AllowOption) Options {
6366 if opts [0 ].RetryMax != nil && * opts [0 ].RetryMax != o .retryMax {
6467 o .retryMax = * opts [0 ].RetryMax
6568 }
69+
70+ if opts [0 ].Jitter != nil && * opts [0 ].Jitter != o .jitter {
71+ o .jitter = * opts [0 ].Jitter
72+ }
6673 }
6774
6875 return o
@@ -82,3 +89,8 @@ func Float64(val float64) *float64 {
8289func Time (v time.Duration ) * time.Duration {
8390 return & v
8491}
92+
93+ // Bool is a helper routine that allocates a new bool value
94+ func Bool (val bool ) * bool {
95+ return & val
96+ }
Original file line number Diff line number Diff line change @@ -14,6 +14,7 @@ func TestOptions(t *testing.T) {
1414 RetryCount : Int64 (100 ),
1515 RetryDelay : Time (30 * time .Millisecond ),
1616 Timeout : Time (3 * time .Millisecond ),
17+ Jitter : Bool (true ),
1718 },
1819 )
1920
@@ -23,4 +24,5 @@ func TestOptions(t *testing.T) {
2324 assert .Equal (t , 100 * time .Millisecond , o .retryMin )
2425 assert .Equal (t , 10 * time .Second , o .retryMax )
2526 assert .Equal (t , 2.0 , o .retryFactor )
27+ assert .True (t , o .jitter )
2628}
Original file line number Diff line number Diff line change @@ -203,6 +203,7 @@ func (q *Queue) handle(m *job.Message) error {
203203 Min : m .RetryMin ,
204204 Max : m .RetryMax ,
205205 Factor : m .RetryFactor ,
206+ Jitter : m .Jitter ,
206207 }
207208 delay := m .RetryDelay
208209 loop:
You can’t perform that action at this time.
0 commit comments