All Projects → the-moebius → http-graceful-shutdown

the-moebius / http-graceful-shutdown

Licence: other
Gracefully terminates HTTP servers in Node.js

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to http-graceful-shutdown

remoting
Jetlang Remoting - asynchronous distributed messaging
Stars: ✭ 27 (-65.82%)
Mutual labels:  http-server
zipstream
A command line tool that allows you to easily share files and directories over the network
Stars: ✭ 49 (-37.97%)
Mutual labels:  http-server
breaker
🚧 Flexible mechanism to make execution flow interruptible.
Stars: ✭ 100 (+26.58%)
Mutual labels:  graceful-shutdown
rux
⚡ Rux is an simple and fast web framework. support route group, param route binding, middleware, compatible http.Handler interface. 简单且快速的 Go api/web 框架,支持路由分组,路由参数绑定,中间件,兼容 http.Handler 接口
Stars: ✭ 81 (+2.53%)
Mutual labels:  http-server
AutoIt-HTTP-Server
A simple http server powered by AutoIt
Stars: ✭ 14 (-82.28%)
Mutual labels:  http-server
http-server-static-content
Static content / file serving for Amp's HTTP server.
Stars: ✭ 19 (-75.95%)
Mutual labels:  http-server
node-graceful-shutdown
Gracefully shutdown your modular NodeJS application.
Stars: ✭ 20 (-74.68%)
Mutual labels:  graceful-shutdown
NETMF-Toolkit
The .NET Micro Framework Toolkit (using Microsoft .NET Micro Framework)
Stars: ✭ 13 (-83.54%)
Mutual labels:  http-server
Network-Learn
A cross-platform network learning demos. Like high-performance http server
Stars: ✭ 23 (-70.89%)
Mutual labels:  http-server
stream video server
demonstrates how to create video streaming server with the help of aiohttp and opencv
Stars: ✭ 15 (-81.01%)
Mutual labels:  http-server
sigctx
Go contexts for graceful shutdown
Stars: ✭ 55 (-30.38%)
Mutual labels:  graceful-shutdown
zhamao-framework
协程、高性能、灵活的聊天机器人 & Web 开发框架(炸毛框架)
Stars: ✭ 99 (+25.32%)
Mutual labels:  http-server
simple http server
simple http server for upload and download
Stars: ✭ 101 (+27.85%)
Mutual labels:  http-server
http4s-good-practices
Collection of what I consider good practices in Http4s (WIP)
Stars: ✭ 74 (-6.33%)
Mutual labels:  http-server
zenith
⚡ Functional Scala HTTP server, client, and toolkit.
Stars: ✭ 15 (-81.01%)
Mutual labels:  http-server
Funk
Funk is a HTTP server implementation of PHPSGI. Implemented the lightweight middlewares, responder components...
Stars: ✭ 16 (-79.75%)
Mutual labels:  http-server
dummyhttp
Super simple HTTP server that replies a fixed body with a fixed response code
Stars: ✭ 25 (-68.35%)
Mutual labels:  http-server
ZiAPI
The elected Zia API for {Epitech} promo 2024 (Paris and Marseille)
Stars: ✭ 29 (-63.29%)
Mutual labels:  http-server
hunt-http
http library for D, support http 1.1 / http 2.0 (http2) / websocket server and client.
Stars: ✭ 29 (-63.29%)
Mutual labels:  http-server
aiohttp-json-rpc
Implements JSON-RPC 2.0 using aiohttp
Stars: ✭ 54 (-31.65%)
Mutual labels:  http-server

@moebius/http-graceful-shutdown

npm version

This package for Node.js provides you with easy to use facilities required to gracefully terminate HTTP servers.

It's designed to work with native http.Server implementation, so it could be used for any server build on top of it (i.e. Express and other). See examples below.

Features

  • Keeps track of all open connections and their status (active/idle)
  • Gracefully terminates all connections by terminating the idle ones right away and waiting for pending requests to complete
  • Very straightforward and simple API to use
  • Minimal working implementation possible without extraneous functionality
  • Written in TypeScript, but supports vanilla JavaScript

Problem

When your application's process is interrupted by the operating system (by passing SIGINT or SIGTERM signals) by default the server is terminated right away and all open connections are brutally severed. This means that if some client was in the process of sending or receiving data from your server it will encounter an error. This could easily lead to escalating errors down the chain and data corruption.

The better strategy would be to listen for interruption signals and to close connections gracefully by closing only idle connections right away and waiting for pending requests to properly complete.

This module solves exactly this problem (or at least the second part of it).

Installation

Install package with yarn:

yarn add @moebius/http-graceful-shutdown

Or npm:

npm i -S @moebius/http-graceful-shutdown

Usage

Vanilla Server

const http = require('http');  
const GracefulShutdownManager = require('@moebius/http-graceful-shutdown').GracefulShutdownManager;


const server = http.createServer(/** … */);
const shutdownManager = new GracefulShutdownManager(server);

server.listen(8080);

process.on('SIGTERM', () => {
  shutdownManager.terminate(() => {
    console.log('Server is gracefully terminated');
  });
});

Express Server

const express = require('express');
const GracefulShutdownManager = require('@moebius/http-graceful-shutdown').GracefulShutdownManager;


const app = express();

const server = app.listen(8080);

const shutdownManager = new GracefulShutdownManager(server);

process.on('SIGTERM', () => {
  shutdownManager.terminate(() => {
    console.log('Server is gracefully terminated');
  });
});

API

GracefulShutdownManager Class

  • construct (server: http.Server)

    Creates an instance of GracefulShutdownManager class.

    The server argument is a Node.js HTTP server instance, i.e. http.Server. If you are using Express, you can obtain it like this: const server = app.listen(/** … */);

  • terminate(callback: Function)

    Initiates graceful termination of the server by closing all idle connections first and then by waiting for pending requests to finish. New requests are not accepted after calling this.

Support

If you like this module please consider to add a star on GitHub repository.

Thank you!

License

The MIT License (MIT)

Copyright (c) 2017 Slava Fomin II, MOEBIUS FOUNDATION.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].