Upgrade Boost to 1.70, fix inefficient connection handling
A request to osrm-routed can be assigned to a thread which is currently busy processing another request, even when there are other threads/cores available. This unnecessarily delays the response, and can make requests appear to hang when awaiting CPU intensive requests to finish. The issue looks like a bug in Boost.Asio multithreaded networking stack. osrm-routed server implementation is heavily influenced by the HTTP server 3 example in the Boost.Asio docs. By upgrading to Boost 1.70 and updating the server connections to match the example provided in the 1.70 release, the problem is resolved. The diff of the changes to the Boost.Asio stack are vast, so it's difficult to identify the exact cause. However the implementation change is to push the strand of execution into the socket (and timer) objects, which suggests it could fix the type of threading issue we are observing.
This commit is contained in:
@@ -37,7 +37,7 @@ class RequestHandler;
|
||||
class Connection : public std::enable_shared_from_this<Connection>
|
||||
{
|
||||
public:
|
||||
explicit Connection(boost::asio::io_service &io_service, RequestHandler &handler);
|
||||
explicit Connection(boost::asio::io_context &io_context, RequestHandler &handler);
|
||||
Connection(const Connection &) = delete;
|
||||
Connection &operator=(const Connection &) = delete;
|
||||
|
||||
@@ -60,7 +60,7 @@ class Connection : public std::enable_shared_from_this<Connection>
|
||||
std::vector<char> compress_buffers(const std::vector<char> &uncompressed_data,
|
||||
const http::compression_type compression_type);
|
||||
|
||||
boost::asio::io_service::strand strand;
|
||||
boost::asio::strand<boost::asio::io_context::executor_type> strand;
|
||||
boost::asio::ip::tcp::socket TCP_socket;
|
||||
boost::asio::deadline_timer timer;
|
||||
RequestHandler &request_handler;
|
||||
|
||||
@@ -43,12 +43,12 @@ class Server
|
||||
}
|
||||
|
||||
explicit Server(const std::string &address, const int port, const unsigned thread_pool_size)
|
||||
: thread_pool_size(thread_pool_size), acceptor(io_service),
|
||||
new_connection(std::make_shared<Connection>(io_service, request_handler))
|
||||
: thread_pool_size(thread_pool_size), acceptor(io_context),
|
||||
new_connection(std::make_shared<Connection>(io_context, request_handler))
|
||||
{
|
||||
const auto port_string = std::to_string(port);
|
||||
|
||||
boost::asio::ip::tcp::resolver resolver(io_service);
|
||||
boost::asio::ip::tcp::resolver resolver(io_context);
|
||||
boost::asio::ip::tcp::resolver::query query(address, port_string);
|
||||
boost::asio::ip::tcp::endpoint endpoint = *resolver.resolve(query);
|
||||
|
||||
@@ -74,7 +74,7 @@ class Server
|
||||
for (unsigned i = 0; i < thread_pool_size; ++i)
|
||||
{
|
||||
std::shared_ptr<std::thread> thread = std::make_shared<std::thread>(
|
||||
boost::bind(&boost::asio::io_service::run, &io_service));
|
||||
boost::bind(&boost::asio::io_context::run, &io_context));
|
||||
threads.push_back(thread);
|
||||
}
|
||||
for (auto thread : threads)
|
||||
@@ -83,7 +83,7 @@ class Server
|
||||
}
|
||||
}
|
||||
|
||||
void Stop() { io_service.stop(); }
|
||||
void Stop() { io_context.stop(); }
|
||||
|
||||
void RegisterServiceHandler(std::unique_ptr<ServiceHandlerInterface> service_handler_)
|
||||
{
|
||||
@@ -96,7 +96,7 @@ class Server
|
||||
if (!e)
|
||||
{
|
||||
new_connection->start();
|
||||
new_connection = std::make_shared<Connection>(io_service, request_handler);
|
||||
new_connection = std::make_shared<Connection>(io_context, request_handler);
|
||||
acceptor.async_accept(
|
||||
new_connection->socket(),
|
||||
boost::bind(&Server::HandleAccept, this, boost::asio::placeholders::error));
|
||||
@@ -108,7 +108,7 @@ class Server
|
||||
}
|
||||
|
||||
unsigned thread_pool_size;
|
||||
boost::asio::io_service io_service;
|
||||
boost::asio::io_context io_context;
|
||||
boost::asio::ip::tcp::acceptor acceptor;
|
||||
std::shared_ptr<Connection> new_connection;
|
||||
RequestHandler request_handler;
|
||||
|
||||
Reference in New Issue
Block a user