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