Commit 659deb38 authored by Lewis Baker's avatar Lewis Baker Committed by Facebook GitHub Bot

Add Timekeeper parameter to folly::coro::retryWithExponentialBackoff()

Summary:
The Timekeeper parameter can be used to pass in manually-controlled time-sources
for testing purposes if required.

Reviewed By: yfeldblum

Differential Revision: D23386677

fbshipit-source-id: bea09292b00f6cd018568d26e5409d2c7ff01dbe
parent 6b2d55ec
...@@ -126,12 +126,14 @@ class ExponentialBackoffWithJitter { ...@@ -126,12 +126,14 @@ class ExponentialBackoffWithJitter {
public: public:
template <typename URNG2> template <typename URNG2>
explicit ExponentialBackoffWithJitter( explicit ExponentialBackoffWithJitter(
Timekeeper* tk,
uint32_t maxRetries, uint32_t maxRetries,
Duration minBackoff, Duration minBackoff,
Duration maxBackoff, Duration maxBackoff,
double relativeJitterStdDev, double relativeJitterStdDev,
URNG2&& rng) noexcept URNG2&& rng) noexcept
: maxRetries_(maxRetries), : timeKeeper_(tk),
maxRetries_(maxRetries),
retryCount_(0), retryCount_(0),
minBackoff_(minBackoff), minBackoff_(minBackoff),
maxBackoff_(maxBackoff), maxBackoff_(maxBackoff),
...@@ -160,7 +162,7 @@ class ExponentialBackoffWithJitter { ...@@ -160,7 +162,7 @@ class ExponentialBackoffWithJitter {
} }
backoff = std::max(backoff, minBackoff_); backoff = std::max(backoff, minBackoff_);
co_await folly::coro::sleep(backoff); co_await folly::coro::sleep(backoff, timeKeeper_);
// Check to see if we were cancelled during the sleep. // Check to see if we were cancelled during the sleep.
const auto& cancelToken = co_await co_current_cancellation_token; const auto& cancelToken = co_await co_current_cancellation_token;
...@@ -170,6 +172,7 @@ class ExponentialBackoffWithJitter { ...@@ -170,6 +172,7 @@ class ExponentialBackoffWithJitter {
} }
private: private:
Timekeeper* timeKeeper_;
const uint32_t maxRetries_; const uint32_t maxRetries_;
uint32_t retryCount_; uint32_t retryCount_;
const Duration minBackoff_; const Duration minBackoff_;
...@@ -190,11 +193,13 @@ auto retryWithExponentialBackoff( ...@@ -190,11 +193,13 @@ auto retryWithExponentialBackoff(
Duration minBackoff, Duration minBackoff,
Duration maxBackoff, Duration maxBackoff,
double relativeJitterStdDev, double relativeJitterStdDev,
Timekeeper* timeKeeper,
URNG&& rng, URNG&& rng,
Func&& func) { Func&& func) {
return folly::coro::retryWhen( return folly::coro::retryWhen(
static_cast<Func&&>(func), static_cast<Func&&>(func),
detail::ExponentialBackoffWithJitter<remove_cvref_t<URNG>>{ detail::ExponentialBackoffWithJitter<remove_cvref_t<URNG>>{
timeKeeper,
maxRetries, maxRetries,
minBackoff, minBackoff,
maxBackoff, maxBackoff,
...@@ -208,14 +213,32 @@ auto retryWithExponentialBackoff( ...@@ -208,14 +213,32 @@ auto retryWithExponentialBackoff(
Duration minBackoff, Duration minBackoff,
Duration maxBackoff, Duration maxBackoff,
double relativeJitterStdDev, double relativeJitterStdDev,
Timekeeper* timeKeeper,
Func&& func) { Func&& func) {
return folly::coro::retryWithExponentialBackoff( return folly::coro::retryWithExponentialBackoff(
maxRetries, maxRetries,
minBackoff, minBackoff,
maxBackoff, maxBackoff,
relativeJitterStdDev, relativeJitterStdDev,
timeKeeper,
ThreadLocalPRNG(), ThreadLocalPRNG(),
static_cast<Func&&>(func)); static_cast<Func&&>(func));
} }
template <typename Func>
auto retryWithExponentialBackoff(
uint32_t maxRetries,
Duration minBackoff,
Duration maxBackoff,
double relativeJitterStdDev,
Func&& func) {
return folly::coro::retryWithExponentialBackoff(
maxRetries,
minBackoff,
maxBackoff,
relativeJitterStdDev,
static_cast<Timekeeper*>(nullptr),
static_cast<Func&&>(func));
}
} // namespace folly::coro } // namespace folly::coro
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment