Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
Repeat.hpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of clio: https://github.com/XRPLF/clio
4 Copyright (c) 2024, 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
24#include <boost/asio/any_io_executor.hpp>
25#include <boost/asio/io_context.hpp>
26#include <boost/asio/post.hpp>
27#include <boost/asio/steady_timer.hpp>
28#include <boost/asio/strand.hpp>
29
30#include <atomic>
31#include <chrono>
32#include <concepts>
33#include <memory>
34#include <semaphore>
35#include <utility>
36
37namespace util {
38
43class Repeat {
44 struct Control {
45 boost::asio::steady_timer timer;
46 boost::asio::strand<boost::asio::any_io_executor> strand;
47 std::atomic_bool stopping{true};
48 std::binary_semaphore semaphore{0};
49
50 Control(auto& ctx) : timer(ctx), strand(boost::asio::make_strand(ctx))
51 {
52 }
53 };
54
55 std::shared_ptr<Control> control_;
56
57public:
64 Repeat(auto& ctx) : control_(std::make_unique<Control>(ctx))
65 {
66 }
67
68 Repeat(Repeat const&) = delete;
69 Repeat&
70 operator=(Repeat const&) = delete;
71 Repeat(Repeat&&) = default;
72 Repeat&
73 operator=(Repeat&&) = default;
74
79 void
80 stop();
81
90 template <std::invocable Action>
91 void
92 start(std::chrono::steady_clock::duration interval, Action&& action)
93 {
94 ASSERT(control_->stopping, "Should be stopped before starting");
95 control_->stopping = false;
96 startImpl(control_, interval, std::forward<Action>(action));
97 }
98
99private:
100 template <std::invocable Action>
101 static void
102 startImpl(std::shared_ptr<Control> control, std::chrono::steady_clock::duration interval, Action&& action)
103 {
104 boost::asio::post(control->strand, [control, interval, action = std::forward<Action>(action)]() mutable {
105 if (control->stopping) {
106 control->semaphore.release();
107 return;
108 }
109
110 control->timer.expires_after(interval);
111 control->timer.async_wait(
112 [control, interval, action = std::forward<Action>(action)](auto const& ec) mutable {
113 if (ec or control->stopping) {
114 control->semaphore.release();
115 return;
116 }
117 action();
118
119 startImpl(std::move(control), interval, std::forward<Action>(action));
120 }
121 );
122 });
123 }
124};
125
126} // namespace util
A class to repeat some action at a regular interval.
Definition Repeat.hpp:43
void stop()
Stop repeating.
Definition Repeat.cpp:27
void start(std::chrono::steady_clock::duration interval, Action &&action)
Start asynchronously repeating.
Definition Repeat.hpp:92
Repeat(auto &ctx)
Construct a new Repeat object.
Definition Repeat.hpp:64
This namespace contains various utilities.
Definition AccountUtils.hpp:30