rippled
Loading...
Searching...
No Matches
Spawn.h
1#pragma once
2
3#include <xrpl/basics/Log.h>
4
5#include <boost/asio/spawn.hpp>
6#include <boost/asio/strand.hpp>
7
8#include <concepts>
9#include <type_traits>
10
11namespace xrpl::util {
12namespace impl {
13
14template <typename T>
15concept IsStrand = std::same_as<std::decay_t<T>, boost::asio::strand<typename std::decay_t<T>::inner_executor_type>>;
16
27inline constexpr auto kPROPAGATE_EXCEPTIONS = [](std::exception_ptr ePtr) {
28 if (ePtr)
29 {
30 try
31 {
33 }
34 catch (std::exception const& e)
35 {
36 JLOG(debugLog().warn()) << "Spawn exception: " << e.what();
37 throw;
38 }
39 catch (...)
40 {
41 JLOG(debugLog().warn()) << "Spawn exception: Unknown";
42 throw;
43 }
44 }
45};
46
47} // namespace impl
48
62template <typename Ctx, typename F>
64void
65spawn(Ctx&& ctx, F&& func)
66{
67 if constexpr (impl::IsStrand<Ctx>)
68 {
69 boost::asio::spawn(std::forward<Ctx>(ctx), std::forward<F>(func), impl::kPROPAGATE_EXCEPTIONS);
70 }
71 else
72 {
73 boost::asio::spawn(
74 boost::asio::make_strand(boost::asio::get_associated_executor(std::forward<Ctx>(ctx))),
75 std::forward<F>(func),
77 }
78}
79
80} // namespace xrpl::util
T is_same_v
constexpr auto kPROPAGATE_EXCEPTIONS
A completion handler that restores boost::asio::spawn's behaviour from Boost 1.83.
Definition Spawn.h:27
void spawn(Ctx &&ctx, F &&func)
Spawns a coroutine using boost::asio::spawn
Definition Spawn.h:65
beast::Journal debugLog()
Returns a debug journal.
Definition Log.cpp:445
T rethrow_exception(T... args)
T what(T... args)