forked from such-gitea/wownero-lws
Switch from epee http server to boost::beast http server. Min boost 1.70 (#136)
There is roughly a 7.4% increase in performance in the switch to boost::beast. Additionally, the REST endpoints `/daemon_status`, `/get_unspent_outs`, and `/submit_raw_tx` do not block in ZMQ calls, allowing for better response times regardless of `monerod` status. The REST endpoints `/login and `/get_random_outs` still need updates to prevent blocking (`/login` is conditional on DB state).
This commit is contained in:
committed by
Lee *!* Clagett
parent
8080159fc8
commit
075dc5d7c2
33
src/net/CMakeLists.txt
Normal file
33
src/net/CMakeLists.txt
Normal file
@@ -0,0 +1,33 @@
|
||||
# Copyright (c) 2024, The Monero Project
|
||||
#
|
||||
# All rights reserved.
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without modification, are
|
||||
# permitted provided that the following conditions are met:
|
||||
#
|
||||
# 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
# conditions and the following disclaimer.
|
||||
#
|
||||
# 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
# of conditions and the following disclaimer in the documentation and/or other
|
||||
# materials provided with the distribution.
|
||||
#
|
||||
# 3. Neither the name of the copyright holder nor the names of its contributors may be
|
||||
# used to endorse or promote products derived from this software without specific
|
||||
# prior written permission.
|
||||
#
|
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
|
||||
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
||||
# THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
||||
# THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
set(monero-lws-net_sources zmq_async.cpp)
|
||||
set(monero-lws-net_headers zmq_async.h)
|
||||
|
||||
add_library(monero-lws-net ${monero-lws-net_sources} ${monero-lws-net_headers})
|
||||
target_link_libraries(monero-lws-net monero::libraries)
|
||||
103
src/net/zmq_async.cpp
Normal file
103
src/net/zmq_async.cpp
Normal file
@@ -0,0 +1,103 @@
|
||||
// Copyright (c) 2024, The Monero Project
|
||||
// All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without modification, are
|
||||
// permitted provided that the following conditions are met:
|
||||
//
|
||||
// 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
// conditions and the following disclaimer.
|
||||
//
|
||||
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
// of conditions and the following disclaimer in the documentation and/or other
|
||||
// materials provided with the distribution.
|
||||
//
|
||||
// 3. Neither the name of the copyright holder nor the names of its contributors may be
|
||||
// used to endorse or promote products derived from this software without specific
|
||||
// prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
|
||||
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
||||
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
||||
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
#include "zmq_async.h"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
namespace net { namespace zmq
|
||||
{
|
||||
const boost::system::error_category& boost_error_category() noexcept
|
||||
{
|
||||
struct category final : boost::system::error_category
|
||||
{
|
||||
virtual const char* name() const noexcept override final
|
||||
{
|
||||
return "error::error_category()";
|
||||
}
|
||||
|
||||
virtual std::string message(int value) const override final
|
||||
{
|
||||
char const* const msg = zmq_strerror(value);
|
||||
if (msg)
|
||||
return msg;
|
||||
return "zmq_strerror failure";
|
||||
}
|
||||
|
||||
virtual boost::system::error_condition default_error_condition(int value) const noexcept override final
|
||||
{
|
||||
// maps specific errors to generic `std::errc` cases.
|
||||
switch (value)
|
||||
{
|
||||
case EFSM:
|
||||
case ETERM:
|
||||
break;
|
||||
default:
|
||||
/* zmq is using cerrno errors. C++ spec indicates that `std::errc`
|
||||
values must be identical to the cerrno value. So just map every zmq
|
||||
specific error to the generic errc equivalent. zmq extensions must
|
||||
be in the switch or they map to a non-existent errc enum value. */
|
||||
return boost::system::errc::errc_t(value);
|
||||
}
|
||||
return boost::system::error_condition{value, *this};
|
||||
}
|
||||
};
|
||||
static const category instance{};
|
||||
return instance;
|
||||
}
|
||||
|
||||
boost::system::error_code make_error_code(std::error_code code)
|
||||
{
|
||||
if (std::addressof(code.category()) != std::addressof(error_category()))
|
||||
throw std::logic_error{"Expected only ZMQ errors"};
|
||||
return boost::system::error_code{code.value(), boost_error_category()};
|
||||
}
|
||||
|
||||
void free_descriptor::operator()(adescriptor* ptr) noexcept
|
||||
{
|
||||
if (ptr)
|
||||
{
|
||||
ptr->release(); // release ASIO ownership, destroys all queued handlers
|
||||
delete ptr;
|
||||
}
|
||||
}
|
||||
|
||||
expect<async_client> async_client::make(boost::asio::io_service& io, socket zsock)
|
||||
{
|
||||
MONERO_PRECOND(zsock != nullptr);
|
||||
|
||||
int fd = 0;
|
||||
std::size_t length = sizeof(fd);
|
||||
if (zmq_getsockopt(zsock.get(), ZMQ_FD, &fd, &length) != 0)
|
||||
return net::zmq::get_error_code();
|
||||
|
||||
async_client out{std::move(zsock), nullptr, false};
|
||||
out.asock.reset(new adescriptor{io, fd});
|
||||
return out;
|
||||
}
|
||||
}} // net // zmq
|
||||
|
||||
165
src/net/zmq_async.h
Normal file
165
src/net/zmq_async.h
Normal file
@@ -0,0 +1,165 @@
|
||||
// Copyright (c) 2024, The Monero Project
|
||||
// All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without modification, are
|
||||
// permitted provided that the following conditions are met:
|
||||
//
|
||||
// 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
// conditions and the following disclaimer.
|
||||
//
|
||||
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
// of conditions and the following disclaimer in the documentation and/or other
|
||||
// materials provided with the distribution.
|
||||
//
|
||||
// 3. Neither the name of the copyright holder nor the names of its contributors may be
|
||||
// used to endorse or promote products derived from this software without specific
|
||||
// prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
|
||||
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
||||
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
||||
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <boost/asio/compose.hpp>
|
||||
#include <boost/asio/io_service.hpp>
|
||||
#include <boost/asio/posix/stream_descriptor.hpp>
|
||||
#include <boost/system/error_code.hpp>
|
||||
#include <cstddef>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <zmq.h>
|
||||
#include "byte_slice.h" // monero/contrib/epee/include
|
||||
#include "common/expect.h" // monero/src
|
||||
#include "net/zmq.h"
|
||||
|
||||
namespace net { namespace zmq
|
||||
{
|
||||
//! \return Category for ZMQ errors.
|
||||
const boost::system::error_category& boost_error_category() noexcept;
|
||||
|
||||
//! \return `code` iff the `::net::zmq` error category
|
||||
boost::system::error_code make_error_code(std::error_code code);
|
||||
|
||||
using adescriptor = boost::asio::posix::stream_descriptor;
|
||||
|
||||
struct free_descriptor
|
||||
{
|
||||
void operator()(adescriptor* ptr) noexcept;
|
||||
};
|
||||
|
||||
using asocket = std::unique_ptr<adescriptor, free_descriptor>;
|
||||
|
||||
struct async_client
|
||||
{
|
||||
async_client() = delete;
|
||||
socket zsock;
|
||||
asocket asock;
|
||||
bool close;
|
||||
|
||||
static expect<async_client> make(boost::asio::io_service& io, socket zsock);
|
||||
};
|
||||
|
||||
class read_msg_op
|
||||
{
|
||||
async_client* sock_;
|
||||
std::string* msg_;
|
||||
|
||||
public:
|
||||
read_msg_op(async_client& sock, std::string& msg)
|
||||
: sock_(std::addressof(sock)), msg_(std::addressof(msg))
|
||||
{}
|
||||
|
||||
template<typename F>
|
||||
void operator()(F& self, const boost::system::error_code error = {}, std::size_t = 0)
|
||||
{
|
||||
if (error)
|
||||
return self.complete(error, 0);
|
||||
if (!sock_)
|
||||
return;
|
||||
if (sock_->close)
|
||||
return self.complete(boost::asio::error::operation_aborted, 0);
|
||||
|
||||
assert(sock_->zsock && sock_->asock);
|
||||
expect<std::string> msg = receive(sock_->zsock.get(), ZMQ_DONTWAIT);
|
||||
if (!msg)
|
||||
{
|
||||
if (msg != make_error_code(EAGAIN))
|
||||
return self.complete(make_error_code(msg.error()), 0);
|
||||
|
||||
// try again
|
||||
sock_->asock->async_read_some(boost::asio::null_buffers(), std::move(self));
|
||||
return;
|
||||
}
|
||||
|
||||
*msg_ = std::move(*msg);
|
||||
self.complete(error, msg_->size());
|
||||
}
|
||||
};
|
||||
|
||||
class write_msg_op
|
||||
{
|
||||
async_client* sock_;
|
||||
epee::byte_slice msg_;
|
||||
|
||||
public:
|
||||
write_msg_op(async_client& sock, epee::byte_slice msg)
|
||||
: sock_(std::addressof(sock)), msg_(std::move(msg))
|
||||
{}
|
||||
|
||||
template<typename F>
|
||||
void operator()(F& self, const boost::system::error_code error = {}, std::size_t = 0)
|
||||
{
|
||||
if (error)
|
||||
return self.complete(error, 0);
|
||||
if (!sock_)
|
||||
return;
|
||||
if (sock_->close)
|
||||
return self.complete(boost::asio::error::operation_aborted, 0);
|
||||
|
||||
assert(sock_->zsock && sock_->asock);
|
||||
|
||||
expect<void> status =
|
||||
::net::zmq::send(msg_.clone(), sock_->zsock.get(), ZMQ_DONTWAIT);
|
||||
if (!status)
|
||||
{
|
||||
if (status != ::net::zmq::make_error_code(EAGAIN))
|
||||
return self.complete(make_error_code(status.error()), 0);
|
||||
|
||||
// try again
|
||||
sock_->asock->async_write_some(boost::asio::null_buffers(), std::move(self));
|
||||
return;
|
||||
}
|
||||
|
||||
self.complete(error, msg_.size());
|
||||
}
|
||||
};
|
||||
|
||||
//! Cannot have an `async_read` and `async_write` at same time (edge trigger)
|
||||
template<typename F>
|
||||
void async_read(async_client& sock, std::string& buffer, F&& f)
|
||||
{
|
||||
// async_compose is required for correct strand invocation, etc
|
||||
boost::asio::async_compose<F, void(boost::system::error_code, std::size_t)>(
|
||||
read_msg_op{sock, buffer}, f, *sock.asock
|
||||
);
|
||||
}
|
||||
|
||||
//! Cannot have an `async_write` and `async_read` at same time (edge trigger)
|
||||
template<typename F>
|
||||
void async_write(async_client& sock, epee::byte_slice msg, F&& f)
|
||||
{
|
||||
// async_compose is required for correct strand invocation, etc
|
||||
boost::asio::async_compose<F, void(boost::system::error_code, std::size_t)>(
|
||||
write_msg_op{sock, std::move(msg)}, f, *sock.asock
|
||||
);
|
||||
}
|
||||
|
||||
}} // net // zmq
|
||||
|
||||
Reference in New Issue
Block a user