Skip to content

Instantly share code, notes, and snippets.

View jinxidoru's full-sized avatar

Viper Bailey jinxidoru

View GitHub Profile
@jinxidoru
jinxidoru / closeable_http_server.ts
Last active January 24, 2022 19:31
Closeable http server
import * as http from 'http'
import * as _ from 'lodash'
//! In the standard implementation of http server, calling close is not guaranteed to close the
//! server. Any idle connections being kept alive by clients will stay open until their timeout
//! is reached. This is problematic for gracefully shutting down a process with an http server.
//! This function will create an http server that tracks the open connections. On close, idle
//! connections are closed and any newly idled connection is immediately closed as well. When there
//! are no more connections on the server, the 'empty' event is raised.