mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-09-12 21:52:53 +02:00
Allow http workers to send data optimistically as an optimization
This commit is contained in:
@@ -1040,9 +1040,11 @@ void HTTPRequest::WriteReply(HTTPStatusCode status, std::span<const std::byte> r
|
||||
const std::string headers{res.StringifyHeaders()};
|
||||
const auto headers_bytes{std::as_bytes(std::span{headers})};
|
||||
|
||||
bool send_buffer_was_empty{false};
|
||||
// Fill the send buffer with the complete serialized response headers + body
|
||||
{
|
||||
LOCK(m_client->m_send_mutex);
|
||||
send_buffer_was_empty = m_client->m_send_buffer.empty();
|
||||
m_client->m_send_buffer.insert(m_client->m_send_buffer.end(), headers_bytes.begin(), headers_bytes.end());
|
||||
|
||||
// We've been using std::span up until now but it is finally time to copy
|
||||
@@ -1051,10 +1053,6 @@ void HTTPRequest::WriteReply(HTTPStatusCode status, std::span<const std::byte> r
|
||||
m_client->m_send_buffer.insert(m_client->m_send_buffer.end(), reply_body.begin(), reply_body.end());
|
||||
}
|
||||
|
||||
// Inform HTTPServer I/O loop that there is data that is ready to be sent to
|
||||
// this client in the next loop iteration.
|
||||
m_client->m_send_ready = true;
|
||||
|
||||
LogDebug(
|
||||
BCLog::HTTP,
|
||||
"HTTPResponse (status code: %d size: %lld) added to send buffer for client %s (id=%llu)",
|
||||
@@ -1062,6 +1060,18 @@ void HTTPRequest::WriteReply(HTTPStatusCode status, std::span<const std::byte> r
|
||||
headers_bytes.size() + reply_body.size(),
|
||||
m_client->m_origin,
|
||||
m_client->m_id);
|
||||
|
||||
// If the send buffer was empty before we wrote this reply, we can try an
|
||||
// optimistic send akin to CConnman::PushMessage() in which we
|
||||
// push the data directly out the socket to client right now, instead
|
||||
// of waiting for the next iteration of the I/O loop.
|
||||
if (send_buffer_was_empty) {
|
||||
m_client->MaybeSendBytesFromBuffer();
|
||||
} else {
|
||||
// Inform HTTPServer I/O that data is ready to be sent to this client
|
||||
// in the next loop iteration.
|
||||
m_client->m_send_ready = true;
|
||||
}
|
||||
}
|
||||
|
||||
util::Expected<void, std::string> HTTPServer::BindAndStartListening(const CService& to)
|
||||
@@ -1536,6 +1546,10 @@ bool HTTPRemoteClient::MaybeSendBytesFromBuffer()
|
||||
// Do not attempt to read from this client.
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
// The send buffer isn't flushed yet, try to push more on the next loop.
|
||||
m_send_ready = true;
|
||||
m_connection_busy = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user