Skip to content

Commit

Permalink
quic: use Number() instead of bigint for QuicSocket stats
Browse files Browse the repository at this point in the history
PR-URL: #34247
Reviewed-By: Anna Henningsen <anna@addaleax.net>
  • Loading branch information
jasnell committed Jul 9, 2020
1 parent 94372b1 commit 7123609
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 73 deletions.
97 changes: 60 additions & 37 deletions doc/api/quic.md
Expand Up @@ -1319,10 +1319,7 @@ added: REPLACEME
-->

New instances of `QuicSocket` are created using the `net.createQuicSocket()`
method.

Once created, a `QuicSocket` can be configured to work as both a client and a
server.
method, and can be used as both a client and a server.

#### Event: `'busy'`
<!-- YAML
Expand Down Expand Up @@ -1477,46 +1474,60 @@ added: REPLACEME

* Type: {boolean}

Will be `true` if the `QuicSocket` has been successfully bound to the local UDP
port.
Will be `true` if the `QuicSocket` has been successfully bound to a local UDP
port. Initially the value is `false`.

`QuicSocket` instances are not bound to a local UDP port until the first time
eithe `quicsocket.listen()` or `quicsocket.connect()` is called. The `'ready'`
event will be emitted once the `QuicSocket` has been bound and the value of
`quicsocket.bound` will become `true`.

Read-only.

#### quicsocket.boundDuration
<!-- YAML
added: REPLACEME
-->

* Type: {bigint}
* Type: {number}

A `BigInt` representing the length of time this `QuicSocket` has been bound
to a local port.
The length of time this `QuicSocket` has been bound to a local port.

Read-only.

#### quicsocket.bytesReceived
<!-- YAML
added: REPLACEME
-->

* Type: {bigint}
* Type: {number}

A `BigInt` representing the number of bytes received by this `QuicSocket`.
The number of bytes received by this `QuicSocket`.

Read-only.

#### quicsocket.bytesSent
<!-- YAML
added: REPLACEME
-->

* Type: {bigint}
* Type: {number}

The number of bytes sent by this `QuicSocket`.

A `BigInt` representing the number of bytes sent by this `QuicSocket`.
Read-only.

#### quicsocket.clientSessions
<!-- YAML
added: REPLACEME
-->

* Type: {bigint}
* Type: {number}

The number of client `QuicSession` instances that have been associated
with this `QuicSocket`.

A `BigInt` representing the number of client `QuicSession` instances that
have been associated with this `QuicSocket`.
Read-only.

#### quicsocket.close(\[callback\])
<!-- YAML
Expand Down Expand Up @@ -1690,9 +1701,11 @@ Will be `true` if the `QuicSocket` has been destroyed.
added: REPLACEME
-->

* Type: {bigint}
* Type: {number}

The length of time this `QuicSocket` has been active,

A `BigInt` representing the length of time this `QuicSocket` has been active,
Read-only.

#### quicsocket.endpoints
<!-- YAML
Expand Down Expand Up @@ -1828,10 +1841,11 @@ If a `callback` is given, it is registered as a handler for the
added: REPLACEME
-->

* Type: {bigint}
* Type: {number}

A `BigInt` representing the length of time this `QuicSocket` has been listening
for connections.
The length of time this `QuicSocket` has been listening for connections.

Read-only

#### quicsocket.listening
<!-- YAML
Expand All @@ -1847,29 +1861,33 @@ Set to `true` if the `QuicSocket` is listening for new connections.
added: REPLACEME
-->

* Type: {bigint}
* Type: {number}

A `BigInt` representing the number of packets received by this `QuicSocket` that
have been ignored.
The number of packets received by this `QuicSocket` that have been ignored.

Read-only.

#### quicsocket.packetsReceived
<!-- YAML
added: REPLACEME
-->

* Type: {bigint}
* Type: {number}

The number of packets successfully received by this `QuicSocket`.

A `BigInt` representing the number of packets successfully received by this
`QuicSocket`.
Read-only

#### quicsocket.packetsSent
<!-- YAML
added: REPLACEME
-->

* Type: {bigint}
* Type: {number}

The number of packets sent by this `QuicSocket`.

A `BigInt` representing the number of packets sent by this `QuicSocket`.
Read-only

#### quicsocket.pending
<!-- YAML
Expand Down Expand Up @@ -1902,20 +1920,23 @@ error code. To begin receiving connections again, disable busy mode by setting
added: REPLACEME
-->

* Type: {bigint}
* Type: {number}

The number of `QuicSession` instances rejected due to server busy status.

A `BigInt` representing the number of `QuicSession` instances rejected
due to server busy status.
Read-only.

#### quicsocket.serverSessions
<!-- YAML
added: REPLACEME
-->

* Type: {bigint}
* Type: {number}

A `BigInt` representing the number of server `QuicSession` instances that
have been associated with this `QuicSocket`.
The number of server `QuicSession` instances that have been associated with
this `QuicSocket`.

Read-only.

#### quicsocket.setDiagnosticPacketLoss(options)
<!-- YAML
Expand All @@ -1939,9 +1960,11 @@ This method is *not* to be used in production applications.
added: REPLACEME
-->

* Type: {bigint}
* Type: {number}

The number of stateless resets that have been sent.

A `BigInt` that represents the number of stateless resets that have been sent.
Read-only.

#### quicsocket.toggleStatelessReset()
<!-- YAML
Expand Down
31 changes: 16 additions & 15 deletions lib/internal/quic/core.js
Expand Up @@ -14,6 +14,7 @@ const {
BigInt64Array,
Error,
Map,
Number,
RegExp,
Set,
Symbol,
Expand Down Expand Up @@ -1527,58 +1528,58 @@ class QuicSocket extends EventEmitter {
get duration() {
// TODO(@jasnell): If the object is destroyed, it should
// use a fixed duration rather than calculating from now
return process.hrtime.bigint() -
getStats(this, IDX_QUIC_SOCKET_STATS_CREATED_AT);
return Number(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
return process.hrtime.bigint() -
getStats(this, IDX_QUIC_SOCKET_STATS_BOUND_AT);
return Number(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
return process.hrtime.bigint() -
getStats(this, IDX_QUIC_SOCKET_STATS_LISTEN_AT);
return Number(process.hrtime.bigint() -
getStats(this, IDX_QUIC_SOCKET_STATS_LISTEN_AT));
}

get bytesReceived() {
return getStats(this, IDX_QUIC_SOCKET_STATS_BYTES_RECEIVED);
return Number(getStats(this, IDX_QUIC_SOCKET_STATS_BYTES_RECEIVED));
}

get bytesSent() {
return getStats(this, IDX_QUIC_SOCKET_STATS_BYTES_SENT);
return Number(getStats(this, IDX_QUIC_SOCKET_STATS_BYTES_SENT));
}

get packetsReceived() {
return getStats(this, IDX_QUIC_SOCKET_STATS_PACKETS_RECEIVED);
return Number(getStats(this, IDX_QUIC_SOCKET_STATS_PACKETS_RECEIVED));
}

get packetsSent() {
return getStats(this, IDX_QUIC_SOCKET_STATS_PACKETS_SENT);
return Number(getStats(this, IDX_QUIC_SOCKET_STATS_PACKETS_SENT));
}

get packetsIgnored() {
return getStats(this, IDX_QUIC_SOCKET_STATS_PACKETS_IGNORED);
return Number(getStats(this, IDX_QUIC_SOCKET_STATS_PACKETS_IGNORED));
}

get serverSessions() {
return getStats(this, IDX_QUIC_SOCKET_STATS_SERVER_SESSIONS);
return Number(getStats(this, IDX_QUIC_SOCKET_STATS_SERVER_SESSIONS));
}

get clientSessions() {
return getStats(this, IDX_QUIC_SOCKET_STATS_CLIENT_SESSIONS);
return Number(getStats(this, IDX_QUIC_SOCKET_STATS_CLIENT_SESSIONS));
}

get statelessResetCount() {
return getStats(this, IDX_QUIC_SOCKET_STATS_STATELESS_RESET_COUNT);
return Number(getStats(this, IDX_QUIC_SOCKET_STATS_STATELESS_RESET_COUNT));
}

get serverBusyCount() {
return getStats(this, IDX_QUIC_SOCKET_STATS_SERVER_BUSY_COUNT);
return Number(getStats(this, IDX_QUIC_SOCKET_STATS_SERVER_BUSY_COUNT));
}

// Diagnostic packet loss is a testing mechanism that allows simulating
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-quic-maxconnectionsperhost.js
Expand Up @@ -56,7 +56,7 @@ const kALPN = 'zzz';
server.on('session', common.mustCall(() => {}, kMaxConnectionsPerHost));

server.on('close', common.mustCall(() => {
assert.strictEqual(server.serverBusyCount, 1n);
assert.strictEqual(server.serverBusyCount, 1);
}));

server.on('ready', common.mustCall(() => {
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-quic-quicsocket-serverbusy.js
Expand Up @@ -32,7 +32,7 @@ server.on('busy', common.mustCall((busy) => {

// When the server is set as busy, all connections
// will be rejected with a SERVER_BUSY response.
server.setServerBusy();
server.serverBusy = true;
server.listen();

server.on('close', common.mustCall());
Expand Down
33 changes: 14 additions & 19 deletions test/parallel/test-quic-quicsocket.js
Expand Up @@ -31,16 +31,16 @@ assert(!socket.pending);
// Socket is not destroyed
assert(!socket.destroyed);

assert.strictEqual(typeof socket.duration, 'bigint');
assert.strictEqual(typeof socket.boundDuration, 'bigint');
assert.strictEqual(typeof socket.listenDuration, 'bigint');
assert.strictEqual(typeof socket.bytesReceived, 'bigint');
assert.strictEqual(socket.bytesReceived, 0n);
assert.strictEqual(socket.bytesSent, 0n);
assert.strictEqual(socket.packetsReceived, 0n);
assert.strictEqual(socket.packetsSent, 0n);
assert.strictEqual(socket.serverSessions, 0n);
assert.strictEqual(socket.clientSessions, 0n);
assert.strictEqual(typeof socket.duration, 'number');
assert.strictEqual(typeof socket.boundDuration, 'number');
assert.strictEqual(typeof socket.listenDuration, 'number');
assert.strictEqual(typeof socket.bytesReceived, 'number');
assert.strictEqual(socket.bytesReceived, 0);
assert.strictEqual(socket.bytesSent, 0);
assert.strictEqual(socket.packetsReceived, 0);
assert.strictEqual(socket.packetsSent, 0);
assert.strictEqual(socket.serverSessions, 0);
assert.strictEqual(socket.clientSessions, 0);

const endpoint = socket.endpoints[0];
assert(endpoint);
Expand Down Expand Up @@ -83,8 +83,8 @@ assert(endpoint);
});
});

[1, 1n, [], {}, null].forEach((args) => {
assert.throws(() => socket.setServerBusy(args), {
[1, 1n, [], {}, null].forEach((arg) => {
assert.throws(() => socket.serverBusy = arg, {
code: 'ERR_INVALID_ARG_TYPE'
});
});
Expand Down Expand Up @@ -143,12 +143,7 @@ socket.on('close', common.mustCall(() => {
});
});

[
'setServerBusy',
].forEach((op) => {
assert.throws(() => socket[op](), {
code: 'ERR_QUICSOCKET_DESTROYED',
message: `Cannot call ${op} after a QuicSocket has been destroyed`
});
assert.throws(() => { socket.serverBusy = true; }, {
code: 'ERR_QUICSOCKET_DESTROYED'
});
}));

0 comments on commit 7123609

Please sign in to comment.