Skip to content

Commit 3bae2d5

Browse files
committedJul 1, 2020
quic: consolidate onSessionClose and onSessionSilentClose
Use a single callback function for both PR-URL: #34137 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: David Carlier <devnexen@gmail.com>
1 parent def8e76 commit 3bae2d5

File tree

5 files changed

+42
-64
lines changed

5 files changed

+42
-64
lines changed
 

‎lib/internal/quic/core.js

+16-24
Original file line numberDiff line numberDiff line change
@@ -271,23 +271,24 @@ function onSessionReady(handle) {
271271
process.nextTick(emit.bind(socket, 'session', session));
272272
}
273273

274-
// During an immediate close, all currently open QuicStreams are
275-
// abruptly closed. If they are still writable or readable, an abort
276-
// event will be emitted, and RESET_STREAM and STOP_SENDING frames
277-
// will be transmitted as necessary. Once streams have been
278-
// shutdown, a CONNECTION_CLOSE frame will be sent and the
279-
// session will enter the closing period, after which it will
280-
// be destroyed either when the idle timeout expires, the
281-
// QuicSession is silently closed, or destroy is called.
282-
function onSessionClose(code, family) {
274+
// Called when the session needs to be closed and destroyed.
275+
// If silent is true, then the session is going to be closed
276+
// immediately without sending any CONNECTION_CLOSE to the
277+
// connected peer. If silent is false, a CONNECTION_CLOSE
278+
// is going to be sent to the peer.
279+
function onSessionClose(code, family, silent, statelessReset) {
283280
if (this[owner_symbol]) {
284-
this[owner_symbol][kClose](family, code);
285-
} else {
286-
// When there's no owner_symbol, the session was closed
287-
// before it could be fully set up. Just immediately
288-
// close everything down on the native side.
289-
this.destroy(code, family);
281+
if (silent) {
282+
this[owner_symbol][kDestroy](statelessReset, family, code);
283+
} else {
284+
this[owner_symbol][kClose](family, code);
285+
}
286+
return;
290287
}
288+
// When there's no owner_symbol, the session was closed
289+
// before it could be fully set up. Just immediately
290+
// close everything down on the native side.
291+
this.destroy(code, family);
291292
}
292293

293294
// Called by the C++ internals when a QuicSession has been destroyed.
@@ -554,14 +555,6 @@ function onStreamHeaders(id, headers, kind, push_id) {
554555
this[owner_symbol][kHeaders](id, headers, kind, push_id);
555556
}
556557

557-
// During a silent close, all currently open QuicStreams are abruptly
558-
// closed. If they are still writable or readable, an abort event will be
559-
// emitted, otherwise the stream is just destroyed. No RESET_STREAM or
560-
// STOP_SENDING is transmitted to the peer.
561-
function onSessionSilentClose(statelessReset, code, family) {
562-
this[owner_symbol][kDestroy](statelessReset, family, code);
563-
}
564-
565558
// When a stream is flow control blocked, causes a blocked event
566559
// to be emitted. This is a purely informational event.
567560
function onStreamBlocked() {
@@ -581,7 +574,6 @@ setCallbacks({
581574
onSessionHandshake,
582575
onSessionKeylog,
583576
onSessionQlog,
584-
onSessionSilentClose,
585577
onSessionStatus,
586578
onSessionTicket,
587579
onSessionVersionNegotiation,

‎src/env.h

-1
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,6 @@ constexpr size_t kFsStatsBufferLength =
462462
V(quic_on_session_use_preferred_address_function, v8::Function) \
463463
V(quic_on_session_qlog_function, v8::Function) \
464464
V(quic_on_session_ready_function, v8::Function) \
465-
V(quic_on_session_silent_close_function, v8::Function) \
466465
V(quic_on_session_status_function, v8::Function) \
467466
V(quic_on_session_ticket_function, v8::Function) \
468467
V(quic_on_session_version_negotiation_function, v8::Function) \

‎src/quic/node_quic.cc

-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ void QuicSetCallbacks(const FunctionCallbackInfo<Value>& args) {
6666
SETFUNCTION("onSessionUsePreferredAddress", session_use_preferred_address);
6767
SETFUNCTION("onSessionPathValidation", session_path_validation);
6868
SETFUNCTION("onSessionQlog", session_qlog);
69-
SETFUNCTION("onSessionSilentClose", session_silent_close);
7069
SETFUNCTION("onSessionStatus", session_status);
7170
SETFUNCTION("onSessionTicket", session_ticket);
7271
SETFUNCTION("onSessionVersionNegotiation", session_version_negotiation);

‎src/quic/node_quic_session.cc

+14-32
Original file line numberDiff line numberDiff line change
@@ -293,9 +293,9 @@ void QuicSessionListener::OnSessionDestroyed() {
293293
previous_listener_->OnSessionDestroyed();
294294
}
295295

296-
void QuicSessionListener::OnSessionClose(QuicError error) {
296+
void QuicSessionListener::OnSessionClose(QuicError error, int flags) {
297297
if (previous_listener_ != nullptr)
298-
previous_listener_->OnSessionClose(error);
298+
previous_listener_->OnSessionClose(error, flags);
299299
}
300300

301301
void QuicSessionListener::OnStreamReady(BaseObjectPtr<QuicStream> stream) {
@@ -328,13 +328,6 @@ void QuicSessionListener::OnStreamBlocked(int64_t stream_id) {
328328
}
329329
}
330330

331-
void QuicSessionListener::OnSessionSilentClose(
332-
bool stateless_reset,
333-
QuicError error) {
334-
if (previous_listener_ != nullptr)
335-
previous_listener_->OnSessionSilentClose(stateless_reset, error);
336-
}
337-
338331
void QuicSessionListener::OnUsePreferredAddress(
339332
int family,
340333
const PreferredAddress& preferred_address) {
@@ -525,14 +518,20 @@ void JSQuicSessionListener::OnSessionDestroyed() {
525518
env->quic_on_session_destroyed_function(), 0, nullptr);
526519
}
527520

528-
void JSQuicSessionListener::OnSessionClose(QuicError error) {
521+
void JSQuicSessionListener::OnSessionClose(QuicError error, int flags) {
529522
Environment* env = session()->env();
530523
HandleScope scope(env->isolate());
531524
Context::Scope context_scope(env->context());
532525

533526
Local<Value> argv[] = {
534527
Number::New(env->isolate(), static_cast<double>(error.code)),
535-
Integer::New(env->isolate(), error.family)
528+
Integer::New(env->isolate(), error.family),
529+
flags & SESSION_CLOSE_FLAG_SILENT
530+
? v8::True(env->isolate())
531+
: v8::False(env->isolate()),
532+
flags & SESSION_CLOSE_FLAG_STATELESS_RESET
533+
? v8::True(env->isolate())
534+
: v8::False(env->isolate())
536535
};
537536

538537
// Grab a shared pointer to this to prevent the QuicSession
@@ -664,26 +663,6 @@ void JSQuicSessionListener::OnSessionTicket(int size, SSL_SESSION* sess) {
664663
arraysize(argv), argv);
665664
}
666665

667-
void JSQuicSessionListener::OnSessionSilentClose(
668-
bool stateless_reset,
669-
QuicError error) {
670-
Environment* env = session()->env();
671-
HandleScope scope(env->isolate());
672-
Context::Scope context_scope(env->context());
673-
674-
Local<Value> argv[] = {
675-
stateless_reset ? v8::True(env->isolate()) : v8::False(env->isolate()),
676-
Number::New(env->isolate(), static_cast<double>(error.code)),
677-
Integer::New(env->isolate(), error.family)
678-
};
679-
680-
// Grab a shared pointer to this to prevent the QuicSession
681-
// from being freed while the MakeCallback is running.
682-
BaseObjectPtr<QuicSession> ptr(session());
683-
session()->MakeCallback(
684-
env->quic_on_session_silent_close_function(), arraysize(argv), argv);
685-
}
686-
687666
void JSQuicSessionListener::OnUsePreferredAddress(
688667
int family,
689668
const PreferredAddress& preferred_address) {
@@ -2377,7 +2356,10 @@ void QuicSession::SilentClose() {
23772356
err.code,
23782357
is_stateless_reset() ? "yes" : "no");
23792358

2380-
listener()->OnSessionSilentClose(is_stateless_reset(), err);
2359+
int flags = QuicSessionListener::SESSION_CLOSE_FLAG_SILENT;
2360+
if (is_stateless_reset())
2361+
flags |= QuicSessionListener::SESSION_CLOSE_FLAG_STATELESS_RESET;
2362+
listener()->OnSessionClose(err, flags);
23812363
}
23822364
// Begin connection close by serializing the CONNECTION_CLOSE packet.
23832365
// There are two variants: one to serialize an application close, the

‎src/quic/node_quic_session.h

+12-6
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,12 @@ struct QuicSessionStatsTraits {
260260

261261
class QuicSessionListener {
262262
public:
263+
enum SessionCloseFlags {
264+
SESSION_CLOSE_FLAG_NONE,
265+
SESSION_CLOSE_FLAG_SILENT,
266+
SESSION_CLOSE_FLAG_STATELESS_RESET
267+
};
268+
263269
virtual ~QuicSessionListener();
264270

265271
virtual void OnKeylog(const char* str, size_t size);
@@ -280,7 +286,9 @@ class QuicSessionListener {
280286
int64_t stream_id,
281287
uint64_t app_error_code);
282288
virtual void OnSessionDestroyed();
283-
virtual void OnSessionClose(QuicError error);
289+
virtual void OnSessionClose(
290+
QuicError error,
291+
int flags = SESSION_CLOSE_FLAG_NONE);
284292
virtual void OnStreamReady(BaseObjectPtr<QuicStream> stream);
285293
virtual void OnHandshakeCompleted();
286294
virtual void OnPathValidation(
@@ -291,9 +299,6 @@ class QuicSessionListener {
291299
int family,
292300
const PreferredAddress& preferred_address);
293301
virtual void OnSessionTicket(int size, SSL_SESSION* session);
294-
virtual void OnSessionSilentClose(
295-
bool stateless_reset,
296-
QuicError error);
297302
virtual void OnStreamBlocked(int64_t stream_id);
298303
virtual void OnVersionNegotiation(
299304
uint32_t supported_version,
@@ -329,15 +334,16 @@ class JSQuicSessionListener : public QuicSessionListener {
329334
int64_t stream_id,
330335
uint64_t app_error_code) override;
331336
void OnSessionDestroyed() override;
332-
void OnSessionClose(QuicError error) override;
337+
void OnSessionClose(
338+
QuicError error,
339+
int flags = SESSION_CLOSE_FLAG_NONE) override;
333340
void OnStreamReady(BaseObjectPtr<QuicStream> stream) override;
334341
void OnHandshakeCompleted() override;
335342
void OnPathValidation(
336343
ngtcp2_path_validation_result res,
337344
const sockaddr* local,
338345
const sockaddr* remote) override;
339346
void OnSessionTicket(int size, SSL_SESSION* session) override;
340-
void OnSessionSilentClose(bool stateless_reset, QuicError error) override;
341347
void OnUsePreferredAddress(
342348
int family,
343349
const PreferredAddress& preferred_address) override;

0 commit comments

Comments
 (0)