quic: cache the timestamp on send and receive

Signed-off-by: James M Snell <jasnell@gmail.com>
Assisted-by: Opencode:Opus 4.6
PR-URL: https://github.com/nodejs/node/pull/63267
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
James M Snell authored and Antoine du Hamel committed 2026-05-23 00:05:40 +02:00
1 parent 9560084560
commit aec1e17ec5
4 files changed
+39 -15

No files matched your search

+17 -7
View File
@@ -239,7 +239,8 @@ void Session::Application::ReceiveStreamReset(Stream* stream,
// < 0 (other): fatal error, session already closed
ssize_t Session::Application::TryWritePendingDatagram(PathStorage* path,
uint8_t* dest,
size_t destlen) {
size_t destlen,
uint64_t ts) {
CHECK(session_->HasPendingDatagrams());
auto max_attempts = session_->config().options.max_datagram_send_attempts;
@@ -275,7 +276,7 @@ ssize_t Session::Application::TryWritePendingDatagram(PathStorage* path,
dg.id,
&dgvec,
1,
uv_hrtime());
ts);
if (accepted) {
// Nice, the datagram was accepted!
@@ -338,6 +339,13 @@ void Session::Application::SendPendingData() {
// call is dynamically capped by ngtcp2_conn_get_send_quantum().
static constexpr size_t kMaxPackets = 64;
Debug(session_, "Application sending pending data");
// Cache the timestamp once for the entire send loop. ngtcp2 does not
// require nanosecond-accurate monotonicity within a single burst —
// a single timestamp per SendPendingData call is what other QUIC
// implementations use (e.g., quiche, msquic). When kernel-level
// packet pacing becomes available via libuv, this timestamp becomes
// the base for computing per-packet transmit timestamps.
const uint64_t ts = uv_hrtime();
PathStorage path;
StreamData stream_data;
@@ -441,7 +449,8 @@ void Session::Application::SendPendingData() {
// Awesome, let's write our packet!
PacketInfo pi;
ssize_t nwrite = WriteVStream(
&path, &pi, packet->data(), &ndatalen, packet->length(), stream_data);
&path, &pi, packet->data(), &ndatalen, packet->length(),
stream_data, ts);
// When ndatalen is > 0, that's our indication that stream data was accepted
// in to the packet. Yay!
@@ -528,7 +537,7 @@ void Session::Application::SendPendingData() {
// if there is one. Otherwise just loop around and keep going.
if (session_->HasPendingDatagrams()) {
auto result = TryWritePendingDatagram(
&path, packet->data(), packet->length());
&path, packet->data(), packet->length(), ts);
// When result is 0, either the datagram was congestion controlled,
// didn't fit in the packet, or was abandoned. Skip and continue.
@@ -590,7 +599,7 @@ void Session::Application::SendPendingData() {
return session_->Close(CloseMethod::SILENT);
}
auto result =
TryWritePendingDatagram(&path, packet->data(), packet->length());
TryWritePendingDatagram(&path, packet->data(), packet->length(), ts);
if (result > 0) {
Debug(session_, "Sending datagram packet with %zd bytes", result);
enqueue_packet(packet, static_cast<size_t>(result), PacketInfo());
@@ -610,7 +619,8 @@ ssize_t Session::Application::WriteVStream(PathStorage* path,
uint8_t* dest,
ssize_t* ndatalen,
size_t max_packet_size,
const StreamData& stream_data) {
const StreamData& stream_data,
uint64_t ts) {
DCHECK_LE(stream_data.count, kMaxVectorCount);
uint32_t flags = NGTCP2_WRITE_STREAM_FLAG_MORE;
if (stream_data.fin) flags |= NGTCP2_WRITE_STREAM_FLAG_FIN;
@@ -627,7 +637,7 @@ ssize_t Session::Application::WriteVStream(PathStorage* path,
stream_data.id,
stream_data,
stream_data.count,
uv_hrtime());
ts);
}
// ============================================================================
+4 -2
View File
@@ -267,7 +267,8 @@ class Session::Application : public MemoryRetainer {
// the datagram is either congestion limited or was abandoned
ssize_t TryWritePendingDatagram(PathStorage* path,
uint8_t* dest,
size_t destlen);
size_t destlen,
uint64_t ts);
// Write the given stream_data into the buffer. The PacketInfo out-param
// is populated by ngtcp2 with per-packet metadata (e.g., ECN codepoint)
@@ -277,7 +278,8 @@ class Session::Application : public MemoryRetainer {
uint8_t* buf,
ssize_t* ndatalen,
size_t max_packet_size,
const StreamData& stream_data);
const StreamData& stream_data,
uint64_t ts);
Session* session_ = nullptr;
};
+11 -4
View File
@@ -2104,20 +2104,23 @@ void Session::SetLastError(QuicError&& error) {
bool Session::Receive(Store&& store,
const SocketAddress& local_address,
const SocketAddress& remote_address,
const PacketInfo& pkt_info) {
const PacketInfo& pkt_info,
uint64_t ts) {
// Convenience wrapper: reads the packet and immediately triggers
// SendPendingData. Used by paths that need an immediate response
// (e.g., Endpoint::Connect for client Initial packets).
// The hot receive path uses ReadPacket() directly with deferred
// flush via BindingData's uv_check callback.
SendPendingDataScope send_scope(this);
return ReadPacket(std::move(store), local_address, remote_address, pkt_info);
return ReadPacket(
std::move(store), local_address, remote_address, pkt_info, ts);
}
bool Session::ReadPacket(Store&& store,
const SocketAddress& local_address,
const SocketAddress& remote_address,
const PacketInfo& pkt_info) {
const PacketInfo& pkt_info,
uint64_t ts) {
DCHECK(!is_destroyed());
impl_->remote_address_ = remote_address;
@@ -2143,8 +2146,12 @@ bool Session::ReadPacket(Store&& store,
// When libuv gains per-packet ECN reporting, the caller should
// populate pkt_info from the receive metadata before calling
// ReadPacket().
// When ts is 0 (the default), call uv_hrtime() here. The batched
// receive path caches a timestamp and passes it to all ReadPacket()
// calls in the same I/O burst.
if (ts == 0) ts = uv_hrtime();
err = ngtcp2_conn_read_pkt(
*this, &path, pkt_info, vec.base, vec.len, uv_hrtime());
*this, &path, pkt_info, vec.base, vec.len, ts);
}
if (is_destroyed()) return false;
+7 -2
View File
@@ -356,7 +356,8 @@ class Session final : public AsyncWrap, private SessionTicket::AppData::Source {
bool Receive(Store&& store,
const SocketAddress& local_address,
const SocketAddress& remote_address,
const PacketInfo& pkt_info = PacketInfo());
const PacketInfo& pkt_info = PacketInfo(),
uint64_t ts = 0);
// ReadPacket processes a single inbound packet through ngtcp2 without
// triggering SendPendingData. This is the building block for batched
@@ -366,10 +367,14 @@ class Session final : public AsyncWrap, private SessionTicket::AppData::Source {
// Receive() is kept as a convenience wrapper that calls ReadPacket()
// then triggers SendPendingData (for paths like Connect that need
// immediate response).
// When ts is 0 (the default), uv_hrtime() is called internally.
// The batched receive path caches a timestamp and passes it to all
// ReadPacket() calls in the same I/O burst.
bool ReadPacket(Store&& store,
const SocketAddress& local_address,
const SocketAddress& remote_address,
const PacketInfo& pkt_info = PacketInfo());
const PacketInfo& pkt_info = PacketInfo(),
uint64_t ts = 0);
// Called by BindingData's flush callback to trigger SendPendingData
// on this session. Encapsulates the application() access so that