Skip to content

Commit fcd0424

Browse files
atlowChemiruyadorno
authored andcommitted
http: server add async dispose
PR-URL: #48548 Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Paolo Insogna <paolo@cowtech.it> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent dd1805e commit fcd0424

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

doc/api/http.md

+12
Original file line numberDiff line numberDiff line change
@@ -1652,6 +1652,17 @@ to 8.0.0, which did not have a keep-alive timeout.
16521652
The socket timeout logic is set up on connection, so changing this value only
16531653
affects new connections to the server, not any existing connections.
16541654

1655+
### `server[Symbol.asyncDispose]()`
1656+
1657+
<!-- YAML
1658+
added: REPLACEME
1659+
-->
1660+
1661+
> Stability: 1 - Experimental
1662+
1663+
Calls [`server.close()`][] and returns a promise that fulfills when the
1664+
server has closed.
1665+
16551666
## Class: `http.ServerResponse`
16561667

16571668
<!-- YAML
@@ -3843,6 +3854,7 @@ Set the maximum number of idle HTTP parsers.
38433854
[`response.write(data, encoding)`]: #responsewritechunk-encoding-callback
38443855
[`response.writeContinue()`]: #responsewritecontinue
38453856
[`response.writeHead()`]: #responsewriteheadstatuscode-statusmessage-headers
3857+
[`server.close()`]: #serverclosecallback
38463858
[`server.headersTimeout`]: #serverheaderstimeout
38473859
[`server.listen()`]: net.md#serverlisten
38483860
[`server.requestTimeout`]: #serverrequesttimeout

lib/_http_server.js

+9
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@
2424
const {
2525
ArrayIsArray,
2626
Error,
27+
FunctionPrototypeCall,
2728
MathMin,
2829
ObjectKeys,
2930
ObjectSetPrototypeOf,
3031
RegExpPrototypeExec,
3132
ReflectApply,
3233
Symbol,
34+
SymbolAsyncDispose,
3335
SymbolFor,
3436
} = primordials;
3537

@@ -80,6 +82,9 @@ const {
8082
ERR_INVALID_ARG_VALUE,
8183
ERR_INVALID_CHAR,
8284
} = codes;
85+
const {
86+
promisify,
87+
} = require('internal/util');
8388
const {
8489
validateInteger,
8590
validateBoolean,
@@ -548,6 +553,10 @@ Server.prototype.close = function() {
548553
ReflectApply(net.Server.prototype.close, this, arguments);
549554
};
550555

556+
Server.prototype[SymbolAsyncDispose] = async function() {
557+
return FunctionPrototypeCall(promisify(this.close), this);
558+
};
559+
551560
Server.prototype.closeAllConnections = function() {
552561
const connections = this[kConnections].all();
553562

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const { createServer } = require('http');
5+
const { kConnectionsCheckingInterval } = require('_http_server');
6+
7+
const server = createServer();
8+
9+
server.listen(0, common.mustCall(() => {
10+
server.on('close', common.mustCall());
11+
server[Symbol.asyncDispose]().then(common.mustCall(() => {
12+
assert(server[kConnectionsCheckingInterval]._destroyed);
13+
}));
14+
}));

0 commit comments

Comments
 (0)