Skip to content

toppr/node-server-shutdown

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Server Shutdown

Dependency Status Build Status Coverage Status NPM version GitHub license

Using just server.close only terminates the server once every connection is closed. This is problematic since, by design, keep-alive connections can continue to hold the server open, and WebSockets can hold the connection open for extended periods of time. A naive solution forcefully destroys all the sockets, interrupting any inflight requests. Another solution is to server.unref the server, but this isn't a satisfactory solution as it does not allow the close event to be used.

This library solves this problem by tracking when a connection is busy, using request for HTTP connections, and hooking into write in the case of WebSockets. The server shutdowns by first stopping any additional connections being made, closing any idle HTTP and WebSocket connections, closing any busy HTTP connections once the inflight request has completed, and closing WebSocket connections on finish of a write.

Install

npm install server-shutdown

Usage

const http = require('http');
const https = require('https');
const ServerShutdown = require('server-shutdown');
const serverShutdown = new ServerShutdown();

const httpServer = http.createServer((req, res) => {
    res.end('HTTP response');
}).listen(80);

const httpsServer = https.createServer((req, res) => {
    res.end('HTTPS response');
}).listen(443);

serverShutdown.registerServer(httpServer);
serverShutdown.registerServer(httpsServer);

process.on('SIGTERM', () => {
    serverShutdown.shutdown(() => {
        console.log('All servers shutdown gracefully');
    })
}));

Adding Socket.io

// continuing from basic uasge
const socketio = require('socket.io');
const io = socketio(httpServer);
serverShutdown.registerServer(io, ServerShutdown.Adapters.socketio);

API

Methods

ServerShutdown.registerServer(server[, adapterName = ServerShutdown.Adapters.http])

Registers a server with the library. The adapter name argument is used to set the type of server being registered.

ServerShutdown.shutdown([callback])

Shutdown all the servers registered. The callback is called once all connections are disconnected and servers are closed.

ServerShutdown.forceShutdown([callback])

Shutdown all the servers registered with all connections forcefully disconnected. The callback is called once all connections are disconnected and servers are closed.

ServerShutdown.registerAdapter(name, adapter)

Register a server adapter with the system. Name should be a string and adapter is an object that contains a close(server, callback) function that is responsible for closing the server and a socketClose(socket) function that is responsible for destroying the sockets the server creates.

Constants

ServerShutdown.Adapters.http

The adapter name for the http adapter. Used with ServerShutdown.registerAdapter.

ServerShutdown.Adapters.socketio

The adapter name for the Socket.io adapter. Used with ServerShutdown.registerAdapter.

Development

Debugging

This library uses debug to produce debugging output. To enable add DEBUG=server-shutdown before your run command.

License

This project is released under the ISC license. See LICENSE.

About

Safely shutdown any number of HTTP and WebSocket servers in node

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages

  • JavaScript 100.0%