diff options
author | Alex Pilon <apilon@hashicorp.com> | 2019-02-22 18:24:37 -0500 |
---|---|---|
committer | Alex Pilon <apilon@hashicorp.com> | 2019-02-22 18:24:37 -0500 |
commit | 15c0b25d011f37e7c20aeca9eaf461f78285b8d9 (patch) | |
tree | 255c250a5c9d4801c74092d33b7337d8c14438ff /vendor/google.golang.org/grpc/backoff.go | |
parent | 07971ca38143c5faf951d152fba370ddcbe26ad5 (diff) | |
download | terraform-provider-statuscake-15c0b25d011f37e7c20aeca9eaf461f78285b8d9.tar.gz terraform-provider-statuscake-15c0b25d011f37e7c20aeca9eaf461f78285b8d9.tar.zst terraform-provider-statuscake-15c0b25d011f37e7c20aeca9eaf461f78285b8d9.zip |
deps: github.com/hashicorp/terraform@sdk-v0.11-with-go-modules
Updated via: go get github.com/hashicorp/terraform@sdk-v0.11-with-go-modules and go mod tidy
Diffstat (limited to 'vendor/google.golang.org/grpc/backoff.go')
-rw-r--r-- | vendor/google.golang.org/grpc/backoff.go | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/vendor/google.golang.org/grpc/backoff.go b/vendor/google.golang.org/grpc/backoff.go new file mode 100644 index 0000000..090fbe8 --- /dev/null +++ b/vendor/google.golang.org/grpc/backoff.go | |||
@@ -0,0 +1,98 @@ | |||
1 | /* | ||
2 | * | ||
3 | * Copyright 2017 gRPC authors. | ||
4 | * | ||
5 | * Licensed under the Apache License, Version 2.0 (the "License"); | ||
6 | * you may not use this file except in compliance with the License. | ||
7 | * You may obtain a copy of the License at | ||
8 | * | ||
9 | * http://www.apache.org/licenses/LICENSE-2.0 | ||
10 | * | ||
11 | * Unless required by applicable law or agreed to in writing, software | ||
12 | * distributed under the License is distributed on an "AS IS" BASIS, | ||
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
14 | * See the License for the specific language governing permissions and | ||
15 | * limitations under the License. | ||
16 | * | ||
17 | */ | ||
18 | |||
19 | package grpc | ||
20 | |||
21 | import ( | ||
22 | "math/rand" | ||
23 | "time" | ||
24 | ) | ||
25 | |||
26 | // DefaultBackoffConfig uses values specified for backoff in | ||
27 | // https://github.com/grpc/grpc/blob/master/doc/connection-backoff.md. | ||
28 | var ( | ||
29 | DefaultBackoffConfig = BackoffConfig{ | ||
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 | } | ||
48 | |||
49 | // BackoffConfig defines the parameters for the default gRPC backoff strategy. | ||
50 | type BackoffConfig struct { | ||
51 | // MaxDelay is the upper bound of backoff delay. | ||
52 | 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 | } | ||