• Stars
    star
    403
  • Rank 107,140 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 7 years ago
  • Updated about 1 year ago

Reviews

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

Repository Details

Node's `server.close` the way you expected it to work.

Stoppable

Build Status

Node's server.close() the way you probably expected it to work by default.

Summary

const server = stoppable(http.createServer(handler))
server.stop()

Stoppable stops accepting new connections and closes existing, idle connections (including keep-alives) without killing requests that are in-flight.

Requirements

  • Node.js v6+

Node.js v4.x is unofficially supported.

Installation

yarn add stoppable

(or use npm)

Usage

constructor

stoppable(server, grace)

Decorates the server instance with a stop method. Returns the server instance, so can be chained, or can be run as a standalone statement.

  • server: Any HTTP or HTTPS Server instance
  • grace: Milliseconds to wait before force-closing connections

grace defaults to Infinity (don't force-close). If you want to immediately kill all sockets you can use a grace of 0.

stop()

server.stop(callback)

Closes the server.

  • callback: passed along to the existing server.close function to auto-register a 'close' event. The first agrument is an error, and the second argument is a boolean that indicates whether it stopped gracefully.

Design decisions

  • Monkey patching generally sucks, but in this case it's the nicest API. Let's call it "decorating."
  • grace could be specified on stop, but it's better to match the existing server.close API.
  • Clients should be handled respectfully, so we aren't just destroying sockets, we're sending FIN packets first.
  • Any solution to this problem requires bookkeeping on every connection and request/response. We're doing a minimum of work on these "hot" code paths and delaying as much as possible to the actual stop method.

Performance

There's no way to provide this functionality without bookkeeping on connection, disconnection, request, and response. However, Stoppable strives to do minimal work in hot code paths and to use optimal data structures.

I'd be interested to see real-world performance benchmarks; the simple loopback artillery benchmark included in the lib shows very little overhead from using a stoppable server:

Without Stoppable

  Scenarios launched:  10000
  Scenarios completed: 10000
  Requests completed:  10000
  RPS sent: 939.85
  Request latency:
    min: 0.5
    max: 51.3
    median: 2.1
    p95: 3.7
    p99: 15.3
  Scenario duration:
    min: 1
    max: 60.7
    median: 3.6
    p95: 7.6
    p99: 19
  Scenario counts:
    0: 10000 (100%)
  Codes:
    200: 10000

With Stoppable

  Scenarios launched:  10000
  Scenarios completed: 10000
  Requests completed:  10000
  RPS sent: 940.73
  Request latency:
    min: 0.5
    max: 43.4
    median: 2.1
    p95: 3.8
    p99: 15.5
  Scenario duration:
    min: 1.1
    max: 57
    median: 3.7
    p95: 8
    p99: 19.4
  Scenario counts:
    0: 10000 (100%)
  Codes:
    200: 10000

License

MIT

More Repositories

1

pbr

a Physically Based Renderer (PBR) in Go
Go
1,136
star
2

newton

A playful, particle-based physics engine designed from the ground up for JavaScript.
JavaScript
917
star
3

throng

A simple worker-manager for clustered Node.js apps
JavaScript
856
star
4

playfuljs-demos

680
star
5

awaiting

The async/await utility for browsers and Node.js.
JavaScript
674
star
6

playfuljs

www.playfuljs.com
CSS
520
star
7

jackrabbit

Simple AMQP / RabbitMQ job queues for node based on amqplib
JavaScript
293
star
8

cryo

JSON on steroids.
JavaScript
157
star
9

oneweekend

Ray Tracing book series implemented in Golang, chapter-by-chapter
Go
153
star
10

notes

Notes about things.
147
star
11

knockout.namespaces

Namespaces plugin for KnockoutJS
JavaScript
57
star
12

pathtracer

A simple, naive path tracer in JavaScript
JavaScript
50
star
13

summarize

Node.js module to extract and summarize html content
JavaScript
41
star
14

pbr2

Go
38
star
15

component-test

An experiment to see what a simple node app would look like with "Components" from the @visionmedia blog
JavaScript
29
star
16

nodevember-14

JavaScript
28
star
17

heroku-node-errcodes

Examples of intentionally triggering various Heroku H* errors with Node.js
JavaScript
21
star
18

heroku-destroy-temp

Heroku CLI plugin to destroy temporary apps.
JavaScript
13
star
19

ludumstar

JavaScript
12
star
20

backbone.viewmodel

ViewModels with UI Bindings for Backbone (ala KnockoutJS, Flex, .NET, MVVM pattern)
JavaScript
7
star
21

dotfiles

personal dotfiles (steam deck dev machine)
Shell
7
star
22

itemize

A lazy, fluent web crawler for Node.js with a modern async/await API.
JavaScript
6
star
23

heroku-cli-node

A Heroku CLI plugin for Node.js app development
JavaScript
5
star
24

tetrinet

JavaScript
5
star
25

node-boilerplate

Structure for your node.js project
JavaScript
4
star
26

lanes

Simple, generic, sticky routing for clustered Node.js apps
JavaScript
4
star
27

server-jsx

Render react views on your node.js server
JavaScript
4
star
28

blit

2D Sprites that render to WebGL
JavaScript
4
star
29

cltjs-node

Node.js in 30 minutes
JavaScript
3
star
30

nko-quickstart

"Hello, world" with deployment instructions.
JavaScript
3
star
31

mongoose-file

Attach a file to a mongoose Schema
JavaScript
3
star
32

docker-plugin

JavaScript
3
star
33

babylonterrain

Testing babylonjs with terrain generation
JavaScript
3
star
34

music-city-game

Workshop for Music City Code 2019
JavaScript
3
star
35

storj

An attempt at the interesting Storj challenge
Go
2
star
36

blitzkrieg

Middleware to provide an authorized domain for blitz.io load testing in Node.js.
JavaScript
2
star
37

space-snake

Ludum Dare 38: A Small World
JavaScript
2
star
38

node-production

Running Node all the way from development to production on Heroku.
JavaScript
2
star
39

okay.js

A knockout knockoff
JavaScript
2
star
40

get-large-json

Test for getting large in-memory json objects
JavaScript
2
star
41

hunterloftis.com

Personal homepage
JavaScript
2
star
42

heroku-buildpack-sfdx

Shell
1
star
43

heroku-buildpack-empty

Quick example of a no-op buildpack
Shell
1
star
44

simplenode

A simple boilerplate for single-page node projects deployable on Ubuntu VPS.
JavaScript
1
star
45

heroku-node-template

Easily template new node projects with best practices for Heroku.
JavaScript
1
star
46

ld39

JavaScript
1
star
47

backpacksncats

Our blog
CSS
1
star
48

Quarry

JavaScript
1
star
49

node-dev-env

Dockerfile
1
star
50

arduino_wiimotionplus

Reading wii motionplus data with the arduino
C++
1
star
51

handoff

JavaScript
1
star
52

socket.io-chat-distributed

Socket.io chat scaled across multiple nodes, backed by redis
HTML
1
star
53

df-micro-web

JavaScript
1
star
54

deno-pointers

Testing out Deno with a little state
TypeScript
1
star
55

game-workshop

JavaScript
1
star
56

dogfight

JavaScript
1
star
57

mongoose-timestamps

A simple mongoose plugin for storing last created and last modified information
1
star
58

loopbusy

Node.js middleware to send 503s and keep your server alive when it's too busy to queue more requests.
JavaScript
1
star
59

processing_wiimotionplus

Renders position data sent over serial by the WMP Arduino code
Java
1
star