3#include <xrpl/basics/Log.h>
4#include <xrpl/basics/contract.h>
5#include <xrpl/beast/utility/Journal.h>
6#include <xrpl/server/Port.h>
7#include <xrpl/server/detail/PlainHTTPPeer.h>
8#include <xrpl/server/detail/SSLHTTPPeer.h>
9#include <xrpl/server/detail/io_list.h>
11#include <boost/asio/basic_waitable_timer.hpp>
12#include <boost/asio/buffer.hpp>
13#include <boost/asio/io_context.hpp>
14#include <boost/asio/ip/tcp.hpp>
15#include <boost/asio/post.hpp>
16#include <boost/asio/spawn.hpp>
17#include <boost/asio/steady_timer.hpp>
18#include <boost/beast/core/detect_ssl.hpp>
19#include <boost/beast/core/multi_buffer.hpp>
20#include <boost/beast/core/tcp_stream.hpp>
21#include <boost/container/flat_map.hpp>
22#include <boost/predef.h>
28#include <sys/resource.h>
46template <
class Handler>
51 using TimerType = boost::asio::basic_waitable_timer<ClockType>;
66 boost::asio::io_context&
ioc_;
70 boost::asio::strand<boost::asio::io_context::executor_type>
strand_;
77 boost::asio::io_context& ioc,
94 boost::asio::io_context&
ioc_;
96 boost::asio::strand<boost::asio::io_context::executor_type>
strand_;
98 port_.protocol.contains(
"https") ||
port_.protocol.contains(
"wss") ||
99 port_.protocol.contains(
"wss2") ||
port_.protocol.contains(
"peer"),
102 port_.protocol.contains(
"http") ||
port_.protocol.contains(
"ws") ||
103 port_.protocol.contains(
"ws2"),
153 template <
class ConstBufferSequence>
157 ConstBufferSequence
const& buffers,
165template <
class Handler>
169 boost::asio::io_context& ioc,
184template <
class Handler>
192template <
class Handler>
199template <
class Handler>
203 boost::beast::multi_buffer buf(16);
205 boost::system::error_code ec;
206 bool const ssl = async_detect_ssl(
stream_, buf, doYield[ec]);
222 if (ec != boost::asio::error::operation_aborted)
224 JLOG(
j_.trace()) <<
"Error detecting ssl: " << ec.message() <<
" from " <<
remoteAddress_;
230template <
class Handler>
242 ss <<
"Can't close acceptor: " <<
port_.name <<
", " << ec.message();
243 JLOG(
j_.error()) << ss.
str();
250 acceptor_.open(localAddress.protocol(), ec);
253 JLOG(
j_.error()) <<
"Open port '" <<
port_.name <<
"' failed:" << ec.message();
257 acceptor_.set_option(boost::asio::ip::tcp::acceptor::reuse_address(
true), ec);
260 JLOG(
j_.error()) <<
"Option for port '" <<
port_.name <<
"' failed:" << ec.message();
267 JLOG(
j_.error()) <<
"Bind port '" <<
port_.name <<
"' failed:" << ec.message();
271 acceptor_.listen(boost::asio::socket_base::max_listen_connections, ec);
274 JLOG(
j_.error()) <<
"Listen on port '" <<
port_.name <<
"' failed:" << ec.message();
278 JLOG(
j_.info()) <<
"Opened " <<
port_;
281template <
class Handler>
284 boost::asio::io_context& ioContext,
299template <
class Handler>
307template <
class Handler>
311 if (!
strand_.running_in_this_thread())
322template <
class Handler>
323template <
class ConstBufferSequence>
327 ConstBufferSequence
const& buffers,
343template <
class Handler>
351 JLOG(
j_.warn()) <<
"Throttling do_accept for " <<
acceptDelay_.count() <<
"ms.";
353 boost::system::error_code tec;
363 acceptor_.async_accept(socket, remoteAddress, doYield[ec]);
366 if (ec == boost::asio::error::operation_aborted)
369 if (ec == boost::asio::error::no_descriptors ||
370 ec == boost::asio::error::no_buffer_space)
372 char const*
const cause = (ec == boost::asio::error::no_descriptors)
373 ?
"too many open files"
374 :
"kernel buffer space exhausted";
375 JLOG(
j_.warn()) <<
"accept: " << cause <<
". Pausing for " <<
acceptDelay_.count()
379 boost::system::error_code tec;
386 JLOG(
j_.error()) <<
"accept error: " << ec.message();
395 if (
auto const sp =
ios().
template emplace<Detector>(
401 create(
ssl_, boost::asio::null_buffers{}, std::move(stream), remoteAddress);
406template <
class Handler>
415 if (getrlimit(RLIMIT_NOFILE, &rl) != 0 || rl.rlim_cur == RLIM_INFINITY)
419 static constexpr char const* kFdDir =
"/proc/self/fd";
421 static constexpr char const* kFdDir =
"/dev/fd";
423 if (DIR* d = ::opendir(kFdDir))
426 while (::readdir(d) !=
nullptr)
430 s.
used = (cnt >= 3) ? (cnt - 3) : 0;
437template <
class Handler>
451 stats && stats->limit > 0 && stats->used * 100 > stats->limit *
kMaxUsedFdPercent;
A generic endpoint for log messages.
Detector(Port const &port, Handler &handler, boost::asio::io_context &ioc, StreamType &&stream, EndpointType remoteAddress, beast::Journal j)
void doDetect(YieldContext yield)
boost::asio::strand< boost::asio::io_context::executor_type > strand_
boost::asio::io_context & ioc_
EndpointType remoteAddress_
void create(bool ssl, ConstBufferSequence const &buffers, StreamType &&stream, EndpointType remoteAddress)
boost::asio::ip::tcp ProtocolType
boost::asio::ip::tcp::socket SocketType
boost::asio::io_context & ioc_
static constexpr std::chrono::milliseconds kFdSampleInterval
EndpointType getEndpoint() const
boost::asio::strand< boost::asio::io_context::executor_type > strand_
static constexpr std::uint64_t kMaxUsedFdPercent
boost::system::error_code ErrorCode
static constexpr std::chrono::milliseconds kMaxAcceptDelay
ProtocolType::endpoint EndpointType
std::chrono::milliseconds acceptDelay_
void close() override
Close the Door listening socket and connections.
void doAccept(YieldContext yield)
Door(Handler &handler, boost::asio::io_context &ioContext, Port const &port, beast::Journal j)
bool shouldThrottleForFds()
std::optional< FDStats > queryFdStats() const
boost::asio::basic_waitable_timer< ClockType > TimerType
boost::beast::tcp_stream StreamType
std::chrono::steady_clock ClockType
ProtocolType::acceptor AcceptorType
static constexpr std::chrono::milliseconds kInitialAcceptDelay
boost::asio::steady_timer backoffTimer_
ClockType::time_point fdSampleAt_
boost::asio::yield_context YieldContext
IOList & ios()
Return the IOList associated with the work.
void spawn(Ctx &&ctx, F &&func)
Spawns a coroutine using boost::asio::spawn.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
boost::beast::ssl_stream< SocketType > StreamType
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
T shared_from_this(T... args)
Configuration information for a Server listening port.