Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
RetryPolicy.hpp
1#pragma once
2
3#include "data/cassandra/Error.hpp"
4#include "data/cassandra/Types.hpp"
5#include "util/Retry.hpp"
6#include "util/log/Logger.hpp"
7
8#include <boost/asio.hpp>
9#include <boost/asio/io_context.hpp>
10#include <boost/asio/strand.hpp>
11
12#include <chrono>
13
14namespace data::cassandra::impl {
15
20 util::Logger log_{"Backend"};
21
22 util::Retry retry_;
23
24public:
28 ExponentialBackoffRetryPolicy(boost::asio::io_context& ioc)
29 : retry_(
30 util::makeRetryExponentialBackoff(
31 std::chrono::milliseconds(1),
32 std::chrono::seconds(1),
33 boost::asio::make_strand(ioc)
34 )
35 )
36 {
37 }
38
44 [[nodiscard]] bool
45 shouldRetry([[maybe_unused]] CassandraError err)
46 {
47 auto const delayMs =
48 std::chrono::duration_cast<std::chrono::milliseconds>(retry_.delayValue()).count();
49 LOG(log_.error()) << "Cassandra write error: " << err << ", current retries "
50 << retry_.attemptNumber() << ", retrying in " << delayMs
51 << " milliseconds";
52
53 return true; // keep retrying forever
54 }
55
61 template <typename Fn>
62 void
63 retry(Fn&& fn)
64 {
65 retry_.retry(std::forward<Fn>(fn));
66 }
67};
68
69} // namespace data::cassandra::impl
A simple container for both error message and error code.
Definition Error.hpp:15
bool shouldRetry(CassandraError err)
Computes next retry delay and returns true unconditionally.
Definition RetryPolicy.hpp:45
ExponentialBackoffRetryPolicy(boost::asio::io_context &ioc)
Create a new retry policy instance with the io_context provided.
Definition RetryPolicy.hpp:28
void retry(Fn &&fn)
Schedules next retry.
Definition RetryPolicy.hpp:63
A simple thread-safe logger for the channel specified in the constructor.
Definition Logger.hpp:77
A retry mechanism.
Definition Retry.hpp:61
This namespace contains various utilities.
Definition AccountUtils.hpp:11