aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/retry/retry.cpp
diff options
context:
space:
mode:
authorarcadia-devtools <arcadia-devtools@yandex-team.ru>2022-02-21 21:20:40 +0300
committerarcadia-devtools <arcadia-devtools@yandex-team.ru>2022-02-21 21:20:40 +0300
commit06a4f28af33f3cef25b5746b6a2f8b85b1d4f76c (patch)
treef5373e49b99fd5c559814da2fc873c0872c1647c /library/cpp/retry/retry.cpp
parent4bc75f9614e93bb89dd7c077468df9a50c409d12 (diff)
downloadydb-06a4f28af33f3cef25b5746b6a2f8b85b1d4f76c.tar.gz
intermediate changes
ref:f9eefd8eed3595f41a195fc7080bbf602b0075af
Diffstat (limited to 'library/cpp/retry/retry.cpp')
-rw-r--r--library/cpp/retry/retry.cpp45
1 files changed, 35 insertions, 10 deletions
diff --git a/library/cpp/retry/retry.cpp b/library/cpp/retry/retry.cpp
index 92466cdeca5..f41c97e1794 100644
--- a/library/cpp/retry/retry.cpp
+++ b/library/cpp/retry/retry.cpp
@@ -2,19 +2,44 @@
#include <util/stream/output.h>
-void DoWithRetry(std::function<void()> func, TRetryOptions retryOptions) {
- DoWithRetry(func, retryOptions, true);
-}
+namespace {
+class TRetryOptionsWithRetCodePolicy : public IRetryPolicy<bool> {
+public:
+ explicit TRetryOptionsWithRetCodePolicy(const TRetryOptions& opts)
+ : Opts(opts)
+ {
+ }
-bool DoWithRetryOnRetCode(std::function<bool()> func, TRetryOptions retryOptions) {
- for (ui32 attempt = 0; attempt <= retryOptions.RetryCount; ++attempt) {
- if (func()) {
- return true;
+ class TRetryState : public IRetryState {
+ public:
+ explicit TRetryState(const TRetryOptions& opts)
+ : Opts(opts)
+ {
+ }
+
+ TMaybe<TDuration> GetNextRetryDelay(bool ret) override {
+ if (ret || Attempt == Opts.RetryCount) {
+ return Nothing();
+ }
+ return Opts.GetTimeToSleep(Attempt++);
}
- auto sleep = retryOptions.SleepFunction;
- sleep(retryOptions.GetTimeToSleep(attempt));
+
+ private:
+ const TRetryOptions Opts;
+ size_t Attempt = 0;
+ };
+
+ IRetryState::TPtr CreateRetryState() const override {
+ return std::make_unique<TRetryState>(Opts);
}
- return false;
+
+private:
+ const TRetryOptions Opts;
+};
+} // namespace
+
+bool DoWithRetryOnRetCode(std::function<bool()> func, TRetryOptions retryOptions) {
+ return DoWithRetryOnRetCode<bool>(std::move(func), std::make_shared<TRetryOptionsWithRetCodePolicy>(retryOptions), retryOptions.SleepFunction);
}
TRetryOptions MakeRetryOptions(const NRetry::TRetryOptionsPB& retryOptions) {