3 * Copyright 2017 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
26 // DefaultBackoffConfig uses values specified for backoff in
27 // https://github.com/grpc/grpc/blob/master/doc/connection-backoff.md.
28 var DefaultBackoffConfig = BackoffConfig{
29 MaxDelay: 120 * time.Second,
30 baseDelay: 1.0 * time.Second,
35 // backoffStrategy defines the methodology for backing off after a grpc
36 // connection failure.
38 // This is unexported until the gRPC project decides whether or not to allow
39 // alternative backoff strategies. Once a decision is made, this type and its
40 // method may be exported.
41 type backoffStrategy interface {
42 // backoff returns the amount of time to wait before the next retry given
43 // the number of consecutive failures.
44 backoff(retries int) time.Duration
47 // BackoffConfig defines the parameters for the default gRPC backoff strategy.
48 type BackoffConfig struct {
49 // MaxDelay is the upper bound of backoff delay.
50 MaxDelay time.Duration
52 // TODO(stevvooe): The following fields are not exported, as allowing
53 // changes would violate the current gRPC specification for backoff. If
54 // gRPC decides to allow more interesting backoff strategies, these fields
55 // may be opened up in the future.
57 // baseDelay is the amount of time to wait before retrying after the first
59 baseDelay time.Duration
61 // factor is applied to the backoff after each retry.
64 // jitter provides a range to randomize backoff delays.
68 func setDefaults(bc *BackoffConfig) {
70 *bc = DefaultBackoffConfig
77 func (bc BackoffConfig) backoff(retries int) time.Duration {
81 backoff, max := float64(bc.baseDelay), float64(bc.MaxDelay)
82 for backoff < max && retries > 0 {
89 // Randomize backoff delays so that if a cluster of requests start at
90 // the same time, they won't operate in lockstep.
91 backoff *= 1 + bc.jitter*(rand.Float64()*2-1)
95 return time.Duration(backoff)