diff options
author | osado <osado@yandex-team.ru> | 2022-02-10 16:49:18 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:49:18 +0300 |
commit | 58cd0b86ed99a72df22479e26a20bc1c1e57e65e (patch) | |
tree | 5d5cb817648f650d76cf1076100726fd9b8448e8 /library/cpp/retry/retry.h | |
parent | 3ed175181ceac225ee14e4519492ad2967a7bd73 (diff) | |
download | ydb-58cd0b86ed99a72df22479e26a20bc1c1e57e65e.tar.gz |
Restoring authorship annotation for <osado@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'library/cpp/retry/retry.h')
-rw-r--r-- | library/cpp/retry/retry.h | 104 |
1 files changed, 52 insertions, 52 deletions
diff --git a/library/cpp/retry/retry.h b/library/cpp/retry/retry.h index 7c1566de60..c47ff5070f 100644 --- a/library/cpp/retry/retry.h +++ b/library/cpp/retry/retry.h @@ -1,7 +1,7 @@ #pragma once -#include "utils.h" - +#include "utils.h" + #include <library/cpp/retry/protos/retry_options.pb.h> #include <util/datetime/base.h> @@ -10,11 +10,11 @@ #include <util/generic/yexception.h> #include <functional> -struct TRetryOptions { - ui32 RetryCount; +struct TRetryOptions { + ui32 RetryCount; // TotalDuration = SleepDuration +/- SleepRandomDelta + (attempt * SleepIncrement) + (2**attempt * SleepExponentialMultiplier) - TDuration SleepDuration; + TDuration SleepDuration; TDuration SleepRandomDelta; TDuration SleepIncrement; TDuration SleepExponentialMultiplier; @@ -24,35 +24,35 @@ struct TRetryOptions { TRetryOptions(ui32 retryCount = 3, TDuration sleepDuration = TDuration::Seconds(1), TDuration sleepRandomDelta = TDuration::Zero(), TDuration sleepIncrement = TDuration::Zero(), TDuration sleepExponentialMultiplier = TDuration::Zero(), std::function<void(TDuration)> sleepFunction = [](TDuration d) { Sleep(d); }) // can't use Sleep itself due to Win compilation error - : RetryCount(retryCount) - , SleepDuration(sleepDuration) - , SleepRandomDelta(sleepRandomDelta) + : RetryCount(retryCount) + , SleepDuration(sleepDuration) + , SleepRandomDelta(sleepRandomDelta) , SleepIncrement(sleepIncrement) , SleepExponentialMultiplier(sleepExponentialMultiplier) , SleepFunction(sleepFunction) - { - } + { + } TRetryOptions& WithCount(ui32 retryCount) { - RetryCount = retryCount; - return *this; - } - - TRetryOptions& WithSleep(TDuration sleepDuration) { - SleepDuration = sleepDuration; - return *this; - } - - TRetryOptions& WithRandomDelta(TDuration sleepRandomDelta) { - SleepRandomDelta = sleepRandomDelta; - return *this; - } - + RetryCount = retryCount; + return *this; + } + + TRetryOptions& WithSleep(TDuration sleepDuration) { + SleepDuration = sleepDuration; + return *this; + } + + TRetryOptions& WithRandomDelta(TDuration sleepRandomDelta) { + SleepRandomDelta = sleepRandomDelta; + return *this; + } + TRetryOptions& WithIncrement(TDuration sleepIncrement) { SleepIncrement = sleepIncrement; return *this; - } - + } + TRetryOptions& WithExponentialMultiplier(TDuration sleepExponentialMultiplier) { SleepExponentialMultiplier = sleepExponentialMultiplier; return *this; @@ -68,45 +68,45 @@ struct TRetryOptions { return SleepDuration + NRetryPrivate::AddRandomDelta(SleepRandomDelta) + NRetryPrivate::AddIncrement(attempt, SleepIncrement) + NRetryPrivate::AddExponentialMultiplier(attempt, SleepExponentialMultiplier); } - static TRetryOptions Count(ui32 retryCount) { - return TRetryOptions(retryCount); - } + static TRetryOptions Count(ui32 retryCount) { + return TRetryOptions(retryCount); + } - static TRetryOptions Default() { - return TRetryOptions(); + static TRetryOptions Default() { + return TRetryOptions(); } - static TRetryOptions NoRetry() { - return TRetryOptions(0); + static TRetryOptions NoRetry() { + return TRetryOptions(0); } -}; +}; -template <typename TResult, typename TException = yexception> +template <typename TResult, typename TException = yexception> TMaybe<TResult> DoWithRetry(std::function<TResult()> func, std::function<void(const TException&)> onFail, TRetryOptions retryOptions, bool throwLast) { - for (ui32 attempt = 0; attempt <= retryOptions.RetryCount; ++attempt) { - try { - return func(); + for (ui32 attempt = 0; attempt <= retryOptions.RetryCount; ++attempt) { + try { + return func(); } catch (TException& ex) { onFail(ex); - if (attempt == retryOptions.RetryCount) { - if (throwLast) { - throw; + if (attempt == retryOptions.RetryCount) { + if (throwLast) { + throw; } - } else { + } else { auto sleep = retryOptions.SleepFunction; sleep(retryOptions.GetTimeToSleep(attempt)); } } } - return Nothing(); -} + return Nothing(); +} template <typename TResult, typename TException = yexception> TMaybe<TResult> DoWithRetry(std::function<TResult()> func, TRetryOptions retryOptions, bool throwLast) { return DoWithRetry<TResult, TException>(func, [](const TException&){}, retryOptions, throwLast); } -template <typename TException = yexception> +template <typename TException = yexception> bool DoWithRetry(std::function<void()> func, std::function<void(const TException&)> onFail, TRetryOptions retryOptions, bool throwLast) { auto f = [&]() { func(); @@ -116,15 +116,15 @@ bool DoWithRetry(std::function<void()> func, std::function<void(const TException } template <typename TException = yexception> -bool DoWithRetry(std::function<void()> func, TRetryOptions retryOptions, bool throwLast) { - auto f = [&]() { - func(); - return nullptr; - }; +bool DoWithRetry(std::function<void()> func, TRetryOptions retryOptions, bool throwLast) { + auto f = [&]() { + func(); + return nullptr; + }; return DoWithRetry<void*, TException>(f, [](const TException&){}, retryOptions, throwLast).Defined(); -} +} -void DoWithRetry(std::function<void()> func, TRetryOptions retryOptions); +void DoWithRetry(std::function<void()> func, TRetryOptions retryOptions); bool DoWithRetryOnRetCode(std::function<bool()> func, TRetryOptions retryOptions); |