Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

under-pressure

Package Overview
Dependencies
Maintainers
8
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

under-pressure

Measure process load with automatic handling of 'Service Unavailable' plugin for Fastify.

  • 3.2.0
  • Source
  • npm
  • Socket score

Version published
Maintainers
8
Created
Source

under-pressure

Greenkeeper badge js-standard-style Build Status

Measure process load with automatic handling of "Service Unavailable" plugin for Fastify. It can check maxEventLoopDelay, maxHeapUsedBytes and maxRssBytes values. You can also specify custom health check, to verify the status of external resources.

Requirements

Fastify ^2.0.0. Please refer to this branch and related versions for Fastify ^1.1.0 compatibility.

Install

npm i under-pressure --save

Usage

Require the plugin and register it into the Fastify instance.

const fastify = require('fastify')()

fastify.register(require('under-pressure'), {
  maxEventLoopDelay: 1000,
  maxHeapUsedBytes: 100000000,
  maxRssBytes: 100000000
})

fastify.get('/', (req, reply) => {
  reply.send({ hello: 'world'})
})

fastify.listen(3000, err => {
  if (err) throw err
  console.log(`server listening on ${fastify.server.address().port}`)
})

under-pressure will automatically handle for you the Service Unavailable error once one of the thresholds has been reached.
You can configure the error message and the Retry-After header.

fastify.register(require('under-pressure'), {
  maxEventLoopDelay: 1000,
  message: 'Under pressure!',
  retryAfter: 50
})

The default value for maxEventLoopDelay, maxHeapUsedBytes and maxRssBytes is 0.
If the value is 0 the check will not be performed.

Thanks to the encapsulation model of Fastify, you can selectively use this plugin in some subset of routes or even with different thresholds in different plugins.

memoryUsage

This plugin also exposes a function that will tell you the current values of heapUsed, rssBytes and eventLoopDelay.

console.log(fastify.memoryUsage())
Status route

If needed you can pass { exposeStatusRoute: true } and under-pressure will expose a /status route for you that sends back a { status: 'ok' } object. This can be useful if you need to attach the server to an ELB on AWS for example.

If you need the change the exposed route path, you can pass { exposeStatusRoute: '/alive' } options.

If you need to pass options to the status route, such as logLevel or custom configuration you can pass an object,

fastify.register(require('under-pressure'), {
  maxEventLoopDelay: 1000,
  exposeStatusRoute: {
    routeOpts: {
      logLevel: 'debug',
      config: {
        someAttr: 'value'
      }
    },
    url: '/alive' // If you also want to set a custom route path and pass options
  }
})

The above example will set the logLevel value for the /status route be debug.

Custom health checks

If needed you can pass a custom healthCheck property which is an async function and under-pressure will allow you to check the status of other components of your service.

This function should return a promise which resolves to a boolean value. The healthCheck function can be called either:

  • every X milliseconds, the time can be configured with the healthCheckInterval option.
  • every time the status route is called, if exposeStatusRoute is set to true.

By default when this function is supplied your service health is considered unhealthy, until it has started to return true.

const fastify = require('fastify')()

fastify.register(require('under-pressure'), {
  healthCheck: async function () {
    // do some magic to check if your db connection is healthy, etc...
    return true
  },
  healthCheckInterval: 500
})

Acknowledgements

This project is kindly sponsored by LetzDoIt.

License

Licensed under MIT.

Keywords

FAQs

Package last updated on 29 Nov 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc