@@ -207,23 +207,31 @@ type ConditionFunc func() (done bool, err error)
207
207
type Backoff struct {
208
208
// The initial duration.
209
209
Duration time.Duration
210
- // Duration is multiplied by factor each iteration. Must be greater
211
- // than or equal to zero.
210
+ // Duration is multiplied by factor each iteration, if factor is not zero
211
+ // and the limits imposed by Steps and Cap have not been reached.
212
+ // Should not be negative.
213
+ // The jitter does not contribute to the updates to the duration parameter.
212
214
Factor float64
213
- // The amount of jitter applied each iteration. Jitter is applied after
214
- // cap.
215
+ // The sleep at each iteration is the duration plus an additional
216
+ // amount chosen uniformly at random from the interval between
217
+ // zero and `jitter*duration`.
215
218
Jitter float64
216
- // The number of steps before duration stops changing. If zero, initial
217
- // duration is always used. Used for exponential backoff in combination
218
- // with Factor.
219
+ // The remaining number of iterations in which the duration
220
+ // parameter may change (but progress can be stopped earlier by
221
+ // hitting the cap). If not positive, the duration is not
222
+ // changed. Used for exponential backoff in combination with
223
+ // Factor and Cap.
219
224
Steps int
220
- // The returned duration will never be greater than cap *before* jitter
221
- // is applied. The actual maximum cap is `cap * (1.0 + jitter)`.
225
+ // A limit on revised values of the duration parameter. If a
226
+ // multiplication by the factor parameter would make the duration
227
+ // exceed the cap then the duration is set to the cap and the
228
+ // steps parameter is set to zero.
222
229
Cap time.Duration
223
230
}
224
231
225
- // Step returns the next interval in the exponential backoff. This method
226
- // will mutate the provided backoff.
232
+ // Step (1) returns an amount of time to sleep determined by the
233
+ // original Duration and Jitter and (2) mutates the provided Backoff
234
+ // to update its Steps and Duration.
227
235
func (b * Backoff ) Step () time.Duration {
228
236
if b .Steps < 1 {
229
237
if b .Jitter > 0 {
@@ -271,14 +279,14 @@ func contextForChannel(parentCh <-chan struct{}) (context.Context, context.Cance
271
279
272
280
// ExponentialBackoff repeats a condition check with exponential backoff.
273
281
//
274
- // It checks the condition up to Steps times, increasing the wait by multiplying
275
- // the previous duration by Factor .
276
- //
277
- // If Jitter is greater than zero, a random amount of each duration is added
278
- // (between duration and duration*(1+jitter)).
279
- //
280
- // If the condition never returns true, ErrWaitTimeout is returned. All other
281
- // errors terminate immediately .
282
+ // It repeatedly checks the condition and then sleeps, using `backoff.Step()`
283
+ // to determine the length of the sleep and adjust Duration and Steps .
284
+ // Stops and returns as soon as:
285
+ // 1. the condition check returns true or an error,
286
+ // 2. `backoff.Steps` checks of the condition have been done, or
287
+ // 3. a sleep truncated by the cap on duration has been completed.
288
+ // In case (1) the returned error is what the condition function returned.
289
+ // In all other cases, ErrWaitTimeout is returned .
282
290
func ExponentialBackoff (backoff Backoff , condition ConditionFunc ) error {
283
291
for backoff .Steps > 0 {
284
292
if ok , err := condition (); err != nil || ok {
0 commit comments