1.1.0 • Published 5 years ago

stoppable v1.1.0

Weekly downloads
302,611
License
MIT
Repository
github
Last release
5 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-app@azure/identitylec-multitenant-apibuerli-nodetonapimagicaeonyxlec-integrations-software-consumerlec-integrations-tracking-consumerlec-integrations-queue-consumer@spokedev/fab_utilslec-event-logs-api@infinitebrahmanuniverse/nolb-stop@canvas-js/nextunleash-server@everything-registry/sub-chunk-2828@notchjs/express@notchjs/notch@nowtjs/express@eigr/spawn-sdk@ekingdom/drakemall-payment-gateway@ekingdom/payment-gateway@flexent/http-serverflanc@backstage/backend-app-api7ghost@appium/driver-test-support@arso-project/sonar-server@arsonar/server@conneryn/c6o-cli@conneryn/cli@conneryn/cli-bundle@conneryn/cli-test@conneryn/cli-tmptortilla-apigraceful-sd@dollarshaveclub/nodejs-servertwitch-intl-cli1unleash-server-openapientangled-statesentangled-states-reactentangled-states-web@acaelum/cakessh2-tunnelhw9@dha_utilities/utilitiestapc-plugin-publishtest-ipfs-example@ciro99678_net/node-red-contrib-mssql-rai@ciro99678_net/red-contrib-mssql-rai@amplitude/ampli@andywer/srvexpress-raresextconsole@classcad/nodeghosttest-util-ipfs-example@canvas-js/cli@c6o/cli@c6o/daemon@browserstack/identity@buerli.io/node@braid/nuxt-drupal-jsonapitetiaroawebsocket-pubsubwebsocket-pubsub-reactwebsocket-pubsub-webronin-server@gabrielcoderz/perspective@player1os/express-utility@prerenderer/prerenderer@rianfowler/backstage-backend-common@stringstack/express@solusoft_es/terminus@service-t/webserver@rypock/express-backend@ssb-graphql/stats@rplan/express-middleware@seagull-js/seagull-cli@seagull/build@seagull/cli@subsquid/util-internal-http-server@finos/perspective@tuchuang.space/mock-server@tueenguyen/generous@tramvai/cli@ubio/framework@typeofweb/server@tinkoff/terminus@tinkoff/express-terminus@tracktl/terminus@transomjs/transom-graceful@weweb-prerender/prerendererpoc-cloudflare-middlewarecbase-shellpolly-proxycorvid-local-server@xiaxue008/mockdbremote-adbdashundredis-smq-monitor
1.1.0

5 years ago

1.0.7

5 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago