quic: improve backend quic packet processing · nodejs/node@ff0cd5b · GitHub
Skip to content

Commit ff0cd5b

Browse files
jasnelladuh95
authored andcommitted
quic: improve backend quic packet processing
Use a uv_check_t on BindingData to process outbound pending packet send, and use TrySend for actually sending packets when possible. Results in an 8% improvement in req/s and ~24% improvement in p95 latency. Also sets us up better for future improvements in libuv if the changes proposed in libuv/libuv#5116 are accepted. Signed-off-by: James M Snell <jasnell@gmail.com> Assisted-by: Opencode:Opus 4.6 PR-URL: #63267 Backport-PR-URL: #64675 Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent c4eb6a9 commit ff0cd5b

6 files changed

Lines changed: 214 additions & 8 deletions

File tree

src/quic/bindingdata.cc

Lines changed: 55 additions & 0 deletions

src/quic/bindingdata.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
#include <ngtcp2/ngtcp2_crypto.h>
1111
#include <node.h>
1212
#include <node_mem.h>
13+
#include <uv.h>
1314
#include <v8.h>
1415
#include <memory>
1516
#include <unordered_map>
17+
#include <vector>
1618
#include "defs.h"
1719

1820
namespace node::quic {
@@ -202,6 +204,13 @@ class BindingData final
202204
// routing so that any endpoint can route packets to any session.
203205
SessionManager& session_manager();
204206

207+
// Schedule a session for deferred SendPendingData. Sessions are accumulated
208+
// during the I/O poll phase (via Endpoint::Receive -> Session::ReadPacket)
209+
// and flushed in a uv_check callback immediately after poll completes.
210+
// This batches multiple received packets before generating responses,
211+
// allowing ngtcp2 to make better ACK coalescing decisions.
212+
void ScheduleSessionFlush(const BaseObjectPtr<Session>& session);
213+
205214
std::unordered_map<Endpoint*, BaseObjectPtr<BaseObject>> listening_endpoints;
206215

207216
size_t current_ngtcp2_memory_ = 0;
@@ -248,6 +257,17 @@ class BindingData final
248257
#undef V
249258

250259
std::unique_ptr<SessionManager> session_manager_;
260+
261+
// Deferred send flush state. The uv_check_t fires immediately after
262+
// the I/O poll phase in the same event loop tick, allowing batched
263+
// receive processing: all packets are read during poll, then
264+
// SendPendingData is called once per dirty session in the check callback.
265+
uv_check_t flush_check_;
266+
std::vector<BaseObjectPtr<Session>> pending_flush_sessions_;
267+
bool flush_check_started_ = false;
268+
bool flush_check_initialized_ = false;
269+
270+
static void OnFlushCheck(uv_check_t* handle);
251271
};
252272

253273
JS_METHOD_IMPL(IllegalConstructor);

src/quic/endpoint.cc

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,14 @@ int Endpoint::UDP::Send(Packet::Ptr packet) {
492492
return err;
493493
}
494494

495+
int Endpoint::UDP::TrySend(Packet* packet) {
496+
DCHECK_NOT_NULL(packet);
497+
if (is_closed_or_closing()) return UV_EBADF;
498+
uv_buf_t buf = *packet;
499+
return uv_udp_try_send(
500+
&impl_->handle_, &buf, 1, packet->destination().data());
501+
}
502+
495503
void Endpoint::UDP::MemoryInfo(MemoryTracker* tracker) const {
496504
if (impl_) tracker->TrackField("impl", impl_);
497505
}
@@ -812,6 +820,45 @@ void Endpoint::Send(Packet::Ptr packet) {
812820
STAT_INCREMENT(Stats, packets_sent);
813821
}
814822

823+
void Endpoint::SendOrTrySend(Packet::Ptr packet) {
824+
#ifdef DEBUG
825+
if (is_diagnostic_packet_loss(options_.tx_loss)) [[unlikely]] {
826+
return;
827+
}
828+
#endif
829+
830+
if (is_closed() || is_closing() || packet->length() == 0) {
831+
return;
832+
}
833+
834+
Debug(this, "TrySend %s", packet->ToString());
835+
size_t packet_length = packet->length();
836+
837+
// Attempt synchronous send. On success (returns number of bytes sent),
838+
// the packet is delivered immediately — no callback overhead, no
839+
// waiting for the next poll cycle.
840+
int err = udp_.TrySend(packet.get());
841+
if (err >= 0) {
842+
// Synchronous send succeeded. Release the packet immediately.
843+
STAT_INCREMENT_N(Stats, bytes_sent, packet_length);
844+
STAT_INCREMENT(Stats, packets_sent);
845+
// Ptr destructor releases back to arena pool.
846+
return;
847+
}
848+
849+
if (err == UV_EAGAIN) {
850+
// Socket not writable or async sends are queued. Fall back to the
851+
// async path — the packet will be queued and flushed on the next
852+
// POLLOUT cycle.
853+
Debug(this, "TrySend got EAGAIN, falling back to async Send");
854+
return Send(std::move(packet));
855+
}
856+
857+
// Other errors are fatal.
858+
Debug(this, "TrySend failed with error %d", err);
859+
Destroy(CloseContext::SEND_FAILURE, err);
860+
}
861+
815862
void Endpoint::SendRetry(const PathDescriptor& options) {
816863
// Generating and sending retry packets does consume some system resources,
817864
// and it is possible for a malicious peer to trigger sending a large number
@@ -1152,10 +1199,22 @@ void Endpoint::Receive(const uv_buf_t& buf,
11521199
DCHECK_NOT_NULL(session);
11531200
if (session->is_destroyed()) return;
11541201
size_t len = store.length();
1155-
if (session->Receive(std::move(store), local_address, remote_address)) {
1202+
// Use ReadPacket (no SendPendingDataScope) so that multiple packets
1203+
// received in the same I/O burst are processed before any responses
1204+
// are generated. The deferred flush via BindingData's uv_check
1205+
// callback calls SendPendingData once per dirty session after all
1206+
// packets in the burst have been read.
1207+
if (session->ReadPacket(std::move(store), local_address, remote_address)) {
11561208
STAT_INCREMENT_N(Stats, bytes_received, len);
11571209
STAT_INCREMENT(Stats, packets_received);
11581210
}
1211+
// Schedule the session for deferred SendPendingData if it hasn't
1212+
// been scheduled already in this burst.
1213+
if (!session->is_destroyed() && !session->pending_flush_) {
1214+
session->pending_flush_ = true;
1215+
BindingData::Get(env()).ScheduleSessionFlush(
1216+
BaseObjectPtr<Session>(session));
1217+
}
11591218
};
11601219

11611220
const auto accept = [&](const Session::Config& config, Store&& store) {

src/quic/endpoint.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,13 @@ class Endpoint final : public AsyncWrap, public Packet::Listener {
228228

229229
void Send(Packet::Ptr packet);
230230

231+
// Attempt synchronous send via uv_udp_try_send. If the socket is
232+
// writable, the packet is sent immediately and the Ptr is released.
233+
// If the socket is not writable (UV_EAGAIN), falls back to the
234+
// async Send path. Used by the deferred flush callback to avoid
235+
// the one-tick latency of async uv_udp_send.
236+
void SendOrTrySend(Packet::Ptr packet);
237+
231238
// Acquire a Packet from the pool. length sets the initial working
232239
// size (must be <= pool capacity). The slot is always allocated at
233240
// full capacity to avoid fragmentation.
@@ -301,6 +308,12 @@ class Endpoint final : public AsyncWrap, public Packet::Listener {
301308
void Close();
302309
int Send(Packet::Ptr packet);
303310

311+
// Synchronous send using uv_udp_try_send. Returns 0 on success,
312+
// UV_EAGAIN if the socket is not writable or the send queue is
313+
// non-empty, or another negative error code on failure.
314+
// On success, the caller is responsible for releasing the packet.
315+
int TrySend(Packet* packet);
316+
304317
// Returns the local UDP socket address to which we are bound,
305318
// or fail with an assert if we are not bound.
306319
SocketAddress local_address() const;

src/quic/session.cc

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2112,13 +2112,21 @@ void Session::SetLastError(QuicError&& error) {
21122112
bool Session::Receive(Store&& store,
21132113
const SocketAddress& local_address,
21142114
const SocketAddress& remote_address) {
2115+
// Convenience wrapper: reads the packet and immediately triggers
2116+
// SendPendingData. Used by paths that need an immediate response
2117+
// (e.g., Endpoint::Connect for client Initial packets).
2118+
// The hot receive path uses ReadPacket() directly with deferred
2119+
// flush via BindingData's uv_check callback.
2120+
SendPendingDataScope send_scope(this);
2121+
return ReadPacket(std::move(store), local_address, remote_address);
2122+
}
2123+
2124+
bool Session::ReadPacket(Store&& store,
2125+
const SocketAddress& local_address,
2126+
const SocketAddress& remote_address) {
21152127
DCHECK(!is_destroyed());
21162128
impl_->remote_address_ = remote_address;
21172129

2118-
// When we are done processing this packet, we arrange to send any
2119-
// pending data for this session.
2120-
SendPendingDataScope send_scope(this);
2121-
21222130
ngtcp2_vec vec = store;
21232131
Path path(local_address, remote_address);
21242132

@@ -2133,14 +2141,16 @@ bool Session::Receive(Store&& store,
21332141
// ensures that any deferred destroy waits until all callbacks for this
21342142
// packet have completed. After calling ngtcp2_conn_read_pkt here, we
21352143
// will need to double check that the session is not destroyed before
2136-
// we try doing anything with it (like updating stats, sending pending
2137-
// data, etc).
2144+
// we try doing anything with it (like updating stats, etc).
21382145
int err;
21392146
{
21402147
NgTcp2CallbackScope callback_scope(this);
2148+
// ECN codepoint (ngtcp2_pkt_info.ecn) is not yet populated because
2149+
// libuv does not currently deliver per-packet ECN metadata. When
2150+
// libuv gains ECN receive reporting, the pkt_info should be
2151+
// populated from the per-packet metadata and passed through here.
21412152
err = ngtcp2_conn_read_pkt(*this,
21422153
&path,
2143-
// TODO(@jasnell): ECN pkt_info blocked on libuv
21442154
nullptr,
21452155
vec.base,
21462156
vec.len,
@@ -2253,6 +2263,17 @@ bool Session::Receive(Store&& store,
22532263
return false;
22542264
}
22552265

2266+
void Session::FlushPendingData() {
2267+
DCHECK(!is_destroyed());
2268+
if (impl_->application_) {
2269+
// Prefer synchronous sends during the deferred flush to avoid the
2270+
// one-tick latency of async uv_udp_send from the uv_check callback.
2271+
prefer_try_send_ = true;
2272+
application().SendPendingData();
2273+
prefer_try_send_ = false;
2274+
}
2275+
}
2276+
22562277
void Session::Send(Packet::Ptr packet) {
22572278
// Sending a Packet is generally best effort. If we're not in a state
22582279
// where we can send a packet, it's ok to drop it on the floor. The
@@ -2269,6 +2290,16 @@ void Session::Send(Packet::Ptr packet) {
22692290
return;
22702291
}
22712292

2293+
// When called from the deferred flush path (uv_check callback),
2294+
// prefer synchronous send to avoid the one-tick latency of async
2295+
// uv_udp_send. SendOrTrySend uses uv_udp_try_send first, falling
2296+
// back to uv_udp_send on EAGAIN.
2297+
if (prefer_try_send_) {
2298+
Debug(this, "Session is sending (try_send) %s", packet->ToString());
2299+
endpoint().SendOrTrySend(std::move(packet));
2300+
return;
2301+
}
2302+
22722303
Debug(this, "Session is sending %s", packet->ToString());
22732304
endpoint().Send(std::move(packet));
22742305
}

src/quic/session.h

Lines changed: 28 additions & 0 deletions

0 commit comments

Comments
 (0)