Skip to content

Commit 94ebb02

Browse files
atlowChemiRafaelGSS
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 07eb310 commit 94ebb02

3 files changed

Lines changed: 33 additions & 0 deletions

File tree

‎doc/api/http.md‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1667,6 +1667,17 @@ to 8.0.0, which did not have a keep-alive timeout.
16671667
The socket timeout logic is set up on connection, so changing this value only
16681668
affects new connections to the server, not any existing connections.
16691669

1670+
### `server[Symbol.asyncDispose]()`
1671+
1672+
<!-- YAML
1673+
added: REPLACEME
1674+
-->
1675+
1676+
> Stability: 1 - Experimental
1677+
1678+
Calls [`server.close()`][] and returns a promise that fulfills when the
1679+
server has closed.
1680+
16701681
## Class: `http.ServerResponse`
16711682

16721683
<!-- YAML
@@ -3895,6 +3906,7 @@ Set the maximum number of idle HTTP parsers.
38953906
[`response.write(data, encoding)`]: #responsewritechunk-encoding-callback
38963907
[`response.writeContinue()`]: #responsewritecontinue
38973908
[`response.writeHead()`]: #responsewriteheadstatuscode-statusmessage-headers
3909+
[`server.close()`]: #serverclosecallback
38983910
[`server.headersTimeout`]: #serverheaderstimeout
38993911
[`server.keepAliveTimeout`]: #serverkeepalivetimeout
39003912
[`server.listen()`]: net.md#serverlisten

‎lib/_http_server.js‎

Lines changed: 7 additions & 0 deletions
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

@@ -81,6 +83,7 @@ const {
8183
}=codes;
8284
const{
8385
kEmptyObject,
86+
promisify,
8487
}=require('internal/util');
8588
const{
8689
validateInteger,
@@ -557,6 +560,10 @@ Server.prototype.close = function() {
557560
ReflectApply(net.Server.prototype.close,this,arguments);
558561
};
559562

563+
Server.prototype[SymbolAsyncDispose]=asyncfunction(){
564+
returnFunctionPrototypeCall(promisify(this.close),this);
565+
};
566+
560567
Server.prototype.closeAllConnections=function(){
561568
constconnections=this[kConnections].all();
562569

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict';
2+
constcommon=require('../common');
3+
constassert=require('assert');
4+
const{ createServer }=require('http');
5+
const{ kConnectionsCheckingInterval }=require('_http_server');
6+
7+
constserver=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)