diff options
Diffstat (limited to 'vendor/google.golang.org/api/gensupport/backoff.go')
-rw-r--r-- | vendor/google.golang.org/api/gensupport/backoff.go | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/vendor/google.golang.org/api/gensupport/backoff.go b/vendor/google.golang.org/api/gensupport/backoff.go new file mode 100644 index 0000000..94b7789 --- /dev/null +++ b/vendor/google.golang.org/api/gensupport/backoff.go | |||
@@ -0,0 +1,51 @@ | |||
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. | ||
4 | |||
5 | package gensupport | ||
6 | |||
7 | import ( | ||
8 | "math/rand" | ||
9 | "time" | ||
10 | ) | ||
11 | |||
12 | // BackoffStrategy defines the set of functions that a backoff-er must | ||
13 | // implement. | ||
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) | ||
18 | |||
19 | // Reset restores the strategy to its initial state. | ||
20 | Reset() | ||
21 | } | ||
22 | |||
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 { | ||
27 | Base time.Duration | ||
28 | Max time.Duration | ||
29 | total time.Duration | ||
30 | n uint | ||
31 | } | ||
32 | |||
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 { | ||
36 | return 0, false | ||
37 | } | ||
38 | |||
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))) | ||
41 | eb.total += d | ||
42 | eb.n++ | ||
43 | return d, true | ||
44 | } | ||
45 | |||
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() { | ||
49 | eb.n = 0 | ||
50 | eb.total = 0 | ||
51 | } | ||