Skip to content

Commit

Permalink
http: server add async dispose
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
atlowChemi authored and ruyadorno committed Sep 12, 2023
1 parent dd1805e commit fcd0424
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
12 changes: 12 additions & 0 deletions doc/api/http.md
Original file line number Diff line number Diff line change
Expand Up @@ -1652,6 +1652,17 @@ to 8.0.0, which did not have a keep-alive timeout.
The socket timeout logic is set up on connection, so changing this value only
affects new connections to the server, not any existing connections.

### `server[Symbol.asyncDispose]()`

<!-- YAML
added: REPLACEME
-->

> Stability: 1 - Experimental
Calls [`server.close()`][] and returns a promise that fulfills when the
server has closed.

## Class: `http.ServerResponse`

<!-- YAML
Expand Down Expand Up @@ -3843,6 +3854,7 @@ Set the maximum number of idle HTTP parsers.
[`response.write(data, encoding)`]: #responsewritechunk-encoding-callback
[`response.writeContinue()`]: #responsewritecontinue
[`response.writeHead()`]: #responsewriteheadstatuscode-statusmessage-headers
[`server.close()`]: #serverclosecallback
[`server.headersTimeout`]: #serverheaderstimeout
[`server.listen()`]: net.md#serverlisten
[`server.requestTimeout`]: #serverrequesttimeout
Expand Down
9 changes: 9 additions & 0 deletions lib/_http_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@
const {
ArrayIsArray,
Error,
FunctionPrototypeCall,
MathMin,
ObjectKeys,
ObjectSetPrototypeOf,
RegExpPrototypeExec,
ReflectApply,
Symbol,
SymbolAsyncDispose,
SymbolFor,
} = primordials;

Expand Down Expand Up @@ -80,6 +82,9 @@ const {
ERR_INVALID_ARG_VALUE,
ERR_INVALID_CHAR,
} = codes;
const {
promisify,
} = require('internal/util');
const {
validateInteger,
validateBoolean,
Expand Down Expand Up @@ -548,6 +553,10 @@ Server.prototype.close = function() {
ReflectApply(net.Server.prototype.close, this, arguments);
};

Server.prototype[SymbolAsyncDispose] = async function() {
return FunctionPrototypeCall(promisify(this.close), this);
};

Server.prototype.closeAllConnections = function() {
const connections = this[kConnections].all();

Expand Down
14 changes: 14 additions & 0 deletions test/parallel/test-http-server-async-dispose.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const { createServer } = require('http');
const { kConnectionsCheckingInterval } = require('_http_server');

const server = createServer();

server.listen(0, common.mustCall(() => {
server.on('close', common.mustCall());
server[Symbol.asyncDispose]().then(common.mustCall(() => {
assert(server[kConnectionsCheckingInterval]._destroyed);
}));
}));

0 comments on commit fcd0424

Please sign in to comment.