| 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 |
} |