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::
16 same_as<std::decay_t<T>, boost::asio::strand<typename std::decay_t<T>::inner_executor_type>>;
17
28inline constexpr auto kPROPAGATE_EXCEPTIONS = [](std::exception_ptr ePtr) {
29 if (ePtr)
30 {
31 try
32 {
34 }
35 catch (std::exception const& e)
36 {
37 JLOG(debugLog().warn()) << "Spawn exception: " << e.what();
38 throw;
39 }
40 catch (...)
41 {
42 JLOG(debugLog().warn()) << "Spawn exception: Unknown";
43 throw;
44 }
45 }
46};
47
48} // namespace impl
49
63template <typename Ctx, typename F>
65void
66spawn(Ctx&& ctx, F&& func)
67{
68 if constexpr (impl::IsStrand<Ctx>)
69 {
70 boost::asio::spawn(
72 }
73 else
74 {
75 boost::asio::spawn(
76 boost::asio::make_strand(boost::asio::get_associated_executor(std::forward<Ctx>(ctx))),
77 std::forward<F>(func),
79 }
80}
81
82} // 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:28
void spawn(Ctx &&ctx, F &&func)
Spawns a coroutine using boost::asio::spawn
Definition Spawn.h:66
beast::Journal debugLog()
Returns a debug journal.
Definition Log.cpp:453
T rethrow_exception(T... args)
T what(T... args)