retry.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #pragma once
  2. #include "retry_policy.h"
  3. #include "utils.h"
  4. #include <library/cpp/retry/protos/retry_options.pb.h>
  5. #include <util/datetime/base.h>
  6. #include <util/generic/maybe.h>
  7. #include <util/generic/typetraits.h>
  8. #include <util/generic/yexception.h>
  9. #include <functional>
  10. struct TRetryOptions {
  11. ui32 RetryCount;
  12. // TotalDuration = SleepDuration +/- SleepRandomDelta + (attempt * SleepIncrement) + (2**attempt * SleepExponentialMultiplier)
  13. TDuration SleepDuration;
  14. TDuration SleepRandomDelta;
  15. TDuration SleepIncrement;
  16. TDuration SleepExponentialMultiplier;
  17. std::function<void(TDuration)> SleepFunction;
  18. TRetryOptions(ui32 retryCount = 3, TDuration sleepDuration = TDuration::Seconds(1), TDuration sleepRandomDelta = TDuration::Zero(),
  19. TDuration sleepIncrement = TDuration::Zero(), TDuration sleepExponentialMultiplier = TDuration::Zero(),
  20. std::function<void(TDuration)> sleepFunction = [](TDuration d) { Sleep(d); }) // can't use Sleep itself due to Win compilation error
  21. : RetryCount(retryCount)
  22. , SleepDuration(sleepDuration)
  23. , SleepRandomDelta(sleepRandomDelta)
  24. , SleepIncrement(sleepIncrement)
  25. , SleepExponentialMultiplier(sleepExponentialMultiplier)
  26. , SleepFunction(sleepFunction)
  27. {
  28. }
  29. TRetryOptions& WithCount(ui32 retryCount) {
  30. RetryCount = retryCount;
  31. return *this;
  32. }
  33. TRetryOptions& WithSleep(TDuration sleepDuration) {
  34. SleepDuration = sleepDuration;
  35. return *this;
  36. }
  37. TRetryOptions& WithRandomDelta(TDuration sleepRandomDelta) {
  38. SleepRandomDelta = sleepRandomDelta;
  39. return *this;
  40. }
  41. TRetryOptions& WithIncrement(TDuration sleepIncrement) {
  42. SleepIncrement = sleepIncrement;
  43. return *this;
  44. }
  45. TRetryOptions& WithExponentialMultiplier(TDuration sleepExponentialMultiplier) {
  46. SleepExponentialMultiplier = sleepExponentialMultiplier;
  47. return *this;
  48. }
  49. TRetryOptions& WithSleepFunction(std::function<void(TDuration)> sleepFunction) {
  50. SleepFunction = sleepFunction;
  51. return *this;
  52. }
  53. // for compatibility attempt == 0 by default
  54. TDuration GetTimeToSleep(ui32 attempt = 0) const {
  55. return SleepDuration + NRetryPrivate::AddRandomDelta(SleepRandomDelta) + NRetryPrivate::AddIncrement(attempt, SleepIncrement) + NRetryPrivate::AddExponentialMultiplier(attempt, SleepExponentialMultiplier);
  56. }
  57. static TRetryOptions Count(ui32 retryCount) {
  58. return TRetryOptions(retryCount);
  59. }
  60. static TRetryOptions Default() {
  61. return TRetryOptions();
  62. }
  63. static TRetryOptions NoRetry() {
  64. return TRetryOptions(0);
  65. }
  66. };
  67. TRetryOptions MakeRetryOptions(const NRetry::TRetryOptionsPB& retryOptions);
  68. namespace NRetryDetails {
  69. template <class TException>
  70. class TRetryOptionsPolicy : public IRetryPolicy<const TException&> {
  71. public:
  72. explicit TRetryOptionsPolicy(const TRetryOptions& opts)
  73. : Opts(opts)
  74. {
  75. }
  76. using IRetryState = typename IRetryPolicy<const TException&>::IRetryState;
  77. class TRetryState : public IRetryState {
  78. public:
  79. explicit TRetryState(const TRetryOptions& opts)
  80. : Opts(opts)
  81. {
  82. }
  83. TMaybe<TDuration> GetNextRetryDelay(const TException&) override {
  84. if (Attempt == Opts.RetryCount) {
  85. return Nothing();
  86. }
  87. return Opts.GetTimeToSleep(Attempt++);
  88. }
  89. private:
  90. const TRetryOptions Opts;
  91. size_t Attempt = 0;
  92. };
  93. typename IRetryState::TPtr CreateRetryState() const override {
  94. return std::make_unique<TRetryState>(Opts);
  95. }
  96. private:
  97. const TRetryOptions Opts;
  98. };
  99. } // namespace NRetryDetails
  100. template <class TException>
  101. typename IRetryPolicy<const TException&>::TPtr MakeRetryPolicy(const TRetryOptions& opts) {
  102. return std::make_shared<NRetryDetails::TRetryOptionsPolicy<TException>>(opts);
  103. }
  104. template <class TException>
  105. typename IRetryPolicy<const TException&>::TPtr MakeRetryPolicy(const NRetry::TRetryOptionsPB& opts) {
  106. return MakeRetryPolicy<TException>(MakeRetryOptions(opts));
  107. }
  108. template <typename TResult, typename TException = yexception>
  109. TMaybe<TResult> DoWithRetry(std::function<TResult()> func, const typename IRetryPolicy<const TException&>::TPtr& retryPolicy, bool throwLast = true, std::function<void(const TException&)> onFail = {}, std::function<void(TDuration)> sleepFunction = {}) {
  110. typename IRetryPolicy<const TException&>::IRetryState::TPtr retryState;
  111. while (true) {
  112. try {
  113. return func();
  114. } catch (const TException& ex) {
  115. if (onFail) {
  116. onFail(ex);
  117. }
  118. if (!retryState) {
  119. retryState = retryPolicy->CreateRetryState();
  120. }
  121. if (const TMaybe<TDuration> delay = retryState->GetNextRetryDelay(ex)) {
  122. if (*delay) {
  123. if (sleepFunction) {
  124. sleepFunction(*delay);
  125. } else {
  126. Sleep(*delay);
  127. }
  128. }
  129. } else {
  130. if (throwLast) {
  131. throw;
  132. }
  133. break;
  134. }
  135. }
  136. }
  137. return Nothing();
  138. }
  139. template <typename TResult, typename TException = yexception>
  140. TMaybe<TResult> DoWithRetry(std::function<TResult()> func, std::function<void(const TException&)> onFail, TRetryOptions retryOptions, bool throwLast = true) {
  141. return DoWithRetry<TResult, TException>(std::move(func), MakeRetryPolicy<TException>(retryOptions), throwLast, std::move(onFail), retryOptions.SleepFunction);
  142. }
  143. template <typename TResult, typename TException = yexception>
  144. TMaybe<TResult> DoWithRetry(std::function<TResult()> func, TRetryOptions retryOptions, bool throwLast = true) {
  145. return DoWithRetry<TResult, TException>(std::move(func), MakeRetryPolicy<TException>(retryOptions), throwLast, {}, retryOptions.SleepFunction);
  146. }
  147. template <typename TException = yexception>
  148. bool DoWithRetry(std::function<void()> func, const typename IRetryPolicy<const TException&>::TPtr& retryPolicy, bool throwLast = true, std::function<void(const TException&)> onFail = {}, std::function<void(TDuration)> sleepFunction = {}) {
  149. auto f = [&]() {
  150. func();
  151. return nullptr;
  152. };
  153. return DoWithRetry<void*, TException>(f, retryPolicy, throwLast, std::move(onFail), std::move(sleepFunction)).Defined();
  154. }
  155. template <typename TException = yexception>
  156. bool DoWithRetry(std::function<void()> func, std::function<void(const TException&)> onFail, TRetryOptions retryOptions, bool throwLast) {
  157. return DoWithRetry<TException>(std::move(func), MakeRetryPolicy<TException>(retryOptions), throwLast, onFail, retryOptions.SleepFunction);
  158. }
  159. template <typename TException = yexception>
  160. bool DoWithRetry(std::function<void()> func, TRetryOptions retryOptions, bool throwLast = true) {
  161. return DoWithRetry<TException>(std::move(func), MakeRetryPolicy<TException>(retryOptions), throwLast, {}, retryOptions.SleepFunction);
  162. }
  163. template <class TRetCode>
  164. TRetCode DoWithRetryOnRetCode(std::function<TRetCode()> func, const typename IRetryPolicy<TRetCode>::TPtr& retryPolicy, std::function<void(TDuration)> sleepFunction = {}) {
  165. auto retryState = retryPolicy->CreateRetryState();
  166. while (true) {
  167. TRetCode code = func();
  168. if (const TMaybe<TDuration> delay = retryState->GetNextRetryDelay(code)) {
  169. if (*delay) {
  170. if (sleepFunction) {
  171. sleepFunction(*delay);
  172. } else {
  173. Sleep(*delay);
  174. }
  175. }
  176. } else {
  177. return code;
  178. }
  179. }
  180. }
  181. bool DoWithRetryOnRetCode(std::function<bool()> func, TRetryOptions retryOptions);
  182. Y_DECLARE_PODTYPE(TRetryOptions);