Skip to content

Commit bbe613a

Browse files
committed
quic: apply multiple additional minor improvements
Signed-off-by: James M Snell <jasnell@gmail.com>
1 parent 364e9fa commit bbe613a

10 files changed

Lines changed: 109 additions & 92 deletions

File tree

src/quic/bindingdata.cc

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ void BindingData::OnFlushCheck() {
335335
// pending_flush_sessions_ and are picked up on the next check tick.
336336
auto sessions = std::move(pending_flush_sessions_);
337337
for (auto& session : sessions) {
338-
session->pending_flush_ = false;
338+
session->flags_.pending_flush = false;
339339
if (!session->is_destroyed()) {
340340
session->FlushPendingData();
341341
}
@@ -437,28 +437,28 @@ JS_METHOD_IMPL(BindingData::SetCallbacks) {
437437
}
438438

439439
NgTcp2CallbackScope::NgTcp2CallbackScope(Session* session) : session(session) {
440-
CHECK(!session->in_ngtcp2_callback_scope_);
441-
session->in_ngtcp2_callback_scope_ = true;
440+
CHECK(!session->flags_.in_ngtcp2_callback_scope);
441+
session->flags_.in_ngtcp2_callback_scope = true;
442442
}
443443

444444
NgTcp2CallbackScope::~NgTcp2CallbackScope() {
445-
session->in_ngtcp2_callback_scope_ = false;
446-
if (session->destroy_deferred_) {
447-
session->destroy_deferred_ = false;
445+
session->flags_.in_ngtcp2_callback_scope = false;
446+
if (session->flags_.destroy_deferred) {
447+
session->flags_.destroy_deferred = false;
448448
session->Destroy();
449449
}
450450
}
451451

452452
NgHttp3CallbackScope::NgHttp3CallbackScope(Session* session)
453453
: session(session) {
454-
CHECK(!session->in_nghttp3_callback_scope_);
455-
session->in_nghttp3_callback_scope_ = true;
454+
CHECK(!session->flags_.in_nghttp3_callback_scope);
455+
session->flags_.in_nghttp3_callback_scope = true;
456456
}
457457

458458
NgHttp3CallbackScope::~NgHttp3CallbackScope() {
459-
session->in_nghttp3_callback_scope_ = false;
460-
if (session->destroy_deferred_) {
461-
session->destroy_deferred_ = false;
459+
session->flags_.in_nghttp3_callback_scope = false;
460+
if (session->flags_.destroy_deferred) {
461+
session->flags_.destroy_deferred = false;
462462
session->Destroy();
463463
}
464464
}

src/quic/data.cc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,9 @@ Store Store::CopyFrom(Local<ArrayBuffer> buffer) {
137137
auto backing = buffer->GetBackingStore();
138138
auto length = buffer->ByteLength();
139139
auto dest = ArrayBuffer::NewBackingStore(
140-
isolate, length, BackingStoreInitializationMode::kUninitialized,
140+
isolate,
141+
length,
142+
BackingStoreInitializationMode::kUninitialized,
141143
BackingStoreOnFailureMode::kReturnNull);
142144
if (!dest) {
143145
THROW_ERR_MEMORY_ALLOCATION_FAILED(Environment::GetCurrent(isolate));
@@ -154,7 +156,9 @@ Store Store::CopyFrom(Local<ArrayBufferView> view) {
154156
auto length = view->ByteLength();
155157
auto offset = view->ByteOffset();
156158
auto dest = ArrayBuffer::NewBackingStore(
157-
isolate, length, BackingStoreInitializationMode::kUninitialized,
159+
isolate,
160+
length,
161+
BackingStoreInitializationMode::kUninitialized,
158162
BackingStoreOnFailureMode::kReturnNull);
159163
// copy content
160164
if (!dest) {

src/quic/endpoint.cc

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ Endpoint::UDP::~UDP() {
378378
}
379379

380380
int Endpoint::UDP::Bind(const Options& options) {
381-
if (is_bound_) return UV_EALREADY;
381+
if (flags_.is_bound) return UV_EALREADY;
382382
if (is_closed_or_closing()) return UV_EBADF;
383383

384384
int flags = 0;
@@ -388,7 +388,7 @@ int Endpoint::UDP::Bind(const Options& options) {
388388
int size;
389389

390390
if (!err) {
391-
is_bound_ = true;
391+
flags_.is_bound = true;
392392
size = static_cast<int>(options.udp_receive_buffer_size);
393393
if (size > 0) {
394394
err = uv_recv_buffer_size(reinterpret_cast<uv_handle_t*>(&impl_->handle_),
@@ -427,34 +427,34 @@ void Endpoint::UDP::Unref() {
427427

428428
int Endpoint::UDP::Start() {
429429
if (is_closed_or_closing()) return UV_EBADF;
430-
if (is_started_) return 0;
430+
if (flags_.is_started) return 0;
431431
int err = uv_udp_recv_start(&impl_->handle_, Impl::OnAlloc, Impl::OnReceive);
432-
is_started_ = (err == 0);
432+
flags_.is_started = (err == 0);
433433
return err;
434434
}
435435

436436
void Endpoint::UDP::Stop() {
437-
if (is_closed_or_closing() || !is_started_) return;
437+
if (is_closed_or_closing() || !flags_.is_started) return;
438438
USE(uv_udp_recv_stop(&impl_->handle_));
439-
is_started_ = false;
439+
flags_.is_started = false;
440440
}
441441

442442
void Endpoint::UDP::Close() {
443443
if (is_closed_or_closing()) return;
444444
DCHECK(impl_);
445445
Stop();
446-
is_bound_ = false;
447-
is_closed_ = true;
446+
flags_.is_bound = false;
447+
flags_.is_closed = true;
448448
impl_->Close();
449449
impl_.reset();
450450
}
451451

452452
bool Endpoint::UDP::is_bound() const {
453-
return is_bound_;
453+
return flags_.is_bound;
454454
}
455455

456456
bool Endpoint::UDP::is_closed() const {
457-
return is_closed_;
457+
return flags_.is_closed;
458458
}
459459

460460
bool Endpoint::UDP::is_closed_or_closing() const {
@@ -1294,8 +1294,8 @@ void Endpoint::Receive(const uint8_t* data,
12941294
}
12951295
// Schedule the session for deferred SendPendingData if it hasn't
12961296
// been scheduled already in this burst.
1297-
if (!session->is_destroyed() && !session->pending_flush_) {
1298-
session->pending_flush_ = true;
1297+
if (!session->is_destroyed() && !session->flags_.pending_flush) {
1298+
session->flags_.pending_flush = true;
12991299
BindingData::Get(env()).ScheduleSessionFlush(
13001300
BaseObjectPtr<Session>(session));
13011301
}

src/quic/endpoint.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -343,9 +343,12 @@ class Endpoint final : public AsyncWrap, public Packet::Listener {
343343
class Impl;
344344

345345
BaseObjectWeakPtr<Impl> impl_;
346-
bool is_bound_ = false;
347-
bool is_started_ = false;
348-
bool is_closed_ = false;
346+
struct Flags {
347+
uint8_t is_bound : 1 = 0;
348+
uint8_t is_started : 1 = 0;
349+
uint8_t is_closed : 1 = 0;
350+
};
351+
Flags flags_;
349352
};
350353

351354
bool is_closed() const;

src/quic/quic.cc

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#include "node_external_reference.h"
1414

1515
#include <ngtcp2/ngtcp2_crypto_ossl.h>
16-
#include <mutex>
16+
1717
namespace node {
1818

1919
using v8::Context;
@@ -25,7 +25,11 @@ using v8::Value;
2525
namespace quic {
2626

2727
namespace {
28-
std::once_flag crypto_init_flag;
28+
uv_once_t crypto_init_flag = UV_ONCE_INIT;
29+
30+
void InitNgtcp2CryptoOnce() {
31+
ngtcp2_crypto_ossl_init();
32+
}
2933
} // namespace
3034

3135
void CreatePerIsolateProperties(IsolateData* isolate_data,
@@ -39,8 +43,8 @@ void CreatePerContextProperties(Local<Object> target,
3943
Local<Value> unused,
4044
Local<Context> context,
4145
void* priv) {
46+
uv_once(&crypto_init_flag, InitNgtcp2CryptoOnce);
4247
Realm* realm = Realm::GetCurrent(context);
43-
std::call_once(crypto_init_flag, ngtcp2_crypto_ossl_init);
4448
BindingData::InitPerContext(realm, target);
4549
Endpoint::InitPerContext(realm, target);
4650
Session::InitPerContext(realm, target);

src/quic/session.cc

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1810,7 +1810,7 @@ bool Session::is_server() const {
18101810
}
18111811

18121812
bool Session::is_destroyed() const {
1813-
return !impl_ || destroy_deferred_;
1813+
return !impl_ || flags_.destroy_deferred;
18141814
}
18151815

18161816
bool Session::is_destroyed_or_closing() const {
@@ -1988,9 +1988,9 @@ void Session::Destroy() {
19881988
// destroy impl_ now because the callback is executing methods on
19891989
// objects owned by impl_ (e.g., the Application). Defer the
19901990
// destruction until the scope exits.
1991-
if (in_ngtcp2_callback_scope_ || in_nghttp3_callback_scope_) {
1991+
if (flags_.in_ngtcp2_callback_scope || flags_.in_nghttp3_callback_scope) {
19921992
Debug(this, "Session destroy deferred (in callback scope)");
1993-
destroy_deferred_ = true;
1993+
flags_.destroy_deferred = true;
19941994
return;
19951995
}
19961996

@@ -2131,8 +2131,8 @@ void Session::EmitQlog(uint32_t flags, std::string_view data) {
21312131
if (is_destroyed()) {
21322132
auto isolate = env()->isolate();
21332133
Global<Object> recv(isolate, object());
2134-
Global<Function> cb(
2135-
isolate, BindingData::Get(env()).session_qlog_callback());
2134+
Global<Function> cb(isolate,
2135+
BindingData::Get(env()).session_qlog_callback());
21362136
std::string buf(data);
21372137
env()->SetImmediate([recv = std::move(recv),
21382138
cb = std::move(cb),
@@ -2381,7 +2381,7 @@ void Session::SendBatch(Packet::Ptr* packets,
23812381
if (primary_count == 0) return;
23822382

23832383
// Use batched send for the primary endpoint.
2384-
if (prefer_try_send_) {
2384+
if (flags_.prefer_try_send) {
23852385
endpoint().SendBatch(primary_packets, primary_count);
23862386
} else {
23872387
// Non-flush path: send individually via async uv_udp_send.
@@ -2396,9 +2396,9 @@ void Session::FlushPendingData() {
23962396
if (impl_->application_) {
23972397
// Prefer synchronous sends during the deferred flush to avoid the
23982398
// one-tick latency of async uv_udp_send from the uv_check callback.
2399-
prefer_try_send_ = true;
2399+
flags_.prefer_try_send = true;
24002400
application().SendPendingData();
2401-
prefer_try_send_ = false;
2401+
flags_.prefer_try_send = false;
24022402
}
24032403
}
24042404

@@ -2422,7 +2422,7 @@ void Session::Send(Packet::Ptr packet) {
24222422
// prefer synchronous send to avoid the one-tick latency of async
24232423
// uv_udp_send. SendOrTrySend uses uv_udp_try_send first, falling
24242424
// back to uv_udp_send on EAGAIN.
2425-
if (prefer_try_send_) {
2425+
if (flags_.prefer_try_send) {
24262426
Debug(this, "Session is sending (try_send) %s", packet->ToString());
24272427
endpoint().SendOrTrySend(std::move(packet));
24282428
return;
@@ -2857,7 +2857,7 @@ bool Session::can_send_packets() const {
28572857
// or closing period. The callback scope check is per-session so that
28582858
// one session's ngtcp2 callback does not block unrelated sessions
28592859
// from sending.
2860-
return !is_destroyed() && !in_ngtcp2_callback_scope_ &&
2860+
return !is_destroyed() && !flags_.in_ngtcp2_callback_scope &&
28612861
!is_in_draining_period() && !is_in_closing_period();
28622862
}
28632863

src/quic/session.h

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -606,25 +606,30 @@ class Session final : public AsyncWrap, private SessionTicket::AppData::Source {
606606
Side side_;
607607
const ngtcp2_mem* allocator_;
608608
std::unique_ptr<Impl> impl_;
609-
// These flags live on Session (not Impl) so that the NgTcp2CallbackScope
610-
// and NgHttp3CallbackScope destructors can safely clear them even after
611-
// Impl has been destroyed via MakeCallback re-entrancy during a callback.
612-
// The scope is placed at the ngtcp2/nghttp3 entry point (e.g. Receive,
613-
// OnTimeout) rather than on individual callbacks, so the deferred destroy
614-
// only fires after all callbacks for that entry point have completed.
615-
bool in_ngtcp2_callback_scope_ = false;
616-
bool in_nghttp3_callback_scope_ = false;
617-
bool destroy_deferred_ = false;
618-
// Set when this session is in BindingData's pending_flush_sessions_ vector.
619-
// Cleared by the flush callback before calling SendPendingData.
620-
// Provides O(1) dedup so a session receiving multiple packets in one I/O
621-
// burst is only scheduled for flush once.
622-
bool pending_flush_ = false;
623-
// When true, Session::Send prefers synchronous delivery via
624-
// Endpoint::SendOrTrySend (uv_udp_try_send with async fallback).
625-
// Set during FlushPendingData to avoid the one-tick latency of
626-
// async-only sends from the uv_check callback.
627-
bool prefer_try_send_ = false;
609+
610+
struct Flags {
611+
// These flags live on Session (not Impl) so that the NgTcp2CallbackScope
612+
// and NgHttp3CallbackScope destructors can safely clear them even after
613+
// Impl has been destroyed via MakeCallback re-entrancy during a callback.
614+
// The scope is placed at the ngtcp2/nghttp3 entry point (e.g. Receive,
615+
// OnTimeout) rather than on individual callbacks, so the deferred destroy
616+
// only fires after all callbacks for that entry point have completed.
617+
uint8_t in_ngtcp2_callback_scope : 1 = 0;
618+
uint8_t in_nghttp3_callback_scope : 1 = 0;
619+
uint8_t destroy_deferred : 1 = 0;
620+
// Set when this session is in BindingData's pending_flush_sessions_ vector.
621+
// Cleared by the flush callback before calling SendPendingData.
622+
// Provides O(1) dedup so a session receiving multiple packets in one I/O
623+
// burst is only scheduled for flush once.
624+
uint8_t pending_flush : 1 = 0;
625+
// When true, Session::Send prefers synchronous delivery via
626+
// Endpoint::SendOrTrySend (uv_udp_try_send with async fallback).
627+
// Set during FlushPendingData to avoid the one-tick latency of
628+
// async-only sends from the uv_check callback.
629+
uint8_t prefer_try_send : 1 = 0;
630+
};
631+
Flags flags_;
632+
628633
QuicConnectionPointer connection_;
629634
std::unique_ptr<TLSSession> tls_session_;
630635
friend struct NgTcp2CallbackScope;

0 commit comments

Comments
 (0)