Clio develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
Retry.hpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of clio: https://github.com/XRPLF/clio
4 Copyright (c) 2024, the clio developers.
5
6 Permission to use, copy, modify, and distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#pragma once
21
22#include <boost/asio/error.hpp>
23#include <boost/asio/io_context.hpp>
24#include <boost/asio/steady_timer.hpp>
25#include <boost/asio/strand.hpp>
26
27#include <atomic>
28#include <chrono>
29#include <cstddef>
30#include <memory>
31
32namespace util {
33
38 std::chrono::steady_clock::duration initialDelay_;
39 std::chrono::steady_clock::duration delay_;
40
41public:
47 RetryStrategy(std::chrono::steady_clock::duration delay);
48 virtual ~RetryStrategy() = default;
49
53 std::chrono::steady_clock::duration
54 getDelay() const;
55
59 void
61
65 void
66 reset();
67
68protected:
72 virtual std::chrono::steady_clock::duration
73 nextDelay() const = 0;
74};
75using RetryStrategyPtr = std::unique_ptr<RetryStrategy>;
76
80class Retry {
81 RetryStrategyPtr strategy_;
82 boost::asio::steady_timer timer_;
83 size_t attemptNumber_ = 0;
84 std::shared_ptr<std::atomic_bool> canceled_{std::make_shared<std::atomic_bool>(false)};
85
86public:
93 Retry(RetryStrategyPtr strategy, boost::asio::strand<boost::asio::io_context::executor_type> strand);
94
98 ~Retry();
99
106 template <typename Fn>
107 void
108 retry(Fn&& func)
109 {
110 *canceled_ = false;
111 timer_.expires_after(strategy_->getDelay());
112 strategy_->increaseDelay();
113 timer_.async_wait(
114 [this, canceled = canceled_, func = std::forward<Fn>(func)](boost::system::error_code const& ec) {
115 if (ec == boost::asio::error::operation_aborted or *canceled) {
116 return;
117 }
118 ++attemptNumber_;
119 func();
120 }
121 );
122 }
123
127 void
128 cancel();
129
133 size_t
134 attemptNumber() const;
135
139 std::chrono::steady_clock::duration
140 delayValue() const;
141
145 void
146 reset();
147};
148
153 std::chrono::steady_clock::duration maxDelay_;
154
155public:
162 ExponentialBackoffStrategy(std::chrono::steady_clock::duration delay, std::chrono::steady_clock::duration maxDelay);
163
164private:
165 std::chrono::steady_clock::duration
166 nextDelay() const override;
167};
168
177Retry
179 std::chrono::steady_clock::duration delay,
180 std::chrono::steady_clock::duration maxDelay,
181 boost::asio::strand<boost::asio::io_context::executor_type> strand
182);
183
184} // namespace util
A retry strategy that retries while exponentially increasing the delay between attempts.
Definition Retry.hpp:152
ExponentialBackoffStrategy(std::chrono::steady_clock::duration delay, std::chrono::steady_clock::duration maxDelay)
Construct a new Exponential Backoff Strategy object.
Definition Retry.cpp:91
Interface for retry strategies.
Definition Retry.hpp:37
std::chrono::steady_clock::duration getDelay() const
Definition Retry.cpp:38
RetryStrategy(std::chrono::steady_clock::duration delay)
Construct a new Retry Strategy object.
Definition Retry.cpp:33
void reset()
Reset the delay value.
Definition Retry.cpp:50
virtual std::chrono::steady_clock::duration nextDelay() const =0
void increaseDelay()
Increase the delay value.
Definition Retry.cpp:44
A retry mechanism.
Definition Retry.hpp:80
size_t attemptNumber() const
Definition Retry.cpp:73
Retry(RetryStrategyPtr strategy, boost::asio::strand< boost::asio::io_context::executor_type > strand)
Construct a new Retry object.
Definition Retry.cpp:55
std::chrono::steady_clock::duration delayValue() const
Definition Retry.cpp:79
~Retry()
Destroy the Retry object.
Definition Retry.cpp:60
void retry(Fn &&func)
Schedule a retry.
Definition Retry.hpp:108
void reset()
Reset the delay value and attempt number.
Definition Retry.cpp:85
void cancel()
Cancel scheduled retry if any.
Definition Retry.cpp:66
This namespace contains various utilities.
Definition AccountUtils.hpp:30
Retry makeRetryExponentialBackoff(std::chrono::steady_clock::duration delay, std::chrono::steady_clock::duration maxDelay, boost::asio::strand< boost::asio::io_context::executor_type > strand)
Create a retry mechanism with exponential backoff strategy.
Definition Retry.cpp:107