Clio develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
RetryPolicy.hpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of clio: https://github.com/XRPLF/clio
4 Copyright (c) 2023, 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 "data/cassandra/Error.hpp"
23#include "data/cassandra/Types.hpp"
24#include "util/Retry.hpp"
25#include "util/log/Logger.hpp"
26
27#include <boost/asio.hpp>
28#include <boost/asio/io_context.hpp>
29#include <boost/asio/strand.hpp>
30
31#include <chrono>
32
33namespace data::cassandra::impl {
34
39 util::Logger log_{"Backend"};
40
41 util::Retry retry_;
42
43public:
47 ExponentialBackoffRetryPolicy(boost::asio::io_context& ioc)
48 : retry_(util::makeRetryExponentialBackoff(
49 std::chrono::milliseconds(1),
50 std::chrono::seconds(1),
51 boost::asio::make_strand(ioc)
52 ))
53 {
54 }
55
61 [[nodiscard]] bool
62 shouldRetry([[maybe_unused]] CassandraError err)
63 {
64 auto const delayMs = std::chrono::duration_cast<std::chrono::milliseconds>(retry_.delayValue()).count();
65 LOG(log_.error()) << "Cassandra write error: " << err << ", current retries " << retry_.attemptNumber()
66 << ", retrying in " << delayMs << " milliseconds";
67
68 return true; // keep retrying forever
69 }
70
76 template <typename Fn>
77 void
78 retry(Fn&& fn)
79 {
80 retry_.retry(std::forward<Fn>(fn));
81 }
82};
83
84} // namespace data::cassandra::impl
A simple container for both error message and error code.
Definition Error.hpp:34
A retry policy that employs exponential backoff.
Definition RetryPolicy.hpp:38
bool shouldRetry(CassandraError err)
Computes next retry delay and returns true unconditionally.
Definition RetryPolicy.hpp:62
ExponentialBackoffRetryPolicy(boost::asio::io_context &ioc)
Create a new retry policy instance with the io_context provided.
Definition RetryPolicy.hpp:47
void retry(Fn &&fn)
Schedules next retry.
Definition RetryPolicy.hpp:78
A simple thread-safe logger for the channel specified in the constructor.
Definition Logger.hpp:110
Pump error(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::ERR severity.
Definition Logger.cpp:215
A retry mechanism.
Definition Retry.hpp:80
size_t attemptNumber() const
Definition Retry.cpp:73
std::chrono::steady_clock::duration delayValue() const
Definition Retry.cpp:79
void retry(Fn &&func)
Schedule a retry.
Definition Retry.hpp:108
This namespace contains various utilities.
Definition AccountUtils.hpp:30