| 97 | } |
| 98 | |
| 99 | func (b *Backoff) NextDelay() time.Duration { |
| 100 | b.numRetries++ |
| 101 | |
| 102 | // Handle the edge case where the min and max have the same value |
| 103 | // (or due to some misconfig max is < min) |
| 104 | if b.nextDelayMin >= b.nextDelayMax { |
| 105 | return b.nextDelayMin |
| 106 | } |
| 107 | |
| 108 | // Add a jitter within the next exponential backoff range |
| 109 | sleepTime := b.nextDelayMin + time.Duration(rand.Int63n(int64(b.nextDelayMax-b.nextDelayMin))) |
| 110 | |
| 111 | // Apply the exponential backoff to calculate the next jitter |
| 112 | // range, unless we've already reached the max |
| 113 | if b.nextDelayMax < b.cfg.MaxBackoff { |
| 114 | b.nextDelayMin = doubleDuration(b.nextDelayMin, b.cfg.MaxBackoff) |
| 115 | b.nextDelayMax = doubleDuration(b.nextDelayMax, b.cfg.MaxBackoff) |
| 116 | } |
| 117 | |
| 118 | return sleepTime |
| 119 | } |
| 120 | |
| 121 | func doubleDuration(value time.Duration, max time.Duration) time.Duration { |
| 122 | value = value * 2 |