Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
RepeatedTask.hpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of clio: https://github.com/XRPLF/clio
4 Copyright (c) 2025, 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 "util/Assert.hpp"
23#include "util/Spawn.hpp"
24
25#include <boost/asio/bind_cancellation_slot.hpp>
26#include <boost/asio/cancellation_signal.hpp>
27#include <boost/asio/cancellation_type.hpp>
28#include <boost/asio/error.hpp>
29#include <boost/asio/executor.hpp>
30#include <boost/asio/spawn.hpp>
31#include <boost/asio/steady_timer.hpp>
32#include <boost/asio/strand.hpp>
33#include <boost/asio/use_future.hpp>
34
35#include <atomic>
36#include <chrono>
37#include <concepts>
38#include <semaphore>
39
40namespace cluster::impl {
41
42// TODO: Try to replace util::Repeat by this. https://github.com/XRPLF/clio/issues/2926
43template <typename Context>
44class RepeatedTask {
45 std::chrono::steady_clock::duration interval_;
46 boost::asio::strand<typename Context::executor_type> strand_;
47
48 enum class State { Running, Stopped };
49 std::atomic<State> state_ = State::Stopped;
50
51 std::binary_semaphore semaphore_{0};
52 boost::asio::steady_timer timer_;
53
54public:
55 RepeatedTask(std::chrono::steady_clock::duration interval, Context& ctx)
56 : interval_(interval), strand_(boost::asio::make_strand(ctx)), timer_(strand_)
57 {
58 }
59
60 ~RepeatedTask()
61 {
62 stop();
63 }
64
65 template <typename Fn>
66 requires std::invocable<Fn, boost::asio::yield_context> or std::invocable<Fn>
67 void
68 run(Fn&& f)
69 {
70 ASSERT(state_ == State::Stopped, "Can only be ran once");
71 state_ = State::Running;
72 util::spawn(strand_, [this, f = std::forward<Fn>(f)](boost::asio::yield_context yield) {
73 boost::system::error_code ec;
74
75 while (state_ == State::Running) {
76 timer_.expires_after(interval_);
77 timer_.async_wait(yield[ec]);
78
79 if (ec or state_ != State::Running)
80 break;
81
82 if constexpr (std::invocable<decltype(f), boost::asio::yield_context>) {
83 f(yield);
84 } else {
85 f();
86 }
87 }
88
89 semaphore_.release();
90 });
91 }
92
93 void
94 stop()
95 {
96 if (auto expected = State::Running; not state_.compare_exchange_strong(expected, State::Stopped))
97 return; // Already stopped or not started
98
99 boost::asio::spawn(strand_, [this](auto&&) { timer_.cancel(); }, boost::asio::use_future).wait();
100 semaphore_.acquire();
101 }
102};
103
104} // namespace cluster::impl
void spawn(Ctx &&ctx, F &&func)
Spawns a coroutine using boost::asio::spawn.
Definition Spawn.hpp:70