From 70a3aab486c55c7248d5de044701f264c88151a7 Mon Sep 17 00:00:00 2001 From: Joel Einbinder Date: Wed, 15 Sep 2021 11:40:54 -0400 Subject: [PATCH] fix(httpServer): speed up closing by destroying active sockets (#8919) --- src/utils/httpServer.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/utils/httpServer.ts b/src/utils/httpServer.ts index 9edba66bf9..38a4528514 100644 --- a/src/utils/httpServer.ts +++ b/src/utils/httpServer.ts @@ -25,7 +25,7 @@ export class HttpServer { private _server: http.Server | undefined; private _urlPrefix: string; private _routes: { prefix?: string, exact?: string, handler: ServerRouteHandler }[] = []; - + private _activeSockets = new Set(); constructor() { this._urlPrefix = ''; } @@ -39,7 +39,12 @@ export class HttpServer { } async start(port?: number): Promise { + console.assert(!this._server, 'server already started'); this._server = http.createServer(this._onRequest.bind(this)); + this._server.on('connection', socket => { + this._activeSockets.add(socket); + socket.once('close', () => this._activeSockets.delete(socket)); + }); this._server.listen(port); await new Promise(cb => this._server!.once('listening', cb)); const address = this._server.address(); @@ -48,6 +53,8 @@ export class HttpServer { } async stop() { + for (const socket of this._activeSockets) + socket.destroy(); await new Promise(cb => this._server!.close(cb)); }