Clio
develop
The XRP Ledger API server.
Theme:
Default
Round
Robot
Toggle main menu visibility
Loading...
Searching...
No Matches
Retry.hpp
1
#pragma once
2
3
#include <boost/asio/any_io_executor.hpp>
4
#include <boost/asio/error.hpp>
5
#include <boost/asio/io_context.hpp>
6
#include <boost/asio/spawn.hpp>
7
#include <boost/asio/steady_timer.hpp>
8
#include <boost/asio/strand.hpp>
9
10
#include <atomic>
11
#include <chrono>
12
#include <cstddef>
13
#include <memory>
14
15
namespace
util
{
16
20
class
RetryStrategy
{
21
std::chrono::steady_clock::duration initialDelay_;
22
std::chrono::steady_clock::duration delay_;
23
24
public
:
30
RetryStrategy
(std::chrono::steady_clock::duration delay);
31
virtual
~RetryStrategy
() =
default
;
32
36
[[nodiscard]] std::chrono::steady_clock::duration
37
getDelay
()
const
;
38
42
void
43
increaseDelay
();
44
48
void
49
reset
();
50
51
protected
:
55
[[nodiscard]]
virtual
std::chrono::steady_clock::duration
56
nextDelay
()
const
= 0;
57
};
58
using
RetryStrategyPtr = std::unique_ptr<RetryStrategy>;
59
63
class
Retry
{
64
RetryStrategyPtr strategy_;
65
boost::asio::steady_timer timer_;
66
size_t
attemptNumber_ = 0;
67
std::shared_ptr<std::atomic_bool> canceled_{std::make_shared<std::atomic_bool>(
false
)};
68
69
public
:
75
struct
Delays
{
76
std::chrono::steady_clock::duration initial;
77
std::chrono::steady_clock::duration max;
78
};
79
86
Retry
(
87
RetryStrategyPtr strategy,
88
boost::asio::strand<boost::asio::io_context::executor_type> strand
89
);
90
99
Retry
(RetryStrategyPtr strategy, boost::asio::any_io_executor executor);
100
104
~Retry
();
105
112
template
<
typename
Fn>
113
void
114
retry
(Fn&& func)
115
{
116
*canceled_ =
false
;
117
timer_.expires_after(strategy_->getDelay());
118
strategy_->increaseDelay();
119
timer_.async_wait([
this
,
120
canceled = canceled_,
121
func = std::forward<Fn>(func)](boost::system::error_code
const
& ec) {
122
if
(ec == boost::asio::error::operation_aborted or *canceled) {
123
return
;
124
}
125
++attemptNumber_;
126
func();
127
});
128
}
129
138
void
139
wait
(boost::asio::yield_context yield);
140
144
void
145
cancel
();
146
150
[[nodiscard]]
size_t
151
attemptNumber
()
const
;
152
156
[[nodiscard]] std::chrono::steady_clock::duration
157
delayValue
()
const
;
158
162
void
163
reset
();
164
};
165
169
class
ExponentialBackoffStrategy
:
public
RetryStrategy
{
170
std::chrono::steady_clock::duration maxDelay_;
171
172
public
:
178
explicit
ExponentialBackoffStrategy
(
Retry::Delays
delays);
179
180
private
:
181
[[nodiscard]] std::chrono::steady_clock::duration
182
nextDelay
()
const override
;
183
};
184
192
Retry
193
makeRetryExponentialBackoff
(
194
Retry::Delays
delays,
195
boost::asio::strand<boost::asio::io_context::executor_type> strand
196
);
197
205
Retry
206
makeRetryExponentialBackoff
(
Retry::Delays
delays, boost::asio::any_io_executor executor);
207
208
}
// namespace util
util::ExponentialBackoffStrategy::ExponentialBackoffStrategy
ExponentialBackoffStrategy(Retry::Delays delays)
Construct a new Exponential Backoff Strategy object.
Definition
Retry.cpp:86
util::RetryStrategy::getDelay
std::chrono::steady_clock::duration getDelay() const
Definition
Retry.cpp:25
util::RetryStrategy::RetryStrategy
RetryStrategy(std::chrono::steady_clock::duration delay)
Construct a new Retry Strategy object.
Definition
Retry.cpp:19
util::RetryStrategy::reset
void reset()
Reset the delay value.
Definition
Retry.cpp:37
util::RetryStrategy::nextDelay
virtual std::chrono::steady_clock::duration nextDelay() const =0
util::RetryStrategy::increaseDelay
void increaseDelay()
Increase the delay value.
Definition
Retry.cpp:31
util::Retry
A retry mechanism.
Definition
Retry.hpp:63
util::Retry::attemptNumber
size_t attemptNumber() const
Definition
Retry.cpp:68
util::Retry::Retry
Retry(RetryStrategyPtr strategy, boost::asio::strand< boost::asio::io_context::executor_type > strand)
Construct a new Retry object.
Definition
Retry.cpp:42
util::Retry::delayValue
std::chrono::steady_clock::duration delayValue() const
Definition
Retry.cpp:74
util::Retry::~Retry
~Retry()
Destroy the Retry object.
Definition
Retry.cpp:55
util::Retry::retry
void retry(Fn &&func)
Schedule a retry.
Definition
Retry.hpp:114
util::Retry::reset
void reset()
Reset the delay value and attempt number.
Definition
Retry.cpp:80
util::Retry::wait
void wait(boost::asio::yield_context yield)
Wait out the current delay by suspending the calling coroutine, then back off.
Definition
Retry.cpp:99
util::Retry::cancel
void cancel()
Cancel scheduled retry if any.
Definition
Retry.cpp:61
util
This namespace contains various utilities.
Definition
HandlerRegistry.hpp:24
util::makeRetryExponentialBackoff
Retry makeRetryExponentialBackoff(Retry::Delays delays, boost::asio::strand< boost::asio::io_context::executor_type > strand)
Create a retry mechanism with exponential backoff strategy.
Definition
Retry.cpp:120
util::Retry::Delays
The delays to use between retry attempts.
Definition
Retry.hpp:75
src
util
Retry.hpp
Generated by
1.17.0