fix(httpServer): speed up closing by destroying active sockets (#8919)

This commit is contained in:
Joel Einbinder 2021-09-15 11:40:54 -04:00 committed by GitHub
parent a2ede38551
commit 70a3aab486
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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<import('net').Socket>();
constructor() {
this._urlPrefix = '';
}
@ -39,7 +39,12 @@ export class HttpServer {
}
async start(port?: number): Promise<string> {
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));
}