diff options
author | David Garcia Quintas <dgq@google.com> | 2017-12-04 16:12:54 -0800 |
---|---|---|
committer | David Garcia Quintas <dgq@google.com> | 2017-12-04 16:12:54 -0800 |
commit | 0f91e513d9dc9bee529701ba254933eb7be07b38 (patch) | |
tree | 87a5af062b2139ebc3969d768105e2687244d7cc /src/core/lib/backoff | |
parent | 460b5e0cd39c0f3de9f4202dad48c6b4ecd23199 (diff) |
Cleaned up API. Backoff now returns a single value: the time of the next retry
Diffstat (limited to 'src/core/lib/backoff')
-rw-r--r-- | src/core/lib/backoff/backoff.cc | 27 | ||||
-rw-r--r-- | src/core/lib/backoff/backoff.h | 25 |
2 files changed, 19 insertions, 33 deletions
diff --git a/src/core/lib/backoff/backoff.cc b/src/core/lib/backoff/backoff.cc index a5128a7e6e..5553e6b3f3 100644 --- a/src/core/lib/backoff/backoff.cc +++ b/src/core/lib/backoff/backoff.cc @@ -29,8 +29,9 @@ namespace { /* Generate a random number between 0 and 1. We roll our own RNG because seeding * rand() modifies a global variable we have no control over. */ double generate_uniform_random_number(uint32_t* rng_state) { - *rng_state = (1103515245 * *rng_state + 12345) % ((uint32_t)1 << 31); - return *rng_state / (double)((uint32_t)1 << 31); + constexpr uint32_t two_raise_31 = uint32_t(1) << 31; + *rng_state = (1103515245 * *rng_state + 12345) % two_raise_31; + return *rng_state / static_cast<double>(two_raise_31); } double generate_uniform_random_number_between(uint32_t* rng_state, double a, @@ -42,35 +43,29 @@ double generate_uniform_random_number_between(uint32_t* rng_state, double a, } } // namespace -Backoff::Backoff(const Options& options) : options_(options) { - rng_state_ = (unsigned int)gpr_now(GPR_CLOCK_REALTIME).tv_nsec; +BackOff::BackOff(const Options& options) : options_(options) { + rng_state_ = static_cast<uint32_t>(gpr_now(GPR_CLOCK_REALTIME).tv_nsec); } -Backoff::Result Backoff::Begin(grpc_exec_ctx* exec_ctx) { +grpc_millis BackOff::Begin(grpc_exec_ctx* exec_ctx) { current_backoff_ = options_.initial_backoff(); - const grpc_millis initial_timeout = - std::max(options_.initial_backoff(), options_.min_connect_timeout()); - const grpc_millis now = grpc_exec_ctx_now(exec_ctx); - return Backoff::Result{now + initial_timeout, now + current_backoff_}; + return current_backoff_ + grpc_exec_ctx_now(exec_ctx); } -Backoff::Result Backoff::Step(grpc_exec_ctx* exec_ctx) { +grpc_millis BackOff::Step(grpc_exec_ctx* exec_ctx) { current_backoff_ = (grpc_millis)(std::min(current_backoff_ * options_.multiplier(), (double)options_.max_backoff())); const double jitter = generate_uniform_random_number_between( &rng_state_, -options_.jitter() * current_backoff_, options_.jitter() * current_backoff_); - const grpc_millis current_timeout = std::max( - (grpc_millis)(current_backoff_ + jitter), options_.min_connect_timeout()); const grpc_millis next_timeout = std::min( (grpc_millis)(current_backoff_ + jitter), options_.max_backoff()); - const grpc_millis now = grpc_exec_ctx_now(exec_ctx); - return Backoff::Result{now + current_timeout, now + next_timeout}; + return next_timeout + grpc_exec_ctx_now(exec_ctx); } -void Backoff::Reset() { current_backoff_ = options_.initial_backoff(); } +void BackOff::Reset() { current_backoff_ = options_.initial_backoff(); } -void Backoff::SetRandomSeed(uint32_t seed) { rng_state_ = seed; } +void BackOff::SetRandomSeed(uint32_t seed) { rng_state_ = seed; } } // namespace grpc_core diff --git a/src/core/lib/backoff/backoff.h b/src/core/lib/backoff/backoff.h index 3e5241c226..5ba05e1d75 100644 --- a/src/core/lib/backoff/backoff.h +++ b/src/core/lib/backoff/backoff.h @@ -25,20 +25,19 @@ namespace grpc_core { /// Implementation of the backoff mechanism described in /// doc/connection-backoff.md -class Backoff { +class BackOff { public: class Options; - struct Result; /// Initialize backoff machinery - does not need to be destroyed - explicit Backoff(const Options& options); + explicit BackOff(const Options& options); - /// Begin retry loop: returns the deadlines to be used for the current attempt - /// and the subsequent retry, if any. - Result Begin(grpc_exec_ctx* exec_ctx); - /// Step a retry loop: returns the deadlines to be used for the current - /// attempt and the subsequent retry, if any. - Result Step(grpc_exec_ctx* exec_ctx); + /// Begin retry loop: returns the deadline to be used for the next attempt, + /// following the backoff / strategy. + grpc_millis Begin(grpc_exec_ctx* exec_ctx); + /// Step a retry loop: returns returns the deadline to be used for the next + /// attempt, / following the backoff / strategy. + grpc_millis Step(grpc_exec_ctx* exec_ctx); /// Reset the backoff, so the next grpc_backoff_step will be a /// grpc_backoff_begin. void Reset(); @@ -86,14 +85,6 @@ class Backoff { grpc_millis max_backoff_; }; // class Options - struct Result { - /// Deadline to be used for the current attempt. - grpc_millis current_deadline; - /// Deadline to be used for the next attempt, following the backoff - /// strategy. - grpc_millis next_attempt_start_time; - }; - private: const Options options_; /// current delay before retries |