@@ -18,14 +18,14 @@ const (
1818
1919// Default parameters used by Retry() functions within different sub packages.
2020var (
21- FastBackoff = logBackoff {
22- SlotDuration : fastSlot ,
23- Ceiling : 6 ,
24- }
25- SlowBackoff = logBackoff {
26- SlotDuration : slowSlot ,
27- Ceiling : 6 ,
28- }
21+ FastBackoff = newBackoff (
22+ withSlotDuration ( fastSlot ) ,
23+ withCeiling ( 6 ) ,
24+ )
25+ SlowBackoff = newBackoff (
26+ withSlotDuration ( slowSlot ) ,
27+ withCeiling ( 6 ) ,
28+ )
2929)
3030
3131// retryOperation is the interface that holds an operation for retry.
@@ -226,6 +226,39 @@ type logBackoff struct {
226226 // where F is a result of multiplication of this value and calculated delay
227227 // duration D; and R is a random sized part from [0,(D - F)].
228228 JitterLimit float64
229+
230+ // generator of jitter
231+ r rand.Rand
232+ }
233+
234+ type option func (b * logBackoff )
235+
236+ func withSlotDuration (slotDuration time.Duration ) option {
237+ return func (b * logBackoff ) {
238+ b .SlotDuration = slotDuration
239+ }
240+ }
241+
242+ func withCeiling (ceiling uint ) option {
243+ return func (b * logBackoff ) {
244+ b .Ceiling = ceiling
245+ }
246+ }
247+
248+ func withJitterLimit (jitterLimit float64 ) option {
249+ return func (b * logBackoff ) {
250+ b .JitterLimit = jitterLimit
251+ }
252+ }
253+
254+ func newBackoff (opts ... option ) logBackoff {
255+ b := logBackoff {
256+ r : rand .New (rand .WithLock ()),
257+ }
258+ for _ , o := range opts {
259+ o (& b )
260+ }
261+ return b
229262}
230263
231264// Wait implements Backoff interface.
@@ -245,7 +278,7 @@ func (b logBackoff) delay(i int) time.Duration {
245278 if f == d {
246279 return f
247280 }
248- return f + time .Duration (rand .Int64 (int64 (d - f )+ 1 ))
281+ return f + time .Duration (b . r .Int64 (int64 (d - f )+ 1 ))
249282}
250283
251284func min (a , b uint ) uint {
0 commit comments