]> git.immae.eu Git - github/fretlink/terraform-provider-statuscake.git/blob - vendor/google.golang.org/api/gensupport/backoff.go
Upgrade to 0.12
[github/fretlink/terraform-provider-statuscake.git] / vendor / google.golang.org / api / gensupport / backoff.go
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 }