Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org »

    under-pressurepublic

    under-pressure

    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.

    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.

    Acknowledgements

    This project is kindly sponsored by LetzDoIt.

    License

    Licensed under MIT.

    install

    npm i under-pressure

    Downloadsweekly downloads

    7

    version

    0.1.0

    license

    MIT

    last publish

    collaborators

    • avatar
    • avatar