mirror of
https://github.com/nodejs/node.git
synced 2026-11-25 23:01:06 -05:00
src, lib: add stats to dtls
PR-URL: https://github.com/nodejs/node/pull/63182 Fixes: https://github.com/nodejs/node/issues/61630 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
This commit is contained in:
1 parent
889854f18b
commit
0c24639a5a
10 files changed
+798
-7
No files matched your search
+206
@@ -178,6 +178,17 @@ Shared state object with properties:
|
||||
* `sessionCount` {number}
|
||||
* `busy` {boolean}
|
||||
|
||||
### `endpoint.stats`
|
||||
|
||||
<!-- YAML
|
||||
added: REPLACEME
|
||||
-->
|
||||
|
||||
* Type: {DTLSEndpoint.Stats}
|
||||
|
||||
The statistics collected for this endpoint. Read only. The stats object is
|
||||
live and updated by the C++ internals as data flows through the endpoint.
|
||||
|
||||
### `endpoint.busy`
|
||||
|
||||
* {boolean}
|
||||
@@ -204,6 +215,99 @@ Immediately destroys the endpoint without sending `close_notify` alerts.
|
||||
|
||||
Equivalent to calling `endpoint.close()`.
|
||||
|
||||
## Class: `DTLSEndpoint.Stats`
|
||||
|
||||
<!-- YAML
|
||||
added: REPLACEME
|
||||
-->
|
||||
|
||||
A view of the collected statistics for an endpoint.
|
||||
|
||||
### `endpointStats.createdAt`
|
||||
|
||||
<!-- YAML
|
||||
added: REPLACEME
|
||||
-->
|
||||
|
||||
* Type: {bigint} A timestamp indicating when the endpoint was created. Read only.
|
||||
|
||||
### `endpointStats.destroyedAt`
|
||||
|
||||
<!-- YAML
|
||||
added: REPLACEME
|
||||
-->
|
||||
|
||||
* Type: {bigint} A timestamp indicating when the endpoint was destroyed. Read only.
|
||||
|
||||
### `endpointStats.bytesReceived`
|
||||
|
||||
<!-- YAML
|
||||
added: REPLACEME
|
||||
-->
|
||||
|
||||
* Type: {bigint} The total number of bytes received by this endpoint. Read only.
|
||||
|
||||
### `endpointStats.bytesSent`
|
||||
|
||||
<!-- YAML
|
||||
added: REPLACEME
|
||||
-->
|
||||
|
||||
* Type: {bigint} The total number of bytes sent by this endpoint. Read only.
|
||||
|
||||
### `endpointStats.packetsReceived`
|
||||
|
||||
<!-- YAML
|
||||
added: REPLACEME
|
||||
-->
|
||||
|
||||
* Type: {bigint} The total number of UDP packets received by this endpoint. Read only.
|
||||
|
||||
### `endpointStats.packetsSent`
|
||||
|
||||
<!-- YAML
|
||||
added: REPLACEME
|
||||
-->
|
||||
|
||||
* Type: {bigint} The total number of UDP packets sent by this endpoint. Read only.
|
||||
|
||||
### `endpointStats.serverSessions`
|
||||
|
||||
<!-- YAML
|
||||
added: REPLACEME
|
||||
-->
|
||||
|
||||
* Type: {bigint} The total number of peer-initiated sessions accepted by this
|
||||
endpoint. Read only.
|
||||
|
||||
### `endpointStats.clientSessions`
|
||||
|
||||
<!-- YAML
|
||||
added: REPLACEME
|
||||
-->
|
||||
|
||||
* Type: {bigint} The total number of sessions initiated by this endpoint. Read only.
|
||||
|
||||
### `endpointStats.serverBusyCount`
|
||||
|
||||
<!-- YAML
|
||||
added: REPLACEME
|
||||
-->
|
||||
|
||||
* Type: {bigint} The total number of incoming connections rejected because the
|
||||
endpoint was marked busy. Read only.
|
||||
|
||||
### `endpointStats.isConnected`
|
||||
|
||||
<!-- YAML
|
||||
added: REPLACEME
|
||||
-->
|
||||
|
||||
* Type: {boolean}
|
||||
|
||||
`true` if the stats object is still connected to the underlying endpoint.
|
||||
Once the endpoint is destroyed, the stats become a stale snapshot.
|
||||
|
||||
## Class: `DTLSSession`
|
||||
|
||||
<!-- YAML
|
||||
@@ -264,6 +368,17 @@ Immediately destroys the session without sending `close_notify`.
|
||||
|
||||
* Returns: {string|undefined} The negotiated SRTP protection profile name.
|
||||
|
||||
### `session.stats`
|
||||
|
||||
<!-- YAML
|
||||
added: REPLACEME
|
||||
-->
|
||||
|
||||
* Type: {DTLSSession.Stats}
|
||||
|
||||
The statistics collected for this session. Read only. The stats object is
|
||||
live and updated as data flows through the session.
|
||||
|
||||
### `session.exportKeyingMaterial(length, label[, context])`
|
||||
|
||||
* `length` {number} Number of bytes to export.
|
||||
@@ -275,6 +390,97 @@ Exports keying material from the DTLS session, as defined in
|
||||
[RFC 5705][]. This is commonly used with DTLS-SRTP to derive
|
||||
encryption keys for media streams.
|
||||
|
||||
## Class: `DTLSSession.Stats`
|
||||
|
||||
<!-- YAML
|
||||
added: REPLACEME
|
||||
-->
|
||||
|
||||
A view of the collected statistics for a session.
|
||||
|
||||
### `sessionStats.createdAt`
|
||||
|
||||
<!-- YAML
|
||||
added: REPLACEME
|
||||
-->
|
||||
|
||||
* Type: {bigint} A timestamp indicating when the session was created. Read only.
|
||||
|
||||
### `sessionStats.destroyedAt`
|
||||
|
||||
<!-- YAML
|
||||
added: REPLACEME
|
||||
-->
|
||||
|
||||
* Type: {bigint} A timestamp indicating when the session was destroyed. Read only.
|
||||
|
||||
### `sessionStats.closingAt`
|
||||
|
||||
<!-- YAML
|
||||
added: REPLACEME
|
||||
-->
|
||||
|
||||
* Type: {bigint} A timestamp indicating when `close()` was called. Read only.
|
||||
|
||||
### `sessionStats.handshakeCompletedAt`
|
||||
|
||||
<!-- YAML
|
||||
added: REPLACEME
|
||||
-->
|
||||
|
||||
* Type: {bigint} A timestamp indicating when the DTLS handshake completed. Read only.
|
||||
|
||||
### `sessionStats.bytesReceived`
|
||||
|
||||
<!-- YAML
|
||||
added: REPLACEME
|
||||
-->
|
||||
|
||||
* Type: {bigint} The total number of application data bytes received. Read only.
|
||||
|
||||
### `sessionStats.bytesSent`
|
||||
|
||||
<!-- YAML
|
||||
added: REPLACEME
|
||||
-->
|
||||
|
||||
* Type: {bigint} The total number of application data bytes sent. Read only.
|
||||
|
||||
### `sessionStats.messagesReceived`
|
||||
|
||||
<!-- YAML
|
||||
added: REPLACEME
|
||||
-->
|
||||
|
||||
* Type: {bigint} The total number of application messages received. Read only.
|
||||
|
||||
### `sessionStats.messagesSent`
|
||||
|
||||
<!-- YAML
|
||||
added: REPLACEME
|
||||
-->
|
||||
|
||||
* Type: {bigint} The total number of application messages sent. Read only.
|
||||
|
||||
### `sessionStats.retransmitCount`
|
||||
|
||||
<!-- YAML
|
||||
added: REPLACEME
|
||||
-->
|
||||
|
||||
* Type: {bigint} The total number of DTLS handshake retransmissions. Read only.
|
||||
|
||||
### `sessionStats.isConnected`
|
||||
|
||||
<!-- YAML
|
||||
added: REPLACEME
|
||||
-->
|
||||
|
||||
* Type: {boolean}
|
||||
|
||||
`true` if the stats object is still connected to the underlying session.
|
||||
Once the session is destroyed, the stats become a stale snapshot.
|
||||
|
||||
### Callback properties
|
||||
|
||||
#### `session.onmessage`
|
||||
|
||||
@@ -42,6 +42,11 @@ const {
|
||||
DTLSSessionState,
|
||||
} = require('internal/dtls/state');
|
||||
|
||||
const {
|
||||
DTLSEndpointStats,
|
||||
DTLSSessionStats,
|
||||
} = require('internal/dtls/stats');
|
||||
|
||||
const {
|
||||
kOwner,
|
||||
kPrivateConstructor,
|
||||
@@ -70,6 +75,7 @@ class DTLSSession {
|
||||
#handle;
|
||||
#endpoint;
|
||||
#state;
|
||||
#stats;
|
||||
#pendingOpen;
|
||||
#pendingClose;
|
||||
#onmessage;
|
||||
@@ -88,6 +94,8 @@ class DTLSSession {
|
||||
this.#endpoint = endpoint;
|
||||
this.#state = new DTLSSessionState(
|
||||
kPrivateConstructor, handle.getState());
|
||||
this.#stats = new DTLSSessionStats(
|
||||
kPrivateConstructor, handle.getStats());
|
||||
this.#pendingOpen = PromiseWithResolvers();
|
||||
this.#pendingClose = PromiseWithResolvers();
|
||||
}
|
||||
@@ -218,6 +226,7 @@ class DTLSSession {
|
||||
}
|
||||
|
||||
get state() { return this.#state; }
|
||||
get stats() { return this.#stats; }
|
||||
get endpoint() { return this.#endpoint; }
|
||||
|
||||
exportKeyingMaterial(length, label, context) {
|
||||
@@ -287,6 +296,7 @@ class DTLSSession {
|
||||
class DTLSEndpoint {
|
||||
#handle;
|
||||
#state;
|
||||
#stats;
|
||||
#sessions = new SafeSet();
|
||||
#pendingClose;
|
||||
#onsession;
|
||||
@@ -297,6 +307,8 @@ class DTLSEndpoint {
|
||||
this.#handle[kOwner] = this;
|
||||
this.#state = new DTLSEndpointState(
|
||||
kPrivateConstructor, this.#handle.getState());
|
||||
this.#stats = new DTLSEndpointStats(
|
||||
kPrivateConstructor, this.#handle.getStats());
|
||||
this.#pendingClose = PromiseWithResolvers();
|
||||
|
||||
if (options.mtu !== undefined) {
|
||||
@@ -392,6 +404,7 @@ class DTLSEndpoint {
|
||||
}
|
||||
|
||||
get state() { return this.#state; }
|
||||
get stats() { return this.#stats; }
|
||||
get sessions() { return this.#sessions; }
|
||||
|
||||
get onerror() { return this.#onerror; }
|
||||
|
||||
+296
-4
@@ -1,7 +1,9 @@
|
||||
'use strict';
|
||||
|
||||
// Placeholder for DTLS statistics tracking.
|
||||
// Will be expanded as the implementation matures.
|
||||
const {
|
||||
BigUint64Array,
|
||||
JSONStringify,
|
||||
} = primordials;
|
||||
|
||||
const {
|
||||
getOptionValue,
|
||||
@@ -11,29 +13,319 @@ if (!process.features.dtls || !getOptionValue('--experimental-dtls')) {
|
||||
return;
|
||||
}
|
||||
|
||||
const {
|
||||
isArrayBuffer,
|
||||
} = require('util/types');
|
||||
|
||||
const {
|
||||
codes: {
|
||||
ERR_ILLEGAL_CONSTRUCTOR,
|
||||
ERR_INVALID_ARG_TYPE,
|
||||
},
|
||||
} = require('internal/errors');
|
||||
|
||||
const { inspect } = require('internal/util/inspect');
|
||||
const assert = require('internal/assert');
|
||||
|
||||
const {
|
||||
kFinishClose,
|
||||
kPrivateConstructor,
|
||||
} = require('internal/dtls/symbols');
|
||||
|
||||
// This file defines the helper objects for accessing statistics collected
|
||||
// by DTLS endpoints and sessions. Each wraps a BigUint64Array backed by
|
||||
// a shared ArrayBuffer that is updated by the C++ internals.
|
||||
|
||||
const {
|
||||
IDX_STATS_ENDPOINT_CREATED_AT,
|
||||
IDX_STATS_ENDPOINT_DESTROYED_AT,
|
||||
IDX_STATS_ENDPOINT_BYTES_RECEIVED,
|
||||
IDX_STATS_ENDPOINT_BYTES_SENT,
|
||||
IDX_STATS_ENDPOINT_PACKETS_RECEIVED,
|
||||
IDX_STATS_ENDPOINT_PACKETS_SENT,
|
||||
IDX_STATS_ENDPOINT_SERVER_SESSIONS,
|
||||
IDX_STATS_ENDPOINT_CLIENT_SESSIONS,
|
||||
IDX_STATS_ENDPOINT_SERVER_BUSY_COUNT,
|
||||
|
||||
IDX_STATS_SESSION_CREATED_AT,
|
||||
IDX_STATS_SESSION_DESTROYED_AT,
|
||||
IDX_STATS_SESSION_CLOSING_AT,
|
||||
IDX_STATS_SESSION_HANDSHAKE_COMPLETED_AT,
|
||||
IDX_STATS_SESSION_BYTES_RECEIVED,
|
||||
IDX_STATS_SESSION_BYTES_SENT,
|
||||
IDX_STATS_SESSION_MESSAGES_RECEIVED,
|
||||
IDX_STATS_SESSION_MESSAGES_SENT,
|
||||
IDX_STATS_SESSION_RETRANSMIT_COUNT,
|
||||
} = internalBinding('dtls');
|
||||
|
||||
assert(IDX_STATS_ENDPOINT_CREATED_AT !== undefined);
|
||||
assert(IDX_STATS_ENDPOINT_DESTROYED_AT !== undefined);
|
||||
assert(IDX_STATS_ENDPOINT_BYTES_RECEIVED !== undefined);
|
||||
assert(IDX_STATS_ENDPOINT_BYTES_SENT !== undefined);
|
||||
assert(IDX_STATS_ENDPOINT_PACKETS_RECEIVED !== undefined);
|
||||
assert(IDX_STATS_ENDPOINT_PACKETS_SENT !== undefined);
|
||||
assert(IDX_STATS_ENDPOINT_SERVER_SESSIONS !== undefined);
|
||||
assert(IDX_STATS_ENDPOINT_CLIENT_SESSIONS !== undefined);
|
||||
assert(IDX_STATS_ENDPOINT_SERVER_BUSY_COUNT !== undefined);
|
||||
assert(IDX_STATS_SESSION_CREATED_AT !== undefined);
|
||||
assert(IDX_STATS_SESSION_DESTROYED_AT !== undefined);
|
||||
assert(IDX_STATS_SESSION_CLOSING_AT !== undefined);
|
||||
assert(IDX_STATS_SESSION_HANDSHAKE_COMPLETED_AT !== undefined);
|
||||
assert(IDX_STATS_SESSION_BYTES_RECEIVED !== undefined);
|
||||
assert(IDX_STATS_SESSION_BYTES_SENT !== undefined);
|
||||
assert(IDX_STATS_SESSION_MESSAGES_RECEIVED !== undefined);
|
||||
assert(IDX_STATS_SESSION_MESSAGES_SENT !== undefined);
|
||||
assert(IDX_STATS_SESSION_RETRANSMIT_COUNT !== undefined);
|
||||
|
||||
class DTLSEndpointStats {
|
||||
constructor(privateSymbol) {
|
||||
/** @type {BigUint64Array} */
|
||||
#handle;
|
||||
/** @type {boolean} */
|
||||
#disconnected = false;
|
||||
|
||||
/**
|
||||
* @param {symbol} privateSymbol
|
||||
* @param {ArrayBuffer} buffer
|
||||
*/
|
||||
constructor(privateSymbol, buffer) {
|
||||
if (privateSymbol !== kPrivateConstructor) {
|
||||
throw new ERR_ILLEGAL_CONSTRUCTOR();
|
||||
}
|
||||
if (!isArrayBuffer(buffer)) {
|
||||
throw new ERR_INVALID_ARG_TYPE('buffer', ['ArrayBuffer'], buffer);
|
||||
}
|
||||
this.#handle = new BigUint64Array(buffer);
|
||||
}
|
||||
|
||||
/** @type {bigint} */
|
||||
get createdAt() {
|
||||
return this.#handle[IDX_STATS_ENDPOINT_CREATED_AT];
|
||||
}
|
||||
|
||||
/** @type {bigint} */
|
||||
get destroyedAt() {
|
||||
return this.#handle[IDX_STATS_ENDPOINT_DESTROYED_AT];
|
||||
}
|
||||
|
||||
/** @type {bigint} */
|
||||
get bytesReceived() {
|
||||
return this.#handle[IDX_STATS_ENDPOINT_BYTES_RECEIVED];
|
||||
}
|
||||
|
||||
/** @type {bigint} */
|
||||
get bytesSent() {
|
||||
return this.#handle[IDX_STATS_ENDPOINT_BYTES_SENT];
|
||||
}
|
||||
|
||||
/** @type {bigint} */
|
||||
get packetsReceived() {
|
||||
return this.#handle[IDX_STATS_ENDPOINT_PACKETS_RECEIVED];
|
||||
}
|
||||
|
||||
/** @type {bigint} */
|
||||
get packetsSent() {
|
||||
return this.#handle[IDX_STATS_ENDPOINT_PACKETS_SENT];
|
||||
}
|
||||
|
||||
/** @type {bigint} */
|
||||
get serverSessions() {
|
||||
return this.#handle[IDX_STATS_ENDPOINT_SERVER_SESSIONS];
|
||||
}
|
||||
|
||||
/** @type {bigint} */
|
||||
get clientSessions() {
|
||||
return this.#handle[IDX_STATS_ENDPOINT_CLIENT_SESSIONS];
|
||||
}
|
||||
|
||||
/** @type {bigint} */
|
||||
get serverBusyCount() {
|
||||
return this.#handle[IDX_STATS_ENDPOINT_SERVER_BUSY_COUNT];
|
||||
}
|
||||
|
||||
toString() {
|
||||
return JSONStringify(this.toJSON());
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
return {
|
||||
__proto__: null,
|
||||
connected: this.isConnected,
|
||||
createdAt: `${this.createdAt}`,
|
||||
destroyedAt: `${this.destroyedAt}`,
|
||||
bytesReceived: `${this.bytesReceived}`,
|
||||
bytesSent: `${this.bytesSent}`,
|
||||
packetsReceived: `${this.packetsReceived}`,
|
||||
packetsSent: `${this.packetsSent}`,
|
||||
serverSessions: `${this.serverSessions}`,
|
||||
clientSessions: `${this.clientSessions}`,
|
||||
serverBusyCount: `${this.serverBusyCount}`,
|
||||
};
|
||||
}
|
||||
|
||||
[inspect.custom](depth, options) {
|
||||
if (depth < 0)
|
||||
return this;
|
||||
|
||||
const opts = {
|
||||
__proto__: null,
|
||||
...options,
|
||||
depth: options.depth == null ? null : options.depth - 1,
|
||||
};
|
||||
|
||||
return `DTLSEndpointStats ${inspect({
|
||||
connected: this.isConnected,
|
||||
createdAt: this.createdAt,
|
||||
destroyedAt: this.destroyedAt,
|
||||
bytesReceived: this.bytesReceived,
|
||||
bytesSent: this.bytesSent,
|
||||
packetsReceived: this.packetsReceived,
|
||||
packetsSent: this.packetsSent,
|
||||
serverSessions: this.serverSessions,
|
||||
clientSessions: this.clientSessions,
|
||||
serverBusyCount: this.serverBusyCount,
|
||||
}, opts)}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* True if this stats object is still connected to the underlying
|
||||
* stats source. If false, the stats are stale.
|
||||
* @type {boolean}
|
||||
*/
|
||||
get isConnected() {
|
||||
return !this.#disconnected;
|
||||
}
|
||||
|
||||
[kFinishClose]() {
|
||||
// Snapshot the stats into a new BigUint64Array since the underlying
|
||||
// buffer will be destroyed.
|
||||
this.#handle = new BigUint64Array(this.#handle);
|
||||
this.#disconnected = true;
|
||||
}
|
||||
}
|
||||
|
||||
class DTLSSessionStats {
|
||||
constructor(privateSymbol) {
|
||||
/** @type {BigUint64Array} */
|
||||
#handle;
|
||||
/** @type {boolean} */
|
||||
#disconnected = false;
|
||||
|
||||
/**
|
||||
* @param {symbol} privateSymbol
|
||||
* @param {ArrayBuffer} buffer
|
||||
*/
|
||||
constructor(privateSymbol, buffer) {
|
||||
if (privateSymbol !== kPrivateConstructor) {
|
||||
throw new ERR_ILLEGAL_CONSTRUCTOR();
|
||||
}
|
||||
if (!isArrayBuffer(buffer)) {
|
||||
throw new ERR_INVALID_ARG_TYPE('buffer', ['ArrayBuffer'], buffer);
|
||||
}
|
||||
this.#handle = new BigUint64Array(buffer);
|
||||
}
|
||||
|
||||
/** @type {bigint} */
|
||||
get createdAt() {
|
||||
return this.#handle[IDX_STATS_SESSION_CREATED_AT];
|
||||
}
|
||||
|
||||
/** @type {bigint} */
|
||||
get destroyedAt() {
|
||||
return this.#handle[IDX_STATS_SESSION_DESTROYED_AT];
|
||||
}
|
||||
|
||||
/** @type {bigint} */
|
||||
get closingAt() {
|
||||
return this.#handle[IDX_STATS_SESSION_CLOSING_AT];
|
||||
}
|
||||
|
||||
/** @type {bigint} */
|
||||
get handshakeCompletedAt() {
|
||||
return this.#handle[IDX_STATS_SESSION_HANDSHAKE_COMPLETED_AT];
|
||||
}
|
||||
|
||||
/** @type {bigint} */
|
||||
get bytesReceived() {
|
||||
return this.#handle[IDX_STATS_SESSION_BYTES_RECEIVED];
|
||||
}
|
||||
|
||||
/** @type {bigint} */
|
||||
get bytesSent() {
|
||||
return this.#handle[IDX_STATS_SESSION_BYTES_SENT];
|
||||
}
|
||||
|
||||
/** @type {bigint} */
|
||||
get messagesReceived() {
|
||||
return this.#handle[IDX_STATS_SESSION_MESSAGES_RECEIVED];
|
||||
}
|
||||
|
||||
/** @type {bigint} */
|
||||
get messagesSent() {
|
||||
return this.#handle[IDX_STATS_SESSION_MESSAGES_SENT];
|
||||
}
|
||||
|
||||
/** @type {bigint} */
|
||||
get retransmitCount() {
|
||||
return this.#handle[IDX_STATS_SESSION_RETRANSMIT_COUNT];
|
||||
}
|
||||
|
||||
toString() {
|
||||
return JSONStringify(this.toJSON());
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
return {
|
||||
__proto__: null,
|
||||
connected: this.isConnected,
|
||||
createdAt: `${this.createdAt}`,
|
||||
destroyedAt: `${this.destroyedAt}`,
|
||||
closingAt: `${this.closingAt}`,
|
||||
handshakeCompletedAt: `${this.handshakeCompletedAt}`,
|
||||
bytesReceived: `${this.bytesReceived}`,
|
||||
bytesSent: `${this.bytesSent}`,
|
||||
messagesReceived: `${this.messagesReceived}`,
|
||||
messagesSent: `${this.messagesSent}`,
|
||||
retransmitCount: `${this.retransmitCount}`,
|
||||
};
|
||||
}
|
||||
|
||||
[inspect.custom](depth, options) {
|
||||
if (depth < 0)
|
||||
return this;
|
||||
|
||||
const opts = {
|
||||
__proto__: null,
|
||||
...options,
|
||||
depth: options.depth == null ? null : options.depth - 1,
|
||||
};
|
||||
|
||||
return `DTLSSessionStats ${inspect({
|
||||
connected: this.isConnected,
|
||||
createdAt: this.createdAt,
|
||||
destroyedAt: this.destroyedAt,
|
||||
closingAt: this.closingAt,
|
||||
handshakeCompletedAt: this.handshakeCompletedAt,
|
||||
bytesReceived: this.bytesReceived,
|
||||
bytesSent: this.bytesSent,
|
||||
messagesReceived: this.messagesReceived,
|
||||
messagesSent: this.messagesSent,
|
||||
retransmitCount: this.retransmitCount,
|
||||
}, opts)}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* True if this stats object is still connected to the underlying
|
||||
* stats source. If false, the stats are stale.
|
||||
* @type {boolean}
|
||||
*/
|
||||
get isConnected() {
|
||||
return !this.#disconnected;
|
||||
}
|
||||
|
||||
[kFinishClose]() {
|
||||
// Snapshot the stats into a new BigUint64Array since the underlying
|
||||
// buffer will be destroyed.
|
||||
this.#handle = new BigUint64Array(this.#handle);
|
||||
this.#disconnected = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -48,6 +48,24 @@ void CreatePerContextProperties(Local<Object> target,
|
||||
NODE_DEFINE_CONSTANT(target, IDX_SESSION_STATE_DESTROYED);
|
||||
NODE_DEFINE_CONSTANT(target, IDX_SESSION_STATE_HAS_MESSAGE_LISTENER);
|
||||
|
||||
// Endpoint stats indices (for BigUint64Array access from JS)
|
||||
#define V(name, _) IDX_STATS_ENDPOINT_##name,
|
||||
enum IDX_STATS_ENDPOINT { DTLS_ENDPOINT_STATS(V) IDX_STATS_ENDPOINT_COUNT };
|
||||
#undef V
|
||||
#define V(name, _) NODE_DEFINE_CONSTANT(target, IDX_STATS_ENDPOINT_##name);
|
||||
DTLS_ENDPOINT_STATS(V);
|
||||
#undef V
|
||||
NODE_DEFINE_CONSTANT(target, IDX_STATS_ENDPOINT_COUNT);
|
||||
|
||||
// Session stats indices
|
||||
#define V(name, _) IDX_STATS_SESSION_##name,
|
||||
enum IDX_STATS_SESSION { DTLS_SESSION_STATS(V) IDX_STATS_SESSION_COUNT };
|
||||
#undef V
|
||||
#define V(name, _) NODE_DEFINE_CONSTANT(target, IDX_STATS_SESSION_##name);
|
||||
DTLS_SESSION_STATS(V);
|
||||
#undef V
|
||||
NODE_DEFINE_CONSTANT(target, IDX_STATS_SESSION_COUNT);
|
||||
|
||||
// SSL verify mode constants
|
||||
constexpr auto SSL_VERIFY_NONE_VALUE = SSL_VERIFY_NONE;
|
||||
constexpr auto SSL_VERIFY_PEER_VALUE = SSL_VERIFY_PEER;
|
||||
|
||||
@@ -6,10 +6,58 @@
|
||||
|
||||
#include <env.h>
|
||||
#include <node_errors.h>
|
||||
#include <uv.h>
|
||||
#include <v8.h>
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace node::dtls {
|
||||
|
||||
// Utilities for updating stats maintained in an AliasedStruct.
|
||||
template <typename Stats, uint64_t Stats::*member>
|
||||
void IncrementStat(Stats* stats, uint64_t amt = 1) {
|
||||
stats->*member += amt;
|
||||
}
|
||||
|
||||
template <typename Stats, uint64_t Stats::*member>
|
||||
void RecordTimestampStat(Stats* stats) {
|
||||
stats->*member = uv_hrtime();
|
||||
}
|
||||
|
||||
#define DTLS_STAT_INCREMENT(Type, name) \
|
||||
IncrementStat<Type, &Type::name>(stats_.Data())
|
||||
#define DTLS_STAT_INCREMENT_N(Type, name, amt) \
|
||||
IncrementStat<Type, &Type::name>(stats_.Data(), amt)
|
||||
#define DTLS_STAT_RECORD_TIMESTAMP(Type, name) \
|
||||
RecordTimestampStat<Type, &Type::name>(stats_.Data())
|
||||
|
||||
#define DTLS_STAT_FIELD(_, name) uint64_t name;
|
||||
|
||||
// ============================================================================
|
||||
// Stats X-macros: V(ENUM_NAME, field_name)
|
||||
|
||||
#define DTLS_ENDPOINT_STATS(V) \
|
||||
V(CREATED_AT, created_at) \
|
||||
V(DESTROYED_AT, destroyed_at) \
|
||||
V(BYTES_RECEIVED, bytes_received) \
|
||||
V(BYTES_SENT, bytes_sent) \
|
||||
V(PACKETS_RECEIVED, packets_received) \
|
||||
V(PACKETS_SENT, packets_sent) \
|
||||
V(SERVER_SESSIONS, server_sessions) \
|
||||
V(CLIENT_SESSIONS, client_sessions) \
|
||||
V(SERVER_BUSY_COUNT, server_busy_count)
|
||||
|
||||
#define DTLS_SESSION_STATS(V) \
|
||||
V(CREATED_AT, created_at) \
|
||||
V(DESTROYED_AT, destroyed_at) \
|
||||
V(CLOSING_AT, closing_at) \
|
||||
V(HANDSHAKE_COMPLETED_AT, handshake_completed_at) \
|
||||
V(BYTES_RECEIVED, bytes_received) \
|
||||
V(BYTES_SENT, bytes_sent) \
|
||||
V(MESSAGES_RECEIVED, messages_received) \
|
||||
V(MESSAGES_SENT, messages_sent) \
|
||||
V(RETRANSMIT_COUNT, retransmit_count)
|
||||
|
||||
// State indices shared between C++ and JS via AliasedStruct/DataView.
|
||||
// Keep in sync with lib/internal/dtls/state.js.
|
||||
enum DTLSEndpointStateIndex {
|
||||
|
||||
@@ -50,10 +50,12 @@ DTLSEndpoint::DTLSEndpoint(Environment* env, Local<Object> wrap)
|
||||
wrap,
|
||||
reinterpret_cast<uv_handle_t*>(&handle_),
|
||||
PROVIDER_DTLS_ENDPOINT),
|
||||
state_(env->isolate()) {
|
||||
state_(env->isolate()),
|
||||
stats_(env->isolate()) {
|
||||
CHECK_EQ(uv_udp_init(env->event_loop(), &handle_), 0);
|
||||
handle_.data = this;
|
||||
MakeWeak();
|
||||
DTLS_STAT_RECORD_TIMESTAMP(DTLSEndpointStats, created_at);
|
||||
}
|
||||
|
||||
Local<FunctionTemplate> DTLSEndpoint::GetConstructorTemplate(Environment* env) {
|
||||
@@ -71,6 +73,7 @@ Local<FunctionTemplate> DTLSEndpoint::GetConstructorTemplate(Environment* env) {
|
||||
SetProtoMethod(isolate, tmpl, "close", DoClose);
|
||||
SetProtoMethod(isolate, tmpl, "destroy", DoDestroy);
|
||||
SetProtoMethod(isolate, tmpl, "getState", GetState);
|
||||
SetProtoMethod(isolate, tmpl, "getStats", GetStats);
|
||||
SetProtoMethod(isolate, tmpl, "getAddress", GetAddress);
|
||||
SetProtoMethod(isolate, tmpl, "setMTU", SetMTU);
|
||||
SetProtoMethod(isolate, tmpl, "setCallbacks", DoSetCallbacks);
|
||||
@@ -96,6 +99,7 @@ void DTLSEndpoint::RegisterExternalReferences(
|
||||
registry->Register(DoClose);
|
||||
registry->Register(DoDestroy);
|
||||
registry->Register(GetState);
|
||||
registry->Register(GetStats);
|
||||
registry->Register(GetAddress);
|
||||
registry->Register(SetMTU);
|
||||
registry->Register(DoSetCallbacks);
|
||||
@@ -171,6 +175,7 @@ BaseObjectPtr<DTLSSession> DTLSEndpoint::Connect(DTLSContext* context,
|
||||
|
||||
sessions_[remote] = session;
|
||||
state_->session_count = sessions_.size();
|
||||
DTLS_STAT_INCREMENT(DTLSEndpointStats, client_sessions);
|
||||
|
||||
// Ref the handle while we have sessions.
|
||||
uv_ref(reinterpret_cast<uv_handle_t*>(&handle_));
|
||||
@@ -197,6 +202,8 @@ int DTLSEndpoint::SendTo(const SocketAddress& dest,
|
||||
int err = uv_udp_try_send(&handle_, &buf, 1, dest.data());
|
||||
|
||||
if (err == static_cast<int>(len)) {
|
||||
DTLS_STAT_INCREMENT_N(DTLSEndpointStats, bytes_sent, len);
|
||||
DTLS_STAT_INCREMENT(DTLSEndpointStats, packets_sent);
|
||||
return 0; // Sent successfully.
|
||||
}
|
||||
|
||||
@@ -215,6 +222,8 @@ int DTLSEndpoint::SendTo(const SocketAddress& dest,
|
||||
return err;
|
||||
}
|
||||
|
||||
DTLS_STAT_INCREMENT_N(DTLSEndpointStats, bytes_sent, len);
|
||||
DTLS_STAT_INCREMENT(DTLSEndpointStats, packets_sent);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -358,6 +367,11 @@ void DTLSEndpoint::OnRecv(uv_udp_t* handle,
|
||||
return;
|
||||
}
|
||||
|
||||
IncrementStat<DTLSEndpointStats, &DTLSEndpointStats::bytes_received>(
|
||||
endpoint->stats_.Data(), nread);
|
||||
IncrementStat<DTLSEndpointStats, &DTLSEndpointStats::packets_received>(
|
||||
endpoint->stats_.Data());
|
||||
|
||||
SocketAddress remote(addr);
|
||||
endpoint->ProcessDatagram(
|
||||
reinterpret_cast<const uint8_t*>(buf->base), nread, remote);
|
||||
@@ -373,6 +387,7 @@ void DTLSEndpoint::OnSend(uv_udp_send_t* req, int status) {
|
||||
void DTLSEndpoint::OnClose() {
|
||||
state_->closing = 0;
|
||||
state_->destroyed = 1;
|
||||
DTLS_STAT_RECORD_TIMESTAMP(DTLSEndpointStats, destroyed_at);
|
||||
|
||||
Local<Function> cb = GetCallback(DTLS_CB_ENDPOINT_CLOSE);
|
||||
if (!cb.IsEmpty()) {
|
||||
@@ -402,7 +417,10 @@ void DTLSEndpoint::ProcessDatagram(const uint8_t* data,
|
||||
void DTLSEndpoint::AcceptConnection(const uint8_t* data,
|
||||
size_t len,
|
||||
const SocketAddress& remote) {
|
||||
if (state_->busy) return;
|
||||
if (state_->busy) {
|
||||
DTLS_STAT_INCREMENT(DTLSEndpointStats, server_busy_count);
|
||||
return;
|
||||
}
|
||||
|
||||
HandleScope handle_scope(env()->isolate());
|
||||
Context::Scope context_scope(env()->context());
|
||||
@@ -481,6 +499,7 @@ void DTLSEndpoint::AcceptConnection(const uint8_t* data,
|
||||
|
||||
sessions_[remote] = session;
|
||||
state_->session_count = sessions_.size();
|
||||
DTLS_STAT_INCREMENT(DTLSEndpointStats, server_sessions);
|
||||
|
||||
uv_ref(reinterpret_cast<uv_handle_t*>(&handle_));
|
||||
|
||||
@@ -581,6 +600,12 @@ void DTLSEndpoint::GetState(const FunctionCallbackInfo<Value>& args) {
|
||||
args.GetReturnValue().Set(endpoint->state_.GetArrayBuffer());
|
||||
}
|
||||
|
||||
void DTLSEndpoint::GetStats(const FunctionCallbackInfo<Value>& args) {
|
||||
DTLSEndpoint* endpoint;
|
||||
ASSIGN_OR_RETURN_UNWRAP(&endpoint, args.This());
|
||||
args.GetReturnValue().Set(endpoint->stats_.GetArrayBuffer());
|
||||
}
|
||||
|
||||
void DTLSEndpoint::GetAddress(const FunctionCallbackInfo<Value>& args) {
|
||||
DTLSEndpoint* endpoint;
|
||||
ASSIGN_OR_RETURN_UNWRAP(&endpoint, args.This());
|
||||
|
||||
@@ -31,6 +31,11 @@ struct DTLSEndpointStateData {
|
||||
uint8_t busy = 0;
|
||||
};
|
||||
|
||||
// Stats collected for a DTLS endpoint, backed by a BigUint64Array.
|
||||
struct DTLSEndpointStats {
|
||||
DTLS_ENDPOINT_STATS(DTLS_STAT_FIELD)
|
||||
};
|
||||
|
||||
// DTLSEndpoint manages a single UDP socket and dispatches incoming
|
||||
// datagrams to the appropriate DTLSSession based on the remote address.
|
||||
// For server mode, it handles stateless cookie exchange via DTLSv1_listen()
|
||||
@@ -93,6 +98,7 @@ class DTLSEndpoint final : public HandleWrap {
|
||||
static void DoClose(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void DoDestroy(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void GetState(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void GetStats(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void GetAddress(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void SetMTU(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void DoSetCallbacks(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
@@ -136,6 +142,7 @@ class DTLSEndpoint final : public HandleWrap {
|
||||
v8::Global<v8::Function> callbacks_[DTLS_CB_COUNT];
|
||||
|
||||
AliasedStruct<DTLSEndpointStateData> state_;
|
||||
AliasedStruct<DTLSEndpointStats> stats_;
|
||||
|
||||
bool listening_ = false;
|
||||
uint32_t mtu_ = 1200; // Conservative default MTU for data payload
|
||||
|
||||
@@ -53,6 +53,8 @@ DTLSSession::DTLSSession(Environment* env,
|
||||
retransmit_timer_(env,
|
||||
[this] {
|
||||
if (destroyed_) return;
|
||||
DTLS_STAT_INCREMENT(DTLSSessionStats,
|
||||
retransmit_count);
|
||||
int ret = DTLSv1_handle_timeout(ssl_.get());
|
||||
if (ret < 0) {
|
||||
// Handshake timeout expired.
|
||||
@@ -70,8 +72,10 @@ DTLSSession::DTLSSession(Environment* env,
|
||||
}),
|
||||
remote_address_(remote),
|
||||
is_server_(is_server),
|
||||
state_(env->isolate()) {
|
||||
state_(env->isolate()),
|
||||
stats_(env->isolate()) {
|
||||
MakeWeak();
|
||||
DTLS_STAT_RECORD_TIMESTAMP(DTLSSessionStats, created_at);
|
||||
retransmit_timer_.Unref();
|
||||
|
||||
// Update shared state.
|
||||
@@ -103,6 +107,7 @@ Local<FunctionTemplate> DTLSSession::GetConstructorTemplate(Environment* env) {
|
||||
SetProtoMethod(isolate, tmpl, "close", DoClose);
|
||||
SetProtoMethod(isolate, tmpl, "destroy", DoDestroy);
|
||||
SetProtoMethod(isolate, tmpl, "getState", GetState);
|
||||
SetProtoMethod(isolate, tmpl, "getStats", GetStats);
|
||||
SetProtoMethod(isolate, tmpl, "getRemoteAddress", GetRemoteAddress);
|
||||
SetProtoMethod(isolate, tmpl, "getProtocol", GetProtocol);
|
||||
SetProtoMethod(isolate, tmpl, "getCipher", GetCipher);
|
||||
@@ -132,6 +137,7 @@ void DTLSSession::RegisterExternalReferences(
|
||||
registry->Register(DoClose);
|
||||
registry->Register(DoDestroy);
|
||||
registry->Register(GetState);
|
||||
registry->Register(GetStats);
|
||||
registry->Register(GetRemoteAddress);
|
||||
registry->Register(GetProtocol);
|
||||
registry->Register(GetCipher);
|
||||
@@ -275,6 +281,7 @@ void DTLSSession::Cycle() {
|
||||
handshake_complete_ = true;
|
||||
state_->handshaking = 0;
|
||||
state_->open = 1;
|
||||
DTLS_STAT_RECORD_TIMESTAMP(DTLSSessionStats, handshake_completed_at);
|
||||
|
||||
Local<Value> argv[] = {
|
||||
String::NewFromUtf8(env()->isolate(), SSL_get_version(ssl_.get()))
|
||||
@@ -301,6 +308,8 @@ void DTLSSession::ClearOut() {
|
||||
int read;
|
||||
|
||||
while ((read = SSL_read(ssl_.get(), buf, sizeof(buf))) > 0) {
|
||||
DTLS_STAT_INCREMENT_N(DTLSSessionStats, bytes_received, read);
|
||||
DTLS_STAT_INCREMENT(DTLSSessionStats, messages_received);
|
||||
// Emit the data to JS via callback.
|
||||
Local<Value> argv[] = {
|
||||
Buffer::Copy(env(), reinterpret_cast<const char*>(buf), read)
|
||||
@@ -385,6 +394,8 @@ int DTLSSession::Send(const uint8_t* data, size_t len) {
|
||||
|
||||
int written = SSL_write(ssl_.get(), data, len);
|
||||
if (written > 0) {
|
||||
DTLS_STAT_INCREMENT_N(DTLSSessionStats, bytes_sent, written);
|
||||
DTLS_STAT_INCREMENT(DTLSSessionStats, messages_sent);
|
||||
EncOut();
|
||||
}
|
||||
return written;
|
||||
@@ -395,6 +406,7 @@ void DTLSSession::Close() {
|
||||
|
||||
closed_ = true;
|
||||
state_->closing = 1;
|
||||
DTLS_STAT_RECORD_TIMESTAMP(DTLSSessionStats, closing_at);
|
||||
|
||||
// Send close_notify.
|
||||
int ret = SSL_shutdown(ssl_.get());
|
||||
@@ -421,6 +433,7 @@ void DTLSSession::Destroy() {
|
||||
closed_ = true;
|
||||
|
||||
state_->destroyed = 1;
|
||||
DTLS_STAT_RECORD_TIMESTAMP(DTLSSessionStats, destroyed_at);
|
||||
state_->open = 0;
|
||||
state_->handshaking = 0;
|
||||
|
||||
@@ -492,6 +505,12 @@ void DTLSSession::GetState(const FunctionCallbackInfo<Value>& args) {
|
||||
args.GetReturnValue().Set(session->state_.GetArrayBuffer());
|
||||
}
|
||||
|
||||
void DTLSSession::GetStats(const FunctionCallbackInfo<Value>& args) {
|
||||
DTLSSession* session;
|
||||
ASSIGN_OR_RETURN_UNWRAP(&session, args.This());
|
||||
args.GetReturnValue().Set(session->stats_.GetArrayBuffer());
|
||||
}
|
||||
|
||||
void DTLSSession::GetRemoteAddress(const FunctionCallbackInfo<Value>& args) {
|
||||
DTLSSession* session;
|
||||
ASSIGN_OR_RETURN_UNWRAP(&session, args.This());
|
||||
|
||||
@@ -17,6 +17,8 @@
|
||||
#include <openssl/bio.h>
|
||||
#include <openssl/ssl.h>
|
||||
|
||||
#include "dtls.h"
|
||||
|
||||
namespace node::dtls {
|
||||
|
||||
class DTLSEndpoint;
|
||||
@@ -30,6 +32,11 @@ struct DTLSSessionStateData {
|
||||
uint8_t has_message_listener = 0;
|
||||
};
|
||||
|
||||
// Stats collected for a DTLS session, backed by a BigUint64Array.
|
||||
struct DTLSSessionStats {
|
||||
DTLS_SESSION_STATS(DTLS_STAT_FIELD)
|
||||
};
|
||||
|
||||
// DTLSSession represents a single DTLS association with a remote peer.
|
||||
// It wraps an OpenSSL SSL* object configured for DTLS, using memory BIOs
|
||||
// to interface with the endpoint's UDP socket.
|
||||
@@ -102,6 +109,7 @@ class DTLSSession final : public AsyncWrap {
|
||||
static void DoClose(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void DoDestroy(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void GetState(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void GetStats(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void GetRemoteAddress(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void GetProtocol(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void GetCipher(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
@@ -159,6 +167,7 @@ class DTLSSession final : public AsyncWrap {
|
||||
int cycle_depth_ = 0;
|
||||
|
||||
AliasedStruct<DTLSSessionStateData> state_;
|
||||
AliasedStruct<DTLSSessionStats> stats_;
|
||||
};
|
||||
|
||||
} // namespace node::dtls
|
||||
|
||||
@@ -0,0 +1,154 @@
|
||||
// Flags: --experimental-dtls --no-warnings
|
||||
|
||||
// Test: DTLS endpoint and session stats increment with data transfer.
|
||||
|
||||
import { hasCrypto, skip, mustCall } from '../common/index.mjs';
|
||||
import assert from 'node:assert';
|
||||
import * as fixtures from '../common/fixtures.mjs';
|
||||
|
||||
const { ok, strictEqual, notStrictEqual } = assert;
|
||||
const { readKey } = fixtures;
|
||||
|
||||
if (!hasCrypto) {
|
||||
skip('missing crypto');
|
||||
}
|
||||
|
||||
if (!process.features.dtls) {
|
||||
skip('DTLS is not enabled');
|
||||
}
|
||||
|
||||
const { listen, connect } = await import('node:dtls');
|
||||
|
||||
const serverCert = readKey('agent1-cert.pem');
|
||||
const serverKey = readKey('agent1-key.pem');
|
||||
const ca = readKey('ca1-cert.pem');
|
||||
|
||||
const serverReceivedData = Promise.withResolvers();
|
||||
const clientReceivedData = Promise.withResolvers();
|
||||
|
||||
let serverSession;
|
||||
|
||||
// Start server.
|
||||
const endpoint = listen(mustCall((session) => {
|
||||
serverSession = session;
|
||||
|
||||
session.onmessage = mustCall((data) => {
|
||||
strictEqual(data.toString(), 'hello from client');
|
||||
session.send('hello from server');
|
||||
serverReceivedData.resolve();
|
||||
});
|
||||
|
||||
session.onhandshake = mustCall();
|
||||
}), {
|
||||
cert: serverCert.toString(),
|
||||
key: serverKey.toString(),
|
||||
port: 0,
|
||||
host: '127.0.0.1',
|
||||
});
|
||||
|
||||
// --- Endpoint stats should be available immediately ---
|
||||
|
||||
const epStats = endpoint.stats;
|
||||
ok(epStats, 'endpoint.stats should be defined');
|
||||
ok(epStats.isConnected, 'stats should be connected');
|
||||
ok(epStats.createdAt > 0n, 'createdAt should be set');
|
||||
strictEqual(epStats.destroyedAt, 0n);
|
||||
strictEqual(epStats.clientSessions, 0n);
|
||||
strictEqual(epStats.serverSessions, 0n);
|
||||
strictEqual(epStats.serverBusyCount, 0n);
|
||||
|
||||
// Connect client.
|
||||
const clientSession = connect('127.0.0.1', endpoint.address.port, {
|
||||
ca: [ca.toString()],
|
||||
rejectUnauthorized: false,
|
||||
});
|
||||
|
||||
clientSession.onmessage = mustCall((data) => {
|
||||
strictEqual(data.toString(), 'hello from server');
|
||||
clientReceivedData.resolve();
|
||||
});
|
||||
|
||||
clientSession.onhandshake = mustCall();
|
||||
|
||||
// Wait for handshake.
|
||||
await clientSession.opened;
|
||||
|
||||
// --- Client session stats after handshake ---
|
||||
|
||||
const csStats = clientSession.stats;
|
||||
ok(csStats, 'session.stats should be defined');
|
||||
ok(csStats.isConnected, 'session stats should be connected');
|
||||
ok(csStats.createdAt > 0n, 'createdAt should be set');
|
||||
ok(csStats.handshakeCompletedAt > 0n, 'handshake timestamp should be set');
|
||||
ok(csStats.handshakeCompletedAt >= csStats.createdAt,
|
||||
'handshake should complete after creation');
|
||||
strictEqual(csStats.closingAt, 0n);
|
||||
strictEqual(csStats.destroyedAt, 0n);
|
||||
|
||||
// Record bytes before sending application data.
|
||||
const csBytesSentBefore = csStats.bytesSent;
|
||||
const csMessagesSentBefore = csStats.messagesSent;
|
||||
|
||||
// Send data.
|
||||
clientSession.send('hello from client');
|
||||
|
||||
// Wait for bidirectional exchange.
|
||||
await Promise.all([serverReceivedData.promise, clientReceivedData.promise]);
|
||||
|
||||
// --- Client session stats after data exchange ---
|
||||
|
||||
ok(csStats.bytesSent > csBytesSentBefore,
|
||||
'bytesSent should increase after send');
|
||||
ok(csStats.messagesSent > csMessagesSentBefore,
|
||||
'messagesSent should increase after send');
|
||||
ok(csStats.bytesReceived > 0n, 'bytesReceived should be non-zero');
|
||||
ok(csStats.messagesReceived > 0n, 'messagesReceived should be non-zero');
|
||||
|
||||
// --- Server session stats after data exchange ---
|
||||
|
||||
ok(serverSession, 'server session should exist');
|
||||
const ssStats = serverSession.stats;
|
||||
ok(ssStats.bytesReceived > 0n, 'server bytesReceived should be non-zero');
|
||||
ok(ssStats.messagesReceived > 0n, 'server messagesReceived should be non-zero');
|
||||
ok(ssStats.bytesSent > 0n, 'server bytesSent should be non-zero');
|
||||
ok(ssStats.messagesSent > 0n, 'server messagesSent should be non-zero');
|
||||
ok(ssStats.handshakeCompletedAt > 0n, 'server handshake timestamp should be set');
|
||||
|
||||
// --- Endpoint stats after data exchange ---
|
||||
|
||||
ok(epStats.bytesReceived > 0n, 'endpoint bytesReceived should be non-zero');
|
||||
ok(epStats.bytesSent > 0n, 'endpoint bytesSent should be non-zero');
|
||||
ok(epStats.packetsReceived > 0n, 'endpoint packetsReceived should be non-zero');
|
||||
ok(epStats.packetsSent > 0n, 'endpoint packetsSent should be non-zero');
|
||||
strictEqual(epStats.serverSessions, 1n);
|
||||
|
||||
// The client's own endpoint should track the client session.
|
||||
const clientEpStats = clientSession.endpoint.stats;
|
||||
strictEqual(clientEpStats.clientSessions, 1n);
|
||||
ok(clientEpStats.bytesSent > 0n);
|
||||
ok(clientEpStats.bytesReceived > 0n);
|
||||
|
||||
// --- toJSON / toString ---
|
||||
|
||||
const epJson = epStats.toJSON();
|
||||
ok(epJson);
|
||||
strictEqual(typeof epJson.bytesReceived, 'string');
|
||||
strictEqual(typeof epJson.bytesSent, 'string');
|
||||
strictEqual(typeof epJson.connected, 'boolean');
|
||||
|
||||
const ssJson = csStats.toJSON();
|
||||
ok(ssJson);
|
||||
strictEqual(typeof ssJson.handshakeCompletedAt, 'string');
|
||||
strictEqual(typeof ssJson.messagesReceived, 'string');
|
||||
|
||||
const epStr = epStats.toString();
|
||||
ok(typeof epStr === 'string');
|
||||
ok(epStr.includes('bytesReceived'));
|
||||
|
||||
// Clean up.
|
||||
await clientSession.close();
|
||||
|
||||
// After close, session closing timestamp should be set.
|
||||
notStrictEqual(csStats.closingAt, 0n);
|
||||
|
||||
await endpoint.close();
|
||||
Reference in new issue
Block a user