server.close()the way you probably expected it to work by default.
const server =server
Stoppable stops accepting new connections and closes existing, idle connections (including keep-alives) without killing requests that are in-flight.
- Node.js v6+
Node.js v4.x is unofficially supported.
yarn add stoppable
(or use npm)
Decorates the server instance with a
Returns the server instance, so can be chained, or can be run as a standalone statement.
- server: Any HTTP or HTTPS Server instance
- grace: Milliseconds to wait before force-closing connections
grace defaults to Infinity (don't force-close).
If you want to immediately kill all sockets you can use a grace of 0.
Closes the server.
- callback: passed along to the existing
server.closefunction to auto-register a 'close' event. The first agrument is an error, and the second argument is a boolean that indicates whether it stopped gracefully.
- Monkey patching generally sucks, but in this case it's the nicest API. Let's call it "decorating."
gracecould be specified on
stop, but it's better to match the existing
- Clients should be handled respectfully, so we aren't just destroying sockets, we're sending
- Any solution to this problem requires bookkeeping on every connection and request/response.
We're doing a minimum of work on these "hot" code paths and delaying as much as possible to the actual
There's no way to provide this functionality without bookkeeping on connection, disconnection, request, and response. However, Stoppable strives to do minimal work in hot code paths and to use optimal data structures.
I'd be interested to see real-world performance benchmarks; the simple loopback artillery benchmark included in the lib shows very little overhead from using a stoppable server:
Scenarios launched: 10000Scenarios completed: 10000Requests completed: 10000RPS sent: 939.85Request latency:min: 0.5max: 51.3median: 2.1p95: 3.7p99: 15.3Scenario duration:min: 1max: 60.7median: 3.6p95: 7.6p99: 19Scenario counts:0: 10000 (100%)Codes:200: 10000
Scenarios launched: 10000Scenarios completed: 10000Requests completed: 10000RPS sent: 940.73Request latency:min: 0.5max: 43.4median: 2.1p95: 3.8p99: 15.5Scenario duration:min: 1.1max: 57median: 3.7p95: 8p99: 19.4Scenario counts:0: 10000 (100%)Codes:200: 10000