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 {
public:
template <typename URNG2>
explicit ExponentialBackoffWithJitter(
Timekeeper* tk,
uint32_t maxRetries,
Duration minBackoff,
Duration maxBackoff,
double relativeJitterStdDev,
URNG2&& rng) noexcept
: maxRetries_(maxRetries),
: timeKeeper_(tk),
maxRetries_(maxRetries),
retryCount_(0),
minBackoff_(minBackoff),
maxBackoff_(maxBackoff),
......@@ -160,7 +162,7 @@ class ExponentialBackoffWithJitter {
}
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.
const auto& cancelToken = co_await co_current_cancellation_token;
......@@ -170,6 +172,7 @@ class ExponentialBackoffWithJitter {
}
private:
Timekeeper* timeKeeper_;
const uint32_t maxRetries_;
uint32_t retryCount_;
const Duration minBackoff_;
......@@ -190,11 +193,13 @@ auto retryWithExponentialBackoff(
Duration minBackoff,
Duration maxBackoff,
double relativeJitterStdDev,
Timekeeper* timeKeeper,
URNG&& rng,
Func&& func) {
return folly::coro::retryWhen(
static_cast<Func&&>(func),
detail::ExponentialBackoffWithJitter<remove_cvref_t<URNG>>{
timeKeeper,
maxRetries,
minBackoff,
maxBackoff,
......@@ -208,14 +213,32 @@ auto retryWithExponentialBackoff(
Duration minBackoff,
Duration maxBackoff,
double relativeJitterStdDev,
Timekeeper* timeKeeper,
Func&& func) {
return folly::coro::retryWithExponentialBackoff(
maxRetries,
minBackoff,
maxBackoff,
relativeJitterStdDev,
timeKeeper,
ThreadLocalPRNG(),
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
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