Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
FallbackRecoveryTimer.hpp
1#pragma once
2
3#include "util/Mutex.hpp"
4
5#include <boost/asio/any_io_executor.hpp>
6#include <boost/asio/steady_timer.hpp>
7#include <boost/asio/thread_pool.hpp>
8#include <boost/system/error_code.hpp>
9
10#include <chrono>
11#include <memory>
12
13namespace cluster::impl {
14
25 struct Impl {
26 boost::asio::steady_timer timer;
27 bool isRunning = false;
28 std::chrono::steady_clock::duration recoveryTime;
29
30 Impl(
31 boost::asio::any_io_executor const& executor,
32 std::chrono::steady_clock::duration recoveryTime
33 )
34 : timer(executor), recoveryTime(recoveryTime)
35 {
36 }
37 };
38
39 std::shared_ptr<util::Mutex<Impl>> impl_;
40
41public:
49 boost::asio::thread_pool& ctx,
50 std::chrono::steady_clock::duration recoveryTime
51 );
52
56 [[nodiscard]] bool
57 isRunning() const;
58
69 template <typename Callback>
70 void
71 start(Callback&& callback)
72 {
73 auto locked = impl_->lock();
74 locked->isRunning = true;
75 locked->timer.expires_after(locked->recoveryTime);
76 locked->timer.async_wait([impl = impl_, cb = std::forward<Callback>(callback)](
77 boost::system::error_code ec
78 ) mutable {
79 impl->lock()->isRunning = false;
80 cb(ec);
81 });
82 }
83
87 void
88 cancel();
89};
90
91} // namespace cluster::impl
FallbackRecoveryTimer(boost::asio::thread_pool &ctx, std::chrono::steady_clock::duration recoveryTime)
Construct a timer bound to the given thread pool.
Definition FallbackRecoveryTimer.cpp:12
void cancel()
Cancel any pending async_wait and clear the running flag.
Definition FallbackRecoveryTimer.cpp:27
void start(Callback &&callback)
Arm the timer and schedule callback for when it fires.
Definition FallbackRecoveryTimer.hpp:71
bool isRunning() const
Returns true if an async_wait is currently pending.
Definition FallbackRecoveryTimer.cpp:21