retry.cpp 999 B

1234567891011121314151617181920212223242526
  1. #include "retry.h"
  2. #include <util/stream/output.h>
  3. void DoWithRetry(std::function<void()> func, TRetryOptions retryOptions) {
  4. DoWithRetry(func, retryOptions, true);
  5. }
  6. bool DoWithRetryOnRetCode(std::function<bool()> func, TRetryOptions retryOptions) {
  7. for (ui32 attempt = 0; attempt <= retryOptions.RetryCount; ++attempt) {
  8. if (func()) {
  9. return true;
  10. }
  11. auto sleep = retryOptions.SleepFunction;
  12. sleep(retryOptions.GetTimeToSleep(attempt));
  13. }
  14. return false;
  15. }
  16. TRetryOptions MakeRetryOptions(const NRetry::TRetryOptionsPB& retryOptions) {
  17. return TRetryOptions(retryOptions.GetMaxTries(),
  18. TDuration::MilliSeconds(retryOptions.GetInitialSleepMs()),
  19. TDuration::MilliSeconds(retryOptions.GetRandomDeltaMs()),
  20. TDuration::MilliSeconds(retryOptions.GetSleepIncrementMs()),
  21. TDuration::MilliSeconds(retryOptions.GetExponentalMultiplierMs()));
  22. }