Skip to content

Commit

Permalink
Switch from epee http server to boost::beast http server. Min boost 1.70
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
vtnerd committed Oct 3, 2024
1 parent 9868fd9 commit d060f0c
Show file tree
Hide file tree
Showing 13 changed files with 1,475 additions and 395 deletions.
3 changes: 3 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ include_directories(.)
add_subdirectory(lmdb)
add_subdirectory(wire)
add_subdirectory(db)
add_subdirectory(net)
add_subdirectory(rpc)
add_subdirectory(util)

Expand All @@ -49,12 +50,14 @@ target_link_libraries(monero-lws-daemon-common
${MONERO_lmdb}
monero-lws-common
monero-lws-db
monero-lws-net
monero-lws-rpc
monero-lws-rpc-scanner
monero-lws-wire-json
monero-lws-util
${Boost_CHRONO_LIBRARY}
${Boost_PROGRAM_OPTIONS_LIBRARY}
${Boost_SYSTEM_LIBRARY}
${Boost_THREAD_LIBRARY}
${Boost_THREAD_LIBS_INIT}
${EXTRA_LIBRARIES}
Expand Down
33 changes: 33 additions & 0 deletions src/net/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
105 changes: 105 additions & 0 deletions src/net/zmq_async.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// 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 changes: 165 additions & 0 deletions src/net/zmq_async.h
Original file line number Diff line number Diff line change
@@ -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

Loading

0 comments on commit d060f0c

Please sign in to comment.