http-shutdown

WebJar for http-shutdown

License

License

MIT
GroupId

GroupId

org.webjars.npm
ArtifactId

ArtifactId

http-shutdown
Last Version

Last Version

1.2.0
Release Date

Release Date

Type

Type

jar
Description

Description

http-shutdown
WebJar for http-shutdown
Project URL

Project URL

http://webjars.org
Source Code Management

Source Code Management

https://github.com/thedillonb/http-shutdown

Download http-shutdown

How to add to project

<!-- https://jarcasting.com/artifacts/org.webjars.npm/http-shutdown/ -->
<dependency>
    <groupId>org.webjars.npm</groupId>
    <artifactId>http-shutdown</artifactId>
    <version>1.2.0</version>
</dependency>
// https://jarcasting.com/artifacts/org.webjars.npm/http-shutdown/
implementation 'org.webjars.npm:http-shutdown:1.2.0'
// https://jarcasting.com/artifacts/org.webjars.npm/http-shutdown/
implementation ("org.webjars.npm:http-shutdown:1.2.0")
'org.webjars.npm:http-shutdown:jar:1.2.0'
<dependency org="org.webjars.npm" name="http-shutdown" rev="1.2.0">
  <artifact name="http-shutdown" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.webjars.npm', module='http-shutdown', version='1.2.0')
)
libraryDependencies += "org.webjars.npm" % "http-shutdown" % "1.2.0"
[org.webjars.npm/http-shutdown "1.2.0"]

Dependencies

There are no dependencies for this project. It is a standalone project that does not depend on any other jars.

Project Modules

There are no modules declared in this project.

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

Versions

Version
1.2.0