1 // Copyright 2016 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
12 // BackoffStrategy defines the set of functions that a backoff-er must
14 type BackoffStrategy interface {
15 // Pause returns the duration of the next pause and true if the operation should be
16 // retried, or false if no further retries should be attempted.
17 Pause() (time.Duration, bool)
19 // Reset restores the strategy to its initial state.
23 // ExponentialBackoff performs exponential backoff as per https://en.wikipedia.org/wiki/Exponential_backoff.
24 // The initial pause time is given by Base.
25 // Once the total pause time exceeds Max, Pause will indicate no further retries.
26 type ExponentialBackoff struct {
33 // Pause returns the amount of time the caller should wait.
34 func (eb *ExponentialBackoff) Pause() (time.Duration, bool) {
35 if eb.total > eb.Max {
39 // The next pause is selected from randomly from [0, 2^n * Base).
40 d := time.Duration(rand.Int63n((1 << eb.n) * int64(eb.Base)))
46 // Reset resets the backoff strategy such that the next Pause call will begin
47 // counting from the start. It is not safe to call concurrently with Pause.
48 func (eb *ExponentialBackoff) Reset() {