aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/retry/retry.cpp
blob: eb589832eb1ec527dc04db920f0da6da5b3281a4 (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()));
}