1.2.2 • Published 4 years ago

http-shutdown v1.2.2

Weekly downloads
34,281
License
MIT
Repository
github
Last release
4 years ago

Http-Shutdown NPM version Build status Test coverage

Shutdown a Nodejs HTTP server gracefully by doing the following:

  1. Close the listening socket to prevent new connections
  2. Close all idle keep-alive sockets to prevent new requests during shutdown
  3. Wait for all in-flight requests to finish before closing their sockets.
  4. 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 unrefing 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
@requestnetwork/request-nodedrakov-js@yarvis/ws@everything-registry/sub-chunk-1871pam-http_enoly-httpnode-standupnpg_rangernuxtpaginationsmu-koan-servermyclasppatepangdeuinode-red-appnode-rtsp-stream-jsmpegnode-multi-webserverpileuleuyanteamarkserv-climathoidmay-it-servelisthenluminati-proxy-strippedmangudinlagirajinmetaformmeta-proxy-managermockingjaystehmusimhujantelegram-test-apisumbal-tileserver-gltileserver-gltileserver-gl-lighttileserver-gl-light-customtrinity-jestunitospotify-personal-authskymet-proxy-managerskymet-proxy-manager-newservice-template-nodesedanbosoks3cf1gsandbox-test-serverslevomat-tileserver-gl-lightsrc2imgstencila-nodestencila-node-0.26.0steedos-node-redrest-api-servicerest-in-contractrefinejs-repotoorzona-xyyl-seed-test-utilairic-api-gateway-config-serverairic-api-gateway-key-server@thinkbeanie/http@vinxi/listhenactive-observeranakketigaanaklanangteaanakwadontea@webgeodatavore/tileserver-gl-light@whitewater-guide/logbook-server@teselagen/s3-uploads@acalcutt/tileserver-gl@acalcutt/tileserver-gl-light@trigo/atrix-soap@simplej/spry-core@smujaddid/drakov@ezs/core@tomw2w/my-nuxt-layer@serenity-js/local-server@plaqard/core@arsonar/server@arso-project/sonar-server@ascot/http-server@ascot/httpaswh@andrew-codes/marcoavo@absanger/npg_ranger@aabelmann/ui-layer@coboxcoop/seeder@coboxcoop/server@youon/videojs-abloop@zalastax/nolb-http-sbackk@danizean/cyberzonebash.origin.express@deepstream/server@codersyndicate/graceful-shutdownexpress-listen-oneexpress-quick-buildereventgate@inspirock/tileserver-glconfigurapi-runnerelectron-ssr@horwood/serverenefti-galerianymock-servicebuhoiezs
1.2.2

4 years ago

1.2.1

5 years ago

1.2.0

7 years ago

1.1.0

8 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago