Skip to content

Commit cc89aac

Browse files
committed
quic: refactor/improve error handling for busy event
Also, change setServerBusy into a setter PR-URL: #34247 Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent edc71ef commit cc89aac

File tree

4 files changed

+93
-21
lines changed

4 files changed

+93
-21
lines changed

doc/api/quic.md

+16-17
Original file line numberDiff line numberDiff line change
@@ -1330,8 +1330,8 @@ added: REPLACEME
13301330
-->
13311331

13321332
Emitted when the server busy state has been toggled using
1333-
`quicSocket.setServerBusy()`. The callback is invoked with a single
1334-
boolean argument indicating `true` if busy status is enabled,
1333+
`quicSocket.serverBusy = true | false`. The callback is invoked with a
1334+
single boolean argument indicating `true` if busy status is enabled,
13351335
`false` otherwise. This event is strictly informational.
13361336

13371337
```js
@@ -1346,8 +1346,8 @@ socket.on('busy', (busy) => {
13461346
console.log('Server is not busy');
13471347
});
13481348

1349-
socket.setServerBusy(true);
1350-
socket.setServerBusy(false);
1349+
socket.serverBusy = true;
1350+
socket.serverBusy = false;
13511351
```
13521352

13531353
This `'busy'` event may be emitted multiple times.
@@ -1874,6 +1874,18 @@ Set to `true` if the socket is not yet bound to the local UDP port.
18741874
added: REPLACEME
18751875
-->
18761876

1877+
#### quicsocket.serverBusy
1878+
<!-- YAML
1879+
added: REPLACEME
1880+
-->
1881+
1882+
* Type: {boolean} When `true`, the `QuicSocket` will reject new connections.
1883+
1884+
Setting `quicsocket.serverBusy` to `true` will tell the `QuicSocket`
1885+
to reject all new incoming connection requests using the `SERVER_BUSY` QUIC
1886+
error code. To begin receiving connections again, disable busy mode by setting
1887+
`quicsocket.serverBusy = false`.
1888+
18771889
#### quicsocket.serverBusyCount
18781890
<!-- YAML
18791891
added: REPLACEME
@@ -1911,19 +1923,6 @@ by artificially dropping received or transmitted packets.
19111923

19121924
This method is *not* to be used in production applications.
19131925

1914-
#### quicsocket.setServerBusy(\[on\])
1915-
<!-- YAML
1916-
added: REPLACEME
1917-
-->
1918-
1919-
* `on` {boolean} When `true`, the `QuicSocket` will reject new connections.
1920-
**Defaults**: `true`.
1921-
1922-
Calling `setServerBusy()` or `setServerBusy(true)` will tell the `QuicSocket`
1923-
to reject all new incoming connection requests using the `SERVER_BUSY` QUIC
1924-
error code. To begin receiving connections again, disable busy mode by calling
1925-
`setServerBusy(false)`.
1926-
19271926
#### quicsocket.statelessResetCount
19281927
<!-- YAML
19291928
added: REPLACEME

lib/internal/quic/core.js

+13-4
Original file line numberDiff line numberDiff line change
@@ -851,7 +851,7 @@ class QuicSocket extends EventEmitter {
851851
highWaterMark: undefined,
852852
lookup: undefined,
853853
server: undefined,
854-
serverBusy: undefined,
854+
serverBusy: false,
855855
serverListening: false,
856856
serverSecureContext: undefined,
857857
sessions: new Set(),
@@ -1075,7 +1075,16 @@ class QuicSocket extends EventEmitter {
10751075
// Called by the C++ internals to notify when server busy status is toggled.
10761076
[kServerBusy](on) {
10771077
this[kInternalState].serverBusy = on;
1078-
process.nextTick(emit.bind(this, 'busy', on));
1078+
// In a nextTick because the event ends up being
1079+
// emitted synchronously when quicSocket.serverBusy
1080+
// is called.
1081+
process.nextTick(() => {
1082+
try {
1083+
this.emit('busy', on);
1084+
} catch (error) {
1085+
this[kRejections](error, 'busy', on);
1086+
}
1087+
});
10791088
}
10801089

10811090
addEndpoint(options = {}) {
@@ -1490,10 +1499,10 @@ class QuicSocket extends EventEmitter {
14901499

14911500
// Marking a server as busy will cause all new
14921501
// connection attempts to fail with a SERVER_BUSY CONNECTION_CLOSE.
1493-
setServerBusy(on = true) {
1502+
set serverBusy(on) {
14941503
const state = this[kInternalState];
14951504
if (state.state === kSocketDestroyed)
1496-
throw new ERR_QUICSOCKET_DESTROYED('setServerBusy');
1505+
throw new ERR_QUICSOCKET_DESTROYED('serverBusy');
14971506
validateBoolean(on, 'on');
14981507
if (state.serverBusy !== on)
14991508
this[kHandle].setServerBusy(on);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Flags: --no-warnings
2+
'use strict';
3+
4+
const common = require('../common');
5+
if (!common.hasQuic)
6+
common.skip('missing quic');
7+
8+
const assert = require('assert');
9+
const {
10+
key,
11+
cert,
12+
ca,
13+
} = require('../common/quic');
14+
15+
const { createQuicSocket } = require('net');
16+
17+
const options = { key, cert, ca, alpn: 'zzz' };
18+
19+
const server = createQuicSocket({ server: options });
20+
21+
server.on('busy', common.mustCall(async () => {
22+
throw new Error('boom');
23+
}));
24+
25+
server.on('close', common.mustCall());
26+
27+
server.on('error', common.mustCall((err) => {
28+
assert.strictEqual(err.message, 'boom');
29+
}));
30+
31+
assert.strictEqual(server.serverBusy, false);
32+
server.serverBusy = true;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Flags: --no-warnings
2+
'use strict';
3+
4+
const common = require('../common');
5+
if (!common.hasQuic)
6+
common.skip('missing quic');
7+
8+
const assert = require('assert');
9+
const {
10+
key,
11+
cert,
12+
ca,
13+
} = require('../common/quic');
14+
15+
const { createQuicSocket } = require('net');
16+
17+
const options = { key, cert, ca, alpn: 'zzz' };
18+
19+
const server = createQuicSocket({ server: options });
20+
21+
server.on('busy', common.mustCall(() => {
22+
throw new Error('boom');
23+
}));
24+
25+
server.on('close', common.mustCall());
26+
27+
server.on('error', common.mustCall((err) => {
28+
assert.strictEqual(err.message, 'boom');
29+
}));
30+
31+
assert.strictEqual(server.serverBusy, false);
32+
server.serverBusy = true;

0 commit comments

Comments
 (0)