Skip to content

Commit c176d5f

Browse files
committed
quic: set destroyed at timestamps for duration calculation
PR-URL: #34262 Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent 48a349e commit c176d5f

File tree

4 files changed

+28
-16
lines changed

4 files changed

+28
-16
lines changed

lib/internal/quic/core.js

+25-16
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ const {
126126
AF_INET6,
127127
NGTCP2_DEFAULT_MAX_PKTLEN,
128128
IDX_QUIC_SESSION_STATS_CREATED_AT,
129+
IDX_QUIC_SESSION_STATS_DESTROYED_AT,
129130
IDX_QUIC_SESSION_STATS_HANDSHAKE_START_AT,
130131
IDX_QUIC_SESSION_STATS_BYTES_RECEIVED,
131132
IDX_QUIC_SESSION_STATS_BYTES_SENT,
@@ -143,13 +144,15 @@ const {
143144
IDX_QUIC_SESSION_STATS_SMOOTHED_RTT,
144145
IDX_QUIC_SESSION_STATS_LATEST_RTT,
145146
IDX_QUIC_STREAM_STATS_CREATED_AT,
147+
IDX_QUIC_STREAM_STATS_DESTROYED_AT,
146148
IDX_QUIC_STREAM_STATS_BYTES_RECEIVED,
147149
IDX_QUIC_STREAM_STATS_BYTES_SENT,
148150
IDX_QUIC_STREAM_STATS_MAX_OFFSET,
149151
IDX_QUIC_STREAM_STATS_FINAL_SIZE,
150152
IDX_QUIC_STREAM_STATS_MAX_OFFSET_ACK,
151153
IDX_QUIC_STREAM_STATS_MAX_OFFSET_RECV,
152154
IDX_QUIC_SOCKET_STATS_CREATED_AT,
155+
IDX_QUIC_SOCKET_STATS_DESTROYED_AT,
153156
IDX_QUIC_SOCKET_STATS_BOUND_AT,
154157
IDX_QUIC_SOCKET_STATS_LISTEN_AT,
155158
IDX_QUIC_SOCKET_STATS_BYTES_RECEIVED,
@@ -1433,6 +1436,9 @@ class QuicSocket extends EventEmitter {
14331436

14341437
// Mark the QuicSocket as being destroyed.
14351438
state.state = kSocketDestroyed;
1439+
this[kHandle].stats[IDX_QUIC_SOCKET_STATS_DESTROYED_AT] =
1440+
process.hrtime.bigint();
1441+
state.stats = new BigInt64Array(this[kHandle].stats);
14361442

14371443
// Immediately close any sessions that may be remaining.
14381444
// If the udp socket is in a state where it is able to do so,
@@ -1531,24 +1537,21 @@ class QuicSocket extends EventEmitter {
15311537
}
15321538

15331539
get duration() {
1534-
// TODO(@jasnell): If the object is destroyed, it should
1535-
// use a fixed duration rather than calculating from now
1536-
return Number(process.hrtime.bigint() -
1537-
getStats(this, IDX_QUIC_SOCKET_STATS_CREATED_AT));
1540+
const end = getStats(this, IDX_QUIC_SOCKET_STATS_DESTROYED_AT) ||
1541+
process.hrtime.bigint();
1542+
return Number(end - getStats(this, IDX_QUIC_SOCKET_STATS_CREATED_AT));
15381543
}
15391544

15401545
get boundDuration() {
1541-
// TODO(@jasnell): If the object is destroyed, it should
1542-
// use a fixed duration rather than calculating from now
1543-
return Number(process.hrtime.bigint() -
1544-
getStats(this, IDX_QUIC_SOCKET_STATS_BOUND_AT));
1546+
const end = getStats(this, IDX_QUIC_SOCKET_STATS_DESTROYED_AT) ||
1547+
process.hrtime.bigint();
1548+
return Number(end - getStats(this, IDX_QUIC_SOCKET_STATS_BOUND_AT));
15451549
}
15461550

15471551
get listenDuration() {
1548-
// TODO(@jasnell): If the object is destroyed, it should
1549-
// use a fixed duration rather than calculating from now
1550-
return Number(process.hrtime.bigint() -
1551-
getStats(this, IDX_QUIC_SOCKET_STATS_LISTEN_AT));
1552+
const end = getStats(this, IDX_QUIC_SOCKET_STATS_DESTROYED_AT) ||
1553+
process.hrtime.bigint();
1554+
return Number(end - getStats(this, IDX_QUIC_SOCKET_STATS_LISTEN_AT));
15521555
}
15531556

15541557
get bytesReceived() {
@@ -1920,6 +1923,8 @@ class QuicSession extends EventEmitter {
19201923
this[kHandle] = undefined;
19211924
if (handle !== undefined) {
19221925
// Copy the stats for use after destruction
1926+
handle.stats[IDX_QUIC_SESSION_STATS_DESTROYED_AT] =
1927+
process.hrtime.bigint();
19231928
state.stats = new BigInt64Array(handle.stats);
19241929
state.idleTimeout = this[kInternalState].state.idleTimeout;
19251930

@@ -2148,8 +2153,9 @@ class QuicSession extends EventEmitter {
21482153
}
21492154

21502155
get duration() {
2151-
return Number(process.hrtime.bigint() -
2152-
getStats(this, IDX_QUIC_SESSION_STATS_CREATED_AT));
2156+
const end = getStats(this, IDX_QUIC_SESSION_STATS_DESTROYED_AT) ||
2157+
process.hrtime.bigint();
2158+
return Number(end - getStats(this, IDX_QUIC_SESSION_STATS_CREATED_AT));
21532159
}
21542160

21552161
get handshakeDuration() {
@@ -2972,6 +2978,8 @@ class QuicStream extends Duplex {
29722978
// object has been destroyed. Any attempt to use the object
29732979
// will segfault and crash the process.
29742980
if (handle !== undefined) {
2981+
handle.stats[IDX_QUIC_STREAM_STATS_DESTROYED_AT] =
2982+
process.hrtime.bigint();
29752983
state.stats = new BigInt64Array(handle.stats);
29762984
handle.destroy();
29772985
}
@@ -3139,8 +3147,9 @@ class QuicStream extends Duplex {
31393147
}
31403148

31413149
get duration() {
3142-
return Number(process.hrtime.bigint() -
3143-
getStats(this, IDX_QUIC_STREAM_STATS_CREATED_AT));
3150+
const end = getStats(this, IDX_QUIC_STREAM_STATS_DESTROYED_AT) ||
3151+
process.hrtime.bigint();
3152+
return Number(end - getStats(this, IDX_QUIC_STREAM_STATS_CREATED_AT));
31443153
}
31453154

31463155
get bytesReceived() {

src/quic/node_quic_session.h

+1
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ enum QuicSessionStateFields {
182182
V(SENT_AT, sent_at, "Last Sent At") \
183183
V(RECEIVED_AT, received_at, "Last Received At") \
184184
V(CLOSING_AT, closing_at, "Closing") \
185+
V(DESTROYED_AT, destroyed_at, "Destroyed At") \
185186
V(BYTES_RECEIVED, bytes_received, "Bytes Received") \
186187
V(BYTES_SENT, bytes_sent, "Bytes Sent") \
187188
V(BIDI_STREAM_COUNT, bidi_stream_count, "Bidi Stream Count") \

src/quic/node_quic_socket.h

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ enum QuicSocketStateFields {
7171
V(CREATED_AT, created_at, "Created At") \
7272
V(BOUND_AT, bound_at, "Bound At") \
7373
V(LISTEN_AT, listen_at, "Listen At") \
74+
V(DESTROYED_AT, destroyed_at, "Destroyed At") \
7475
V(BYTES_RECEIVED, bytes_received, "Bytes Received") \
7576
V(BYTES_SENT, bytes_sent, "Bytes Sent") \
7677
V(PACKETS_RECEIVED, packets_received, "Packets Received") \

src/quic/node_quic_stream.h

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ enum QuicStreamHeadersKind : int {
5050
V(RECEIVED_AT, received_at, "Last Received At") \
5151
V(ACKED_AT, acked_at, "Last Acknowledged At") \
5252
V(CLOSING_AT, closing_at, "Closing At") \
53+
V(DESTROYED_AT, destroyed_at, "Destroyed At") \
5354
V(BYTES_RECEIVED, bytes_received, "Bytes Received") \
5455
V(BYTES_SENT, bytes_sent, "Bytes Sent") \
5556
V(MAX_OFFSET, max_offset, "Max Offset") \

0 commit comments

Comments
 (0)