diff --git a/lib/internal/quic/core.js b/lib/internal/quic/core.js index a052ba6e2f3d03..446bec3761374b 100644 --- a/lib/internal/quic/core.js +++ b/lib/internal/quic/core.js @@ -147,6 +147,7 @@ const { IDX_QUIC_SESSION_STATS_STREAMS_OUT_COUNT, IDX_QUIC_SESSION_STATS_KEYUPDATE_COUNT, IDX_QUIC_SESSION_STATS_LOSS_RETRANSMIT_COUNT, + IDX_QUIC_SESSION_STATS_HANDSHAKE_COMPLETED_AT, IDX_QUIC_SESSION_STATS_ACK_DELAY_RETRANSMIT_COUNT, IDX_QUIC_SESSION_STATS_MAX_BYTES_IN_FLIGHT, IDX_QUIC_SESSION_STATS_BLOCK_COUNT, @@ -625,6 +626,13 @@ function onRemoveListener(event) { toggleListeners(this[kHandle], event, false); } +function getStats(obj, idx) { + const stats = obj[kHandle]?.stats || obj[kInternalState].stats; + // If stats is undefined at this point, it's just a bug + assert(stats); + return stats[idx]; +} + // QuicEndpoint wraps a UDP socket and is owned // by a QuicSocket. It does not exist independently // of the QuicSocket. @@ -1524,77 +1532,65 @@ class QuicSocket extends EventEmitter { this[kHandle].setServerBusy(on); } + get serverBusy() { + return this[kInternalState].serverBusy; + } + get duration() { // TODO(@jasnell): If the object is destroyed, it should // use a fixed duration rather than calculating from now - const now = process.hrtime.bigint(); - const stats = this[kInternalState].stats || this[kHandle].stats; - return now - stats[IDX_QUIC_SOCKET_STATS_CREATED_AT]; + return process.hrtime.bigint() - + getStats(this, IDX_QUIC_SOCKET_STATS_CREATED_AT); } get boundDuration() { // TODO(@jasnell): If the object is destroyed, it should // use a fixed duration rather than calculating from now - const now = process.hrtime.bigint(); - const stats = this[kInternalState].stats || this[kHandle].stats; - return now - stats[IDX_QUIC_SOCKET_STATS_BOUND_AT]; + return process.hrtime.bigint() - + getStats(this, IDX_QUIC_SOCKET_STATS_BOUND_AT); } get listenDuration() { // TODO(@jasnell): If the object is destroyed, it should // use a fixed duration rather than calculating from now - const now = process.hrtime.bigint(); - const stats = this[kInternalState].stats || this[kHandle].stats; - return now - stats[IDX_QUIC_SOCKET_STATS_LISTEN_AT]; + return process.hrtime.bigint() - + getStats(this, IDX_QUIC_SOCKET_STATS_LISTEN_AT); } get bytesReceived() { - const stats = this[kInternalState].stats || this[kHandle].stats; - return stats[IDX_QUIC_SOCKET_STATS_BYTES_RECEIVED]; + return getStats(this, IDX_QUIC_SOCKET_STATS_BYTES_RECEIVED); } get bytesSent() { - const stats = this[kInternalState].stats || this[kHandle].stats; - return stats[IDX_QUIC_SOCKET_STATS_BYTES_SENT]; + return getStats(this, IDX_QUIC_SOCKET_STATS_BYTES_SENT); } get packetsReceived() { - const stats = this[kInternalState].stats || this[kHandle].stats; - return stats[IDX_QUIC_SOCKET_STATS_PACKETS_RECEIVED]; + return getStats(this, IDX_QUIC_SOCKET_STATS_PACKETS_RECEIVED); } get packetsSent() { - const stats = this[kInternalState].stats || this[kHandle].stats; - return stats[IDX_QUIC_SOCKET_STATS_PACKETS_SENT]; + return getStats(this, IDX_QUIC_SOCKET_STATS_PACKETS_SENT); } get packetsIgnored() { - const stats = this[kInternalState].stats || this[kHandle].stats; - return stats[IDX_QUIC_SOCKET_STATS_PACKETS_IGNORED]; - } - - get serverBusy() { - return this[kInternalState].serverBusy; + return getStats(this, IDX_QUIC_SOCKET_STATS_PACKETS_IGNORED); } get serverSessions() { - const stats = this[kInternalState].stats || this[kHandle].stats; - return stats[IDX_QUIC_SOCKET_STATS_SERVER_SESSIONS]; + return getStats(this, IDX_QUIC_SOCKET_STATS_SERVER_SESSIONS); } get clientSessions() { - const stats = this[kInternalState].stats || this[kHandle].stats; - return stats[IDX_QUIC_SOCKET_STATS_CLIENT_SESSIONS]; + return getStats(this, IDX_QUIC_SOCKET_STATS_CLIENT_SESSIONS); } get statelessResetCount() { - const stats = this[kInternalState].stats || this[kHandle].stats; - return stats[IDX_QUIC_SOCKET_STATS_STATELESS_RESET_COUNT]; + return getStats(this, IDX_QUIC_SOCKET_STATS_STATELESS_RESET_COUNT); } get serverBusyCount() { - const stats = this[kInternalState].stats || this[kHandle].stats; - return stats[IDX_QUIC_SOCKET_STATS_SERVER_BUSY_COUNT]; + return getStats(this, IDX_QUIC_SOCKET_STATS_SERVER_BUSY_COUNT); } // Diagnostic packet loss is a testing mechanism that allows simulating @@ -2183,82 +2179,68 @@ class QuicSession extends EventEmitter { } get duration() { - const now = process.hrtime.bigint(); - const stats = this[kInternalState].stats || this[kHandle].stats; - return now - stats[IDX_QUIC_SESSION_STATS_CREATED_AT]; + return process.hrtime.bigint() - + getStats(this, IDX_QUIC_SESSION_STATS_CREATED_AT); } get handshakeDuration() { - const stats = this[kInternalState].stats || this[kHandle].stats; const end = this.handshakeComplete ? - stats[4] : process.hrtime.bigint(); - return end - stats[IDX_QUIC_SESSION_STATS_HANDSHAKE_START_AT]; + getStats(this, IDX_QUIC_SESSION_STATS_HANDSHAKE_COMPLETED_AT) : + process.hrtime.bigint(); + return end - getStats(this, IDX_QUIC_SESSION_STATS_HANDSHAKE_START_AT); } get bytesReceived() { - const stats = this[kInternalState].stats || this[kHandle].stats; - return stats[IDX_QUIC_SESSION_STATS_BYTES_RECEIVED]; + return getStats(this, IDX_QUIC_SESSION_STATS_BYTES_RECEIVED); } get bytesSent() { - const stats = this[kInternalState].stats || this[kHandle].stats; - return stats[IDX_QUIC_SESSION_STATS_BYTES_SENT]; + return getStats(this, IDX_QUIC_SESSION_STATS_BYTES_SENT); } get bidiStreamCount() { - const stats = this[kInternalState].stats || this[kHandle].stats; - return stats[IDX_QUIC_SESSION_STATS_BIDI_STREAM_COUNT]; + return getStats(this, IDX_QUIC_SESSION_STATS_BIDI_STREAM_COUNT); } get uniStreamCount() { - const stats = this[kInternalState].stats || this[kHandle].stats; - return stats[IDX_QUIC_SESSION_STATS_UNI_STREAM_COUNT]; + return getStats(this, IDX_QUIC_SESSION_STATS_UNI_STREAM_COUNT); } get maxInFlightBytes() { - const stats = this[kInternalState].stats || this[kHandle].stats; - return stats[IDX_QUIC_SESSION_STATS_MAX_BYTES_IN_FLIGHT]; + return getStats(this, IDX_QUIC_SESSION_STATS_MAX_BYTES_IN_FLIGHT); } get lossRetransmitCount() { - const stats = this[kInternalState].stats || this[kHandle].stats; - return stats[IDX_QUIC_SESSION_STATS_LOSS_RETRANSMIT_COUNT]; + return getStats(this, IDX_QUIC_SESSION_STATS_LOSS_RETRANSMIT_COUNT); } get ackDelayRetransmitCount() { - const stats = this[kInternalState].stats || this[kHandle].stats; - return stats[IDX_QUIC_SESSION_STATS_ACK_DELAY_RETRANSMIT_COUNT]; + return getStats(this, IDX_QUIC_SESSION_STATS_ACK_DELAY_RETRANSMIT_COUNT); } get peerInitiatedStreamCount() { - const stats = this[kInternalState].stats || this[kHandle].stats; - return stats[IDX_QUIC_SESSION_STATS_STREAMS_IN_COUNT]; + return getStats(this, IDX_QUIC_SESSION_STATS_STREAMS_IN_COUNT); } get selfInitiatedStreamCount() { - const stats = this[kInternalState].stats || this[kHandle].stats; - return stats[IDX_QUIC_SESSION_STATS_STREAMS_OUT_COUNT]; + return getStats(this, IDX_QUIC_SESSION_STATS_STREAMS_OUT_COUNT); } get keyUpdateCount() { - const stats = this[kInternalState].stats || this[kHandle].stats; - return stats[IDX_QUIC_SESSION_STATS_KEYUPDATE_COUNT]; + return getStats(this, IDX_QUIC_SESSION_STATS_KEYUPDATE_COUNT); } get minRTT() { - const stats = this[kInternalState].stats || this[kHandle].stats; - return stats[IDX_QUIC_SESSION_STATS_MIN_RTT]; + return getStats(this, IDX_QUIC_SESSION_STATS_MIN_RTT); } get latestRTT() { - const stats = this[kInternalState].stats || this[kHandle].stats; - return stats[IDX_QUIC_SESSION_STATS_LATEST_RTT]; + return getStats(this, IDX_QUIC_SESSION_STATS_LATEST_RTT); } get smoothedRTT() { - const stats = this[kInternalState].stats || this[kHandle].stats; - return stats[IDX_QUIC_SESSION_STATS_SMOOTHED_RTT]; + return getStats(this, IDX_QUIC_SESSION_STATS_SMOOTHED_RTT); } updateKey() { @@ -3176,39 +3158,32 @@ class QuicStream extends Duplex { } get duration() { - const now = process.hrtime.bigint(); - const stats = this[kInternalState].stats || this[kHandle].stats; - return now - stats[IDX_QUIC_STREAM_STATS_CREATED_AT]; + return process.hrtime.bigint() - + getStats(this, IDX_QUIC_STREAM_STATS_CREATED_AT); } get bytesReceived() { - const stats = this[kInternalState].stats || this[kHandle].stats; - return stats[IDX_QUIC_STREAM_STATS_BYTES_RECEIVED]; + return getStats(this, IDX_QUIC_STREAM_STATS_BYTES_RECEIVED); } get bytesSent() { - const stats = this[kInternalState].stats || this[kHandle].stats; - return stats[IDX_QUIC_STREAM_STATS_BYTES_SENT]; + return getStats(this, IDX_QUIC_STREAM_STATS_BYTES_SENT); } get maxExtendedOffset() { - const stats = this[kInternalState].stats || this[kHandle].stats; - return stats[IDX_QUIC_STREAM_STATS_MAX_OFFSET]; + return getStats(this, IDX_QUIC_STREAM_STATS_MAX_OFFSET); } get finalSize() { - const stats = this[kInternalState].stats || this[kHandle].stats; - return stats[IDX_QUIC_STREAM_STATS_FINAL_SIZE]; + return getStats(this, IDX_QUIC_STREAM_STATS_FINAL_SIZE); } get maxAcknowledgedOffset() { - const stats = this[kInternalState].stats || this[kHandle].stats; - return stats[IDX_QUIC_STREAM_STATS_MAX_OFFSET_ACK]; + return getStats(this, IDX_QUIC_STREAM_STATS_MAX_OFFSET_ACK); } get maxReceivedOffset() { - const stats = this[kInternalState].stats || this[kHandle].stats; - return stats[IDX_QUIC_STREAM_STATS_MAX_OFFSET_RECV]; + return getStats(this, IDX_QUIC_STREAM_STATS_MAX_OFFSET_RECV); } }