diff options
Diffstat (limited to 'vendor/google.golang.org/grpc/backoff.go')
-rw-r--r-- | vendor/google.golang.org/grpc/backoff.go | 70 |
1 files changed, 5 insertions, 65 deletions
diff --git a/vendor/google.golang.org/grpc/backoff.go b/vendor/google.golang.org/grpc/backoff.go index 090fbe8..fa31565 100644 --- a/vendor/google.golang.org/grpc/backoff.go +++ b/vendor/google.golang.org/grpc/backoff.go | |||
@@ -16,83 +16,23 @@ | |||
16 | * | 16 | * |
17 | */ | 17 | */ |
18 | 18 | ||
19 | // See internal/backoff package for the backoff implementation. This file is | ||
20 | // kept for the exported types and API backward compatility. | ||
21 | |||
19 | package grpc | 22 | package grpc |
20 | 23 | ||
21 | import ( | 24 | import ( |
22 | "math/rand" | ||
23 | "time" | 25 | "time" |
24 | ) | 26 | ) |
25 | 27 | ||
26 | // DefaultBackoffConfig uses values specified for backoff in | 28 | // DefaultBackoffConfig uses values specified for backoff in |
27 | // https://github.com/grpc/grpc/blob/master/doc/connection-backoff.md. | 29 | // https://github.com/grpc/grpc/blob/master/doc/connection-backoff.md. |
28 | var ( | 30 | var DefaultBackoffConfig = BackoffConfig{ |
29 | DefaultBackoffConfig = BackoffConfig{ | 31 | MaxDelay: 120 * time.Second, |
30 | MaxDelay: 120 * time.Second, | ||
31 | baseDelay: 1.0 * time.Second, | ||
32 | factor: 1.6, | ||
33 | jitter: 0.2, | ||
34 | } | ||
35 | ) | ||
36 | |||
37 | // backoffStrategy defines the methodology for backing off after a grpc | ||
38 | // connection failure. | ||
39 | // | ||
40 | // This is unexported until the gRPC project decides whether or not to allow | ||
41 | // alternative backoff strategies. Once a decision is made, this type and its | ||
42 | // method may be exported. | ||
43 | type backoffStrategy interface { | ||
44 | // backoff returns the amount of time to wait before the next retry given | ||
45 | // the number of consecutive failures. | ||
46 | backoff(retries int) time.Duration | ||
47 | } | 32 | } |
48 | 33 | ||
49 | // BackoffConfig defines the parameters for the default gRPC backoff strategy. | 34 | // BackoffConfig defines the parameters for the default gRPC backoff strategy. |
50 | type BackoffConfig struct { | 35 | type BackoffConfig struct { |
51 | // MaxDelay is the upper bound of backoff delay. | 36 | // MaxDelay is the upper bound of backoff delay. |
52 | MaxDelay time.Duration | 37 | MaxDelay time.Duration |
53 | |||
54 | // TODO(stevvooe): The following fields are not exported, as allowing | ||
55 | // changes would violate the current gRPC specification for backoff. If | ||
56 | // gRPC decides to allow more interesting backoff strategies, these fields | ||
57 | // may be opened up in the future. | ||
58 | |||
59 | // baseDelay is the amount of time to wait before retrying after the first | ||
60 | // failure. | ||
61 | baseDelay time.Duration | ||
62 | |||
63 | // factor is applied to the backoff after each retry. | ||
64 | factor float64 | ||
65 | |||
66 | // jitter provides a range to randomize backoff delays. | ||
67 | jitter float64 | ||
68 | } | ||
69 | |||
70 | func setDefaults(bc *BackoffConfig) { | ||
71 | md := bc.MaxDelay | ||
72 | *bc = DefaultBackoffConfig | ||
73 | |||
74 | if md > 0 { | ||
75 | bc.MaxDelay = md | ||
76 | } | ||
77 | } | ||
78 | |||
79 | func (bc BackoffConfig) backoff(retries int) time.Duration { | ||
80 | if retries == 0 { | ||
81 | return bc.baseDelay | ||
82 | } | ||
83 | backoff, max := float64(bc.baseDelay), float64(bc.MaxDelay) | ||
84 | for backoff < max && retries > 0 { | ||
85 | backoff *= bc.factor | ||
86 | retries-- | ||
87 | } | ||
88 | if backoff > max { | ||
89 | backoff = max | ||
90 | } | ||
91 | // Randomize backoff delays so that if a cluster of requests start at | ||
92 | // the same time, they won't operate in lockstep. | ||
93 | backoff *= 1 + bc.jitter*(rand.Float64()*2-1) | ||
94 | if backoff < 0 { | ||
95 | return 0 | ||
96 | } | ||
97 | return time.Duration(backoff) | ||
98 | } | 38 | } |