diff options
Diffstat (limited to 'src/core/lib/backoff')
-rw-r--r-- | src/core/lib/backoff/backoff.cc | 23 | ||||
-rw-r--r-- | src/core/lib/backoff/backoff.h | 16 |
2 files changed, 21 insertions, 18 deletions
diff --git a/src/core/lib/backoff/backoff.cc b/src/core/lib/backoff/backoff.cc index 41f625a636..d561fc7460 100644 --- a/src/core/lib/backoff/backoff.cc +++ b/src/core/lib/backoff/backoff.cc @@ -41,18 +41,20 @@ double generate_uniform_random_number_between(uint32_t* rng_state, double a, const double range = b - a; return a + generate_uniform_random_number(rng_state) * range; } -} // namespace -BackOff::BackOff(const Options& options) : options_(options) { - rng_state_ = static_cast<uint32_t>(gpr_now(GPR_CLOCK_REALTIME).tv_nsec); -} +} // namespace -grpc_millis BackOff::Begin() { - current_backoff_ = options_.initial_backoff(); - return current_backoff_ + grpc_core::ExecCtx::Get()->Now(); +BackOff::BackOff(const Options& options) + : options_(options), + rng_state_(static_cast<uint32_t>(gpr_now(GPR_CLOCK_REALTIME).tv_nsec)) { + Reset(); } -grpc_millis BackOff::Step() { +grpc_millis BackOff::NextAttemptTime() { + if (initial_) { + initial_ = false; + return current_backoff_ + grpc_core::ExecCtx::Get()->Now(); + } current_backoff_ = (grpc_millis)(std::min(current_backoff_ * options_.multiplier(), (double)options_.max_backoff())); @@ -63,7 +65,10 @@ grpc_millis BackOff::Step() { return next_timeout + grpc_core::ExecCtx::Get()->Now(); } -void BackOff::Reset() { current_backoff_ = options_.initial_backoff(); } +void BackOff::Reset() { + current_backoff_ = options_.initial_backoff(); + initial_ = true; +} void BackOff::SetRandomSeed(uint32_t seed) { rng_state_ = seed; } diff --git a/src/core/lib/backoff/backoff.h b/src/core/lib/backoff/backoff.h index 84ef9b82e4..de30e5268b 100644 --- a/src/core/lib/backoff/backoff.h +++ b/src/core/lib/backoff/backoff.h @@ -32,14 +32,11 @@ class BackOff { /// Initialize backoff machinery - does not need to be destroyed explicit BackOff(const Options& options); - /// Begin retry loop: returns the deadline to be used for the next attempt, - /// following the backoff strategy. - grpc_millis Begin(); - /// Step a retry loop: returns the deadline to be used for the next attempt, - /// following the backoff strategy. - grpc_millis Step(); - /// Reset the backoff, so the next grpc_backoff_step will be a - /// grpc_backoff_begin. + /// Returns the time at which the next attempt should start. + grpc_millis NextAttemptTime(); + + /// Reset the backoff, so the next value returned by NextAttemptTime() + /// will be the time of the second attempt (rather than the Nth). void Reset(); void SetRandomSeed(unsigned int seed); @@ -80,9 +77,10 @@ class BackOff { private: const Options options_; + uint32_t rng_state_; + bool initial_; /// current delay before retries grpc_millis current_backoff_; - uint32_t rng_state_; }; } // namespace grpc_core |