Skip to content

Commit 2c7862e

Browse files
committed
feat: implement jitter in backoff handling
- Add Jitter field to Message struct for randomizing backoff steps - Include jitter in Options struct and NewOptions function - Add Bool helper function for allocating new bool values - Update TestOptions to include Jitter and assert its value - Add Jitter to Queue's handle function Signed-off-by: appleboy <appleboy.tw@gmail.com>
1 parent dc3c120 commit 2c7862e

File tree

4 files changed

+18
-0
lines changed

4 files changed

+18
-0
lines changed

job/job.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff 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
}

job/option.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff 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 {
8289
func 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+
}

job/option_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff 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
}

queue.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff 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:

0 commit comments

Comments
 (0)