npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

graceful-http

v1.0.1

Published

HTTP graceful shutdown, needed to achieve zero-downtime doing restarts.

Downloads

2,071

Readme

graceful-http

HTTP graceful shutdown, needed to achieve zero-downtime doing restarts.

npm i graceful-http

Allows all requests to finish without interruption.
It also end sockets gracefully using the Connection header.

For zero-downtime you must have more processes (PM2 cluster or something else).

Usage

const express = require('express')
const graceful = require('graceful-http')

const app = express()

app.get('/', function (req, res) {
  res.send('ok')
})

const server = app.listen(3000)
const close = graceful(server)

process.once('SIGINT', async function () {
  try {
    await close()
    // + here close database, etc
    // process.exit(0) // pm2 cluster requires to exit the process
  } catch (error) {
    console.error(error)
    // process.exit(1)
  }
})

Normal request (less than 60s)

Let's say you have a request that always takes several seconds.
All pending requests are automatically handled for you.

app.get('/long-request', async function (req, res) {
  await sleep(1000) // ie. fetching stock prices
  // + ie. here PM2 sends SIGINT signal
  await sleep(1000) // ie. querying a remote database

  res.send('ok')
})

Long request (more than 60s)

Use graceful.check(res) to know if the server is closing,
this way you can send the response early. Useful for long-polling.

app.get('/long-polling', async function (req, res) {
  for (let i = 0; i < 75; i++) {
    // checks if server is closing
    if (graceful.check(res)) { // Koa: ctx.res
      break
    }

    await sleep(1000)
  }

  res.send('ok')
})

This check is necessary because graceful-http has an internal timeout.
After 60s of close() it forcefully close all sockets.
It's needed because there could be clients not respecting the Connection header.

Express: graceful.check(res)
Koa: graceful.check(ctx.res)

Default configuration

const close = graceful(server, {
  endIdle: 15000,
  forceEnd: 60000,
  loopResponses: true
})

endIdle will end sockets with no pending requests.
Default Node HTTP keep-alive timeout is 5000 ms.
Keep endIdle higher than the keep-alive timeout.

forceEnd will end sockets even with pending requests.
You must set forceEnd higher than the most longer request.

loopResponses it's a tweak where automatically handles pending requests
to more quickly free sockets from the server.
Disable it if you have like fifty thousand of requests concurrently and care about latency.
This one time minimal latency is at the moment of close().

It's only for HTTP, and not HTTPS either HTTP2.

Credits to Dashlane

https://blog.dashlane.com/implementing-nodejs-http-graceful-shutdown/

License

MIT