• Stars
    star
    173
  • Rank 220,124 (Top 5 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 9 years ago
  • Updated almost 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Shutdown a Nodejs HTTP server gracefully by terminating the listening socket, then destroying all keep-alive idle sockets all while allowing in-flight requests to finish.

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

More Repositories

1

CodeBucket

CodeBucket is the best way to browse and maintain your Bitbucket repositories on any iPhone, iPod Touch, and iPad device!
C#
198
star
2

RepoStumble

A mobile application for viewing GitHub repositories in a fashion similar to StumbleUpon
C#
153
star
3

rails-angularjs-simple-forum

A simple demonstration between a Rails backend and AngularJS front end: creating a simple forum.
Ruby
81
star
4

twitter-cashtag-heatmap

A demonstration of using Node.js with Twitter's streaming capability to create a live heatmap via Socket.io!
JavaScript
61
star
5

GitHub-Trending

Scrapes GitHub's Trending Pages for use in CodeHub
TypeScript
48
star
6

MonoTouch.SlideoutNavigation

A MonoTouch slide-out UI component
C#
40
star
7

Gistacular

A clean and simple iOS client for GitHub's Gist
C#
25
star
8

Owin.HealthCheck

Health Check Middleware for the OWIN pipeline
C#
23
star
9

appreciateui

AppreciateUI: Mobile Application UI Designs
C#
20
star
10

GitHubSharp

A C# Client for the GitHub API
C#
17
star
11

appreciateui-web

The front-end website to AppreciateUI
JavaScript
17
star
12

Xamarin.Utilities

A medley of utilities for my xamarin projects
C#
11
star
13

CodeFramework

A collection of framework classes for the Code mobile applications
C#
11
star
14

Owin.ServiceStack

Host ServiceStack (v3) as OWIN middleware
C#
10
star
15

CodeStash

An iOS client for Atlassian's Stash
C#
8
star
16

BitbucketSharp

A C# Client for the Bitbucket REST API
C#
8
star
17

BoxMusicPlayer-Web

A Music player written in jQuery Mobile and PHP that uses Box.com as a back-end to serve music files
JavaScript
7
star
18

AtlassianStashSharp

A C# Client Library for Atlassian's Stash
C#
4
star
19

thedillonb.github.io

Personal Page
HTML
4
star
20

santana

Go
3
star
21

Shame-Clock

Countdown to shame!
HTML
3
star
22

holla

Generate configuration files based on dynamic data
2
star
23

SkinDefenderApp

The Skin Defender Application
C#
1
star
24

CodingShame

The Jekyll Site for CodingShame.com
HTML
1
star
25

scribe

Manages file writing and rotation of stdout
Rust
1
star