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
44class Repeat {
45 struct Control {
46 boost::asio::steady_timer timer;
47 boost::asio::strand<boost::asio::any_io_executor> strand;
48 std::atomic_bool stopping{true};
49 std::binary_semaphore semaphore{0};
50
51 Control(auto& ctx) : timer(ctx), strand(boost::asio::make_strand(ctx))
52 {
53 }
54 };
55
56 std::shared_ptr<Control> control_;
57
58public:
66 Repeat(auto& ctx) : control_(std::make_unique<Control>(ctx))
67 {
68 }
69
70 Repeat(Repeat const&) = delete;
71 Repeat&
72 operator=(Repeat const&) = delete;
73 Repeat(Repeat&&) = default;
74 Repeat&
75 operator=(Repeat&&) = default;
76
82 void
83 stop();
84
93 template <std::invocable Action>
94 void
95 start(std::chrono::steady_clock::duration interval, Action&& action)
96 {
97 ASSERT(control_->stopping, "Should be stopped before starting");
98 control_->stopping = false;
99 startImpl(control_, interval, std::forward<Action>(action));
100 }
101
102private:
103 template <std::invocable Action>
104 static void
105 startImpl(
106 std::shared_ptr<Control> control,
107 std::chrono::steady_clock::duration interval,
108 Action&& action
109 )
110 {
111 boost::asio::post(
112 control->strand, [control, interval, action = std::forward<Action>(action)]() mutable {
113 if (control->stopping) {
114 control->semaphore.release();
115 return;
116 }
117
118 control->timer.expires_after(interval);
119 control->timer.async_wait(
120 [control,
121 interval,
122 action = std::forward<Action>(action)](auto const& ec) mutable {
123 if (ec or control->stopping) {
124 control->semaphore.release();
125 return;
126 }
127 action();
128
129 startImpl(std::move(control), interval, std::forward<Action>(action));
130 }
131 );
132 }
133 );
134 }
135};
136
137} // namespace util
A class to repeat some action at a regular interval.
Definition Repeat.hpp:44
void stop()
Stop repeating.
Definition Repeat.cpp:27
void start(std::chrono::steady_clock::duration interval, Action &&action)
Start asynchronously repeating.
Definition Repeat.hpp:95
Repeat(auto &ctx)
Construct a new Repeat object.
Definition Repeat.hpp:66
This namespace contains various utilities.
Definition AccountUtils.hpp:30