1.1.0 • Published 6 years ago

stoppable v1.1.0

Weekly downloads
302,611
License
MIT
Repository
github
Last release
6 years ago

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

yubo-applec-multitenant-apibuerli-nodetonapimagicaeonyxlec-integrations-software-consumerlec-integrations-tracking-consumerlec-integrations-queue-consumer@spokedev/fab_utilslec-event-logs-api@infinitebrahmanuniverse/nolb-stop@canvas-js/next@everything-registry/sub-chunk-2828@notchjs/notch@nowtjs/expresstapc-plugin-publishtest-ipfs-exampleremote-adbronin-servertest-util-ipfs-exampletetiaroamegacubominiflaremock-ssolongpastehw9graceful-sdkashakasha-proxyghostnear-clinear-shellnode-red-contrib-auroralnode-red-contrib-google-smarthomenode-red-contrib-local-alexa-devicespolly-proxynode-express-docker-starterpoc-cloudflare-middlewarenode-red-contrib-huebridgenode-red-contrib-alexa-localnode-red-contrib-alexa-virtual-smarthomenode-red-contrib-amazon-echonode-red-contrib-amazon-echo-allernode-red-contrib-amazon-echo-extnode-red-contrib-amazon-echo-oztourernode-red-contrib-amazon-echo-theukshadownuxt-drupal-jsonapinshakhat_ghostnode-red-node-test-helperquadstore-httpredis-smq-monitorssh2-tunneltwitch-intl-cli1tortilla-apiunleash-serverunleash-server-openapi@arso-project/sonar-server@arsonar/server@appium/driver-test-support@andywer/srv@amplitude/ampli@backstage/backend-common@backstage/backend-defaults@backstage/backend-app-api@azure/identity@buerli.io/node@c6o/cli@c6o/daemon@browserstack/identity@ciro99678_net/node-red-contrib-mssql-rai@ciro99678_net/red-contrib-mssql-rai@classcad/node7ghost@acaelum/cake@braid/nuxt-drupal-jsonapi@dha_utilities/utilitieszeppos-app-utilswebsocket-pubsubwebsocket-pubsub-reactwebsocket-pubsub-web@finos/perspective@grund/server-express@hellomd/node-sdk@igames/ghost@flexent/http-server@ekingdom/drakemall-payment-gateway@ekingdom/payment-gateway@eigr/spawn-sdk@gabrielcoderz/perspective@godaddy/terminus@infomaker/http-test-server@inquirescript/cli@labshare/services@krknet/http-server@kube-js/kube-ts-server@loopback/http-server@itly/itly@lifeomic/cli@mablhq/mabl-cli@mablhq/playwright-tools
1.1.0

6 years ago

1.0.7

6 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

8 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago