20#ifndef ORES_NATS_SERVICE_RETRY_HPP
21#define ORES_NATS_SERVICE_RETRY_HPP
25#include <boost/asio/awaitable.hpp>
26#include <boost/system/system_error.hpp>
27#include <boost/asio/steady_timer.hpp>
28#include <boost/asio/this_coro.hpp>
29#include <boost/asio/use_awaitable.hpp>
30#include "ores.logging/make_logger.hpp"
32namespace ores::nats::service {
48boost::asio::awaitable<void>
49retry_with_backoff(Fn&& fn,
50 std::string_view operation_name,
51 std::chrono::milliseconds initial_delay = std::chrono::seconds(1),
52 std::chrono::milliseconds max_delay = std::chrono::seconds(30),
53 int max_retries = -1) {
56 static const std::string_view logger_name =
"ores.nats.service.retry";
57 static auto& lg = []() ->
auto& {
58 static auto instance = make_logger(logger_name);
62 auto delay = initial_delay;
63 for (
int attempt = 0; ; ++attempt) {
68 }
catch (
const boost::system::system_error&) {
70 }
catch (
const std::exception& e) {
71 if (max_retries >= 0 && attempt >= max_retries)
74 BOOST_LOG_SEV(lg, warn)
75 << operation_name <<
" failed (attempt " << (attempt + 1)
77 <<
". Retrying in " << delay.count() <<
" ms.";
83 const auto ex =
co_await boost::asio::this_coro::executor;
84 boost::asio::steady_timer timer(ex, delay);
85 co_await timer.async_wait(boost::asio::use_awaitable);
86 delay = std::min(delay * 2, max_delay);
Implements logging infrastructure for ORE Studio.
Definition boost_severity.hpp:28