Http-Shutdown
![Test coverage](https://camo.githubusercontent.com/be148ed74995369debcaf362ed4d7c648662965cc52689c7883c46fa3e8443cb/68747470733a2f2f696d672e736869656c64732e696f2f636f766572616c6c732f74686564696c6c6f6e622f687474702d73687574646f776e2e7376673f7374796c653d666c61742d737175617265)
Shutdown a Nodejs HTTP server gracefully by doing the following:
- Close the listening socket to prevent new connections
- Close all idle keep-alive sockets to prevent new requests during shutdown
- Wait for all in-flight requests to finish before closing their sockets.
- Profit!
Other solutions might just use server.close
which only terminates the listening socket and waits for other sockets to close - which is incomplete since keep-alive sockets can still make requests. Or, they may use ref()/unref()
to simply cause Nodejs to terminate if the sockets are idle - which doesn't help if you have other things to shutdown after the server shutsdown.
http-shutdown
is a complete solution. It uses idle indicators combined with an active socket list to safely, and gracefully, close all sockets. It does not use ref()/unref()
but, instead, actively closes connections as they finish meaning that socket 'close' events still work correctly since the sockets are actually closing - you're not just unref
ing and forgetting about them.
Installation
$ npm install http-shutdown
Usage
There are currently two ways to use this library. The first is explicit wrapping of the Server
object:
// Create the http server
var server = require('http').createServer(function(req, res) {
res.end('Good job!');
});
// Wrap the server object with additional functionality.
// This should be done immediately after server construction, or before you start listening.
// Additional functionailiy needs to be added for http server events to properly shutdown.
server = require('http-shutdown')(server);
// Listen on a port and start taking requests.
server.listen(3000);
// Sometime later... shutdown the server.
server.shutdown(function(err) {
if (err) {
return console.log('shutdown failed', err.message);
}
console.log('Everything is cleanly shutdown.');
});
The second is implicitly adding prototype functionality to the Server
object:
// .extend adds a .withShutdown prototype method to the Server object
require('http-shutdown').extend();
var server = require('http').createServer(function(req, res) {
res.end('God job!');
}).withShutdown(); // <-- Easy to chain. Returns the Server object
// Sometime later, shutdown the server.
server.shutdown(function(err) {
if (err) {
return console.log('shutdown failed', err.message);
}
console.log('Everything is cleanly shutdown.');
});
Test
$ npm test