• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright 2016 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 #ifndef GRPC_CORE_LIB_BACKOFF_BACKOFF_H
20 #define GRPC_CORE_LIB_BACKOFF_BACKOFF_H
21 
22 #include <grpc/support/port_platform.h>
23 
24 #include "src/core/lib/iomgr/exec_ctx.h"
25 
26 namespace grpc_core {
27 
28 /// Implementation of the backoff mechanism described in
29 /// doc/connection-backoff.md
30 class BackOff {
31  public:
32   class Options;
33 
34   /// Initialize backoff machinery - does not need to be destroyed
35   explicit BackOff(const Options& options);
36 
37   /// Returns the time at which the next attempt should start.
38   grpc_millis NextAttemptTime();
39 
40   /// Reset the backoff, so the next value returned by NextAttemptTime()
41   /// will be the time of the second attempt (rather than the Nth).
42   void Reset();
43 
44   void SetRandomSeed(unsigned int seed);
45 
46   class Options {
47    public:
set_initial_backoff(grpc_millis initial_backoff)48     Options& set_initial_backoff(grpc_millis initial_backoff) {
49       initial_backoff_ = initial_backoff;
50       return *this;
51     }
set_multiplier(double multiplier)52     Options& set_multiplier(double multiplier) {
53       multiplier_ = multiplier;
54       return *this;
55     }
set_jitter(double jitter)56     Options& set_jitter(double jitter) {
57       jitter_ = jitter;
58       return *this;
59     }
set_max_backoff(grpc_millis max_backoff)60     Options& set_max_backoff(grpc_millis max_backoff) {
61       max_backoff_ = max_backoff;
62       return *this;
63     }
64     /// how long to wait after the first failure before retrying
initial_backoff()65     grpc_millis initial_backoff() const { return initial_backoff_; }
66     /// factor with which to multiply backoff after a failed retry
multiplier()67     double multiplier() const { return multiplier_; }
68     /// amount to randomize backoffs
jitter()69     double jitter() const { return jitter_; }
70     /// maximum time between retries
max_backoff()71     grpc_millis max_backoff() const { return max_backoff_; }
72 
73    private:
74     grpc_millis initial_backoff_;
75     double multiplier_;
76     double jitter_;
77     grpc_millis max_backoff_;
78   };  // class Options
79 
80  private:
81   const Options options_;
82   uint32_t rng_state_;
83   bool initial_;
84   /// current delay before retries
85   grpc_millis current_backoff_;
86 };
87 
88 }  // namespace grpc_core
89 #endif /* GRPC_CORE_LIB_BACKOFF_BACKOFF_H */
90