aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/retry/retry.cpp
blob: 92466cdeca596ca26545eb1271ac93c9dd91fca3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include "retry.h"

#include <util/stream/output.h>

void DoWithRetry(std::function<void()> func, TRetryOptions retryOptions) {
    DoWithRetry(func, retryOptions, true);
}

bool DoWithRetryOnRetCode(std::function<bool()> func, TRetryOptions retryOptions) {
    for (ui32 attempt = 0; attempt <= retryOptions.RetryCount; ++attempt) {
        if (func()) {
            return true;
        }
        auto sleep = retryOptions.SleepFunction;
        sleep(retryOptions.GetTimeToSleep(attempt));
    }
    return false;
}

TRetryOptions MakeRetryOptions(const NRetry::TRetryOptionsPB& retryOptions) {
    return TRetryOptions(retryOptions.GetMaxTries(),
                         TDuration::MilliSeconds(retryOptions.GetInitialSleepMs()),
                         TDuration::MilliSeconds(retryOptions.GetRandomDeltaMs()),
                         TDuration::MilliSeconds(retryOptions.GetSleepIncrementMs()),
                         TDuration::MilliSeconds(retryOptions.GetExponentalMultiplierMs()));
}