Skip to content

Commit f43bf64

Browse files
authored
Add TCP_NODELAY to disable Nagle's algorithm in Boost.ASIO-based http_client (#1310)
* Add TCP_NODELAY to disable Nagle's algorithm to avoid waiting for ACK for HTTP request headers before transmitting the request body, in order to significantly improve performance for requests with bodies (see #1201) * Also add TCP_NODELAY on the server-side, to disable Nagle's algorithm to avoid waiting for ACK for HTTP response headers before transmitting the response body (see #1201)
1 parent a835bfa commit f43bf64

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

Release/src/http/client/http_client_asio.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,12 @@ class asio_connection
330330

331331
void start_reuse() { m_is_reused = true; }
332332

333+
void enable_no_delay()
334+
{
335+
boost::asio::ip::tcp::no_delay option(true);
336+
m_socket.set_option(option);
337+
}
338+
333339
private:
334340
// Guards concurrent access to socket/ssl::stream. This is necessary
335341
// because timeouts and cancellation can touch the socket at the same time
@@ -610,6 +616,7 @@ class asio_context final : public request_context, public std::enable_shared_fro
610616
if (!ec)
611617
{
612618
m_context->m_timer.reset();
619+
m_context->m_connection->enable_no_delay();
613620
m_context->m_connection->async_write(m_request,
614621
boost::bind(&ssl_proxy_tunnel::handle_write_request,
615622
shared_from_this(),
@@ -1006,6 +1013,7 @@ class asio_context final : public request_context, public std::enable_shared_fro
10061013
m_timer.reset();
10071014
if (!ec)
10081015
{
1016+
m_connection->enable_no_delay();
10091017
write_request();
10101018
}
10111019
else if (ec.value() == boost::system::errc::operation_canceled ||

Release/src/http/listener/http_server_asio.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,9 @@ void hostport_listener::on_accept(std::unique_ptr<ip::tcp::socket> socket, const
593593
// Handle successful accept
594594
if (!ec)
595595
{
596+
boost::asio::ip::tcp::no_delay option(true);
597+
socket->set_option(option);
598+
596599
auto conn = asio_server_connection::create(std::move(socket), m_p_server, this);
597600

598601
m_connections.insert(conn.get());

0 commit comments

Comments
 (0)