xrpld
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 <exception>
10#include <type_traits>
11
12namespace xrpl::util {
13namespace impl {
14
15template <typename T>
16concept IsStrand = std::
17 same_as<std::decay_t<T>, boost::asio::strand<typename std::decay_t<T>::inner_executor_type>>;
18
29inline constexpr auto kPropagateExceptions = [](std::exception_ptr ePtr) {
30 if (ePtr)
31 {
32 try
33 {
35 }
36 catch (std::exception const& e)
37 {
38 JLOG(debugLog().warn()) << "Spawn exception: " << e.what();
39 throw;
40 }
41 catch (...)
42 {
43 JLOG(debugLog().warn()) << "Spawn exception: Unknown";
44 throw;
45 }
46 }
47};
48
49} // namespace impl
50
64template <typename Ctx, typename F>
66void
67spawn(Ctx&& ctx, F&& func)
68{
69 if constexpr (impl::IsStrand<Ctx>)
70 {
71 boost::asio::spawn(
73 }
74 else
75 {
76 boost::asio::spawn(
77 boost::asio::make_strand(boost::asio::get_associated_executor(std::forward<Ctx>(ctx))),
78 std::forward<F>(func),
80 }
81}
82
83} // namespace xrpl::util
T forward(T... args)
T is_invocable_r_v
constexpr auto kPropagateExceptions
A completion handler that restores boost::asio::spawn's behaviour from Boost 1.83.
Definition Spawn.h:29
void spawn(Ctx &&ctx, F &&func)
Spawns a coroutine using boost::asio::spawn.
Definition Spawn.h:67
beast::Journal debugLog()
Returns a debug journal.
Definition Log.cpp:399
T rethrow_exception(T... args)
T what(T... args)