rippled
Loading...
Searching...
No Matches
Spawn.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright(c) 2025 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or 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#ifndef RIPPLE_SERVER_SPAWN_H_INCLUDED
21#define RIPPLE_SERVER_SPAWN_H_INCLUDED
22
23#include <xrpl/basics/Log.h>
24
25#include <boost/asio/spawn.hpp>
26#include <boost/asio/strand.hpp>
27
28#include <concepts>
29#include <type_traits>
30
31namespace ripple::util {
32namespace impl {
33
34template <typename T>
37 boost::asio::strand<typename std::decay_t<T>::inner_executor_type>>;
38
49inline constexpr auto kPROPAGATE_EXCEPTIONS = [](std::exception_ptr ePtr) {
50 if (ePtr)
51 {
52 try
53 {
55 }
56 catch (std::exception const& e)
57 {
58 JLOG(debugLog().warn()) << "Spawn exception: " << e.what();
59 throw;
60 }
61 catch (...)
62 {
63 JLOG(debugLog().warn()) << "Spawn exception: Unknown";
64 throw;
65 }
66 }
67};
68
69} // namespace impl
70
84template <typename Ctx, typename F>
86void
87spawn(Ctx&& ctx, F&& func)
88{
89 if constexpr (impl::IsStrand<Ctx>)
90 {
91 boost::asio::spawn(
93 std::forward<F>(func),
95 }
96 else
97 {
98 boost::asio::spawn(
99 boost::asio::make_strand(
100 boost::asio::get_associated_executor(std::forward<Ctx>(ctx))),
101 std::forward<F>(func),
103 }
104}
105
106} // namespace ripple::util
107
108#endif
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:49
void spawn(Ctx &&ctx, F &&func)
Spawns a coroutine using boost::asio::spawn
Definition Spawn.h:87
beast::Journal debugLog()
Returns a debug journal.
Definition Log.cpp:468
T rethrow_exception(T... args)
T what(T... args)