Launch Week Day 5: Introducing Reachability for PHP.Learn More
Socket
Book a DemoSign in
Socket

under-pressure

Package Overview
Dependencies
Maintainers
17
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

under-pressure - npm Package Compare versions

Comparing version
6.0.0
to
6.1.0
+4
-260
index.js
'use strict'
const fe = require('@fastify/error')
const fp = require('fastify-plugin')
const assert = require('assert')
const { monitorEventLoopDelay } = require('perf_hooks')
const { eventLoopUtilization } = require('perf_hooks').performance
const warning = require('process-warning')()
warning.create('FastifyWarning.under-pressure', 'FST_MODULE_DEP_under-pressure'.toUpperCase(), 'under-pressure has been deprecated. Use @fastify/under-pressure@7.0.0 instead.')
warning.emit('FST_MODULE_DEP_under-pressure'.toUpperCase())
const SERVICE_UNAVAILABLE = 503
const createError = (msg = 'Service Unavailable') => fe('FST_UNDER_PRESSURE', msg, SERVICE_UNAVAILABLE)
const TYPE_EVENT_LOOP_DELAY = 'eventLoopDelay'
const TYPE_HEAP_USED_BYTES = 'heapUsedBytes'
const TYPE_RSS_BYTES = 'rssBytes'
const TYPE_HEALTH_CHECK = 'healthCheck'
const TYPE_EVENT_LOOP_UTILIZATION = 'eventLoopUtilization'
function getSampleInterval (value, eventLoopResolution) {
const defaultValue = monitorEventLoopDelay ? 1000 : 5
const sampleInterval = value || defaultValue
return monitorEventLoopDelay ? Math.max(eventLoopResolution, sampleInterval) : sampleInterval
}
async function underPressure (fastify, opts) {
opts = opts || {}
const resolution = 10
const sampleInterval = getSampleInterval(opts.sampleInterval, resolution)
const maxEventLoopDelay = opts.maxEventLoopDelay || 0
const maxHeapUsedBytes = opts.maxHeapUsedBytes || 0
const maxRssBytes = opts.maxRssBytes || 0
const healthCheck = opts.healthCheck || false
const healthCheckInterval = opts.healthCheckInterval || -1
const UnderPressureError = opts.customError || createError(opts.message)
const maxEventLoopUtilization = opts.maxEventLoopUtilization || 0
const pressureHandler = opts.pressureHandler
const checkMaxEventLoopDelay = maxEventLoopDelay > 0
const checkMaxHeapUsedBytes = maxHeapUsedBytes > 0
const checkMaxRssBytes = maxRssBytes > 0
const checkMaxEventLoopUtilization = eventLoopUtilization ? maxEventLoopUtilization > 0 : false
let heapUsed = 0
let rssBytes = 0
let eventLoopDelay = 0
let lastCheck
let histogram
let elu
let eventLoopUtilized = 0
if (monitorEventLoopDelay) {
histogram = monitorEventLoopDelay({ resolution })
histogram.enable()
} else {
lastCheck = now()
}
if (eventLoopUtilization) {
elu = eventLoopUtilization()
}
fastify.decorate('memoryUsage', memoryUsage)
const timer = setInterval(updateMemoryUsage, sampleInterval)
timer.unref()
let externalsHealthy = false
let externalHealthCheckTimer
if (healthCheck) {
assert(typeof healthCheck === 'function', 'opts.healthCheck should be a function that returns a promise that resolves to true or false')
assert(healthCheckInterval > 0 || opts.exposeStatusRoute, 'opts.healthCheck requires opts.healthCheckInterval or opts.exposeStatusRoute')
const doCheck = async () => {
try {
externalsHealthy = await healthCheck(fastify)
} catch (error) {
externalsHealthy = false
fastify.log.error({ error }, 'external healthCheck function supplied to `under-pressure` threw an error. setting the service status to unhealthy.')
}
}
await doCheck()
if (healthCheckInterval > 0) {
externalHealthCheckTimer = setInterval(doCheck, healthCheckInterval)
externalHealthCheckTimer.unref()
}
} else {
externalsHealthy = true
}
fastify.addHook('onClose', onClose)
opts.exposeStatusRoute = mapExposeStatusRoute(opts.exposeStatusRoute)
if (opts.exposeStatusRoute) {
fastify.route({
...opts.exposeStatusRoute.routeOpts,
url: opts.exposeStatusRoute.url,
method: 'GET',
schema: Object.assign({}, opts.exposeStatusRoute.routeSchemaOpts, {
response: {
200: {
type: 'object',
properties: Object.assign(
{ status: { type: 'string' } },
opts.exposeStatusRoute.routeResponseSchemaOpts
)
}
}
}),
handler: onStatus
})
}
if (checkMaxEventLoopUtilization === false && checkMaxEventLoopDelay === false &&
checkMaxHeapUsedBytes === false &&
checkMaxRssBytes === false &&
healthCheck === false) {
return
}
const underPressureError = new UnderPressureError()
const retryAfter = opts.retryAfter || 10
fastify.addHook('onRequest', onRequest)
function mapExposeStatusRoute (opts) {
if (!opts) {
return false
}
if (typeof opts === 'string') {
return { url: opts }
}
return Object.assign({ url: '/status' }, opts)
}
function updateEventLoopDelay () {
if (histogram) {
eventLoopDelay = Math.max(0, histogram.mean / 1e6 - resolution)
if (Number.isNaN(eventLoopDelay)) eventLoopDelay = Infinity
histogram.reset()
} else {
const toCheck = now()
eventLoopDelay = Math.max(0, toCheck - lastCheck - sampleInterval)
lastCheck = toCheck
}
}
function updateEventLoopUtilization () {
if (elu) {
eventLoopUtilized = eventLoopUtilization(elu).utilization
} else {
eventLoopUtilized = 0
}
}
function updateMemoryUsage () {
const mem = process.memoryUsage()
heapUsed = mem.heapUsed
rssBytes = mem.rss
updateEventLoopDelay()
updateEventLoopUtilization()
}
function onRequest (req, reply, next) {
if (checkMaxEventLoopDelay && eventLoopDelay > maxEventLoopDelay) {
handlePressure(req, reply, next, TYPE_EVENT_LOOP_DELAY, eventLoopDelay)
return
}
if (checkMaxHeapUsedBytes && heapUsed > maxHeapUsedBytes) {
handlePressure(req, reply, next, TYPE_HEAP_USED_BYTES, heapUsed)
return
}
if (checkMaxRssBytes && rssBytes > maxRssBytes) {
handlePressure(req, reply, next, TYPE_RSS_BYTES, rssBytes)
return
}
if (!externalsHealthy) {
handlePressure(req, reply, next, TYPE_HEALTH_CHECK)
return
}
if (checkMaxEventLoopUtilization && eventLoopUtilized > maxEventLoopUtilization) {
handlePressure(req, reply, next, TYPE_EVENT_LOOP_UTILIZATION, eventLoopUtilized)
return
}
next()
}
function handlePressure (req, reply, next, type, value) {
if (typeof pressureHandler === 'function') {
const result = pressureHandler(req, reply, type, value)
if (result instanceof Promise) {
result.then(() => next(), next)
} else if (result == null) {
next()
} else {
reply.send(result)
}
} else {
reply.status(SERVICE_UNAVAILABLE).header('Retry-After', retryAfter)
next(underPressureError)
}
}
function memoryUsage () {
return {
eventLoopDelay,
rssBytes,
heapUsed,
eventLoopUtilized
}
}
async function onStatus (req, reply) {
const okResponse = { status: 'ok' }
if (healthCheck) {
try {
const checkResult = await healthCheck(fastify)
if (!checkResult) {
req.log.error('external health check failed')
reply.status(SERVICE_UNAVAILABLE).header('Retry-After', retryAfter)
throw underPressureError
}
return Object.assign(okResponse, checkResult)
} catch (err) {
req.log.error({ err }, 'external health check failed with error')
reply.status(SERVICE_UNAVAILABLE).header('Retry-After', retryAfter)
throw underPressureError
}
}
return okResponse
}
function onClose (fastify, done) {
clearInterval(timer)
clearInterval(externalHealthCheckTimer)
done()
}
}
function now () {
const ts = process.hrtime()
return (ts[0] * 1e3) + (ts[1] / 1e6)
}
module.exports = fp(underPressure, {
fastify: '4.x',
name: 'under-pressure'
})
module.exports.TYPE_EVENT_LOOP_DELAY = TYPE_EVENT_LOOP_DELAY
module.exports.TYPE_EVENT_LOOP_UTILIZATION = TYPE_EVENT_LOOP_UTILIZATION
module.exports.TYPE_HEALTH_CHECK = TYPE_HEALTH_CHECK
module.exports.TYPE_HEAP_USED_BYTES = TYPE_HEAP_USED_BYTES
module.exports.TYPE_RSS_BYTES = TYPE_RSS_BYTES
module.exports = require('under-pressure-deprecated')
{
"name": "under-pressure",
"version": "6.0.0",
"description": "Measure process load with automatic handling of 'Service Unavailable' plugin for Fastify.",
"version": "6.1.0",
"main": "index.js",
"scripts": {
"lint": "standard | snazzy",
"unit": "tap -j 1 test/*test.js",
"test": "npm run lint && npm run unit && npm run typescript",
"typescript": "tsd"
},
"license": "MIT",
"repository": {
"type": "git",
"url": "git+https://github.com/fastify/under-pressure.git"
"url": "git://github.com/fastify/under-pressure.git"
},
"bugs": {
"url": "https://github.com/fastify/under-pressure/issues"
},
"keywords": [
"fastify",
"service unavailable",
"limit",
"delay",
"retry"
],
"author": "Tomas Della Vedova - @delvedor (http://delved.org)",
"license": "MIT",
"homepage": "https://github.com/fastify/under-pressure",
"dependencies": {
"@fastify/error": "^2.0.0",
"fastify-plugin": "^3.0.0"
},
"devDependencies": {
"@types/node": "^17.0.7",
"fastify": "^4.0.0-rc.2",
"pre-commit": "^1.2.2",
"semver": "^7.3.2",
"simple-get": "^4.0.0",
"snazzy": "^9.0.0",
"standard": "^17.0.0",
"tap": "^16.2.0",
"tsd": "^0.20.0",
"typescript": "^4.0.3"
"process-warning": "^1.0.0",
"under-pressure-deprecated": "npm:under-pressure@6.0.0"
}
}
+2
-228
# under-pressure
![CI](https://github.com/fastify/under-pressure/workflows/CI/badge.svg)
[![NPM version](https://img.shields.io/npm/v/under-pressure.svg?style=flat)](https://www.npmjs.com/package/under-pressure)
[![Known Vulnerabilities](https://snyk.io/test/github/fastify/under-pressure/badge.svg)](https://snyk.io/test/github/fastify/under-pressure)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/)
Measure process load with automatic handling of *"Service Unavailable"* plugin for Fastify.
It can check `maxEventLoopDelay`, `maxHeapUsedBytes`, `maxRssBytes` and `maxEventLoopUtilization` values.
You can also specify a custom health check, to verify the status of
external resources.
<a name="requirements"></a>
## Requirements
Fastify ^2.0.0. Please refer to [this branch](https://github.com/fastify/under-pressure/tree/1.x) and related versions for Fastify ^1.1.0 compatibility.
<a name="install"></a>
## Install
```
npm i under-pressure --save
```
<a name="usage"></a>
## Usage
Require the plugin and register it into the Fastify instance.
```js
const fastify = require('fastify')()
fastify.register(require('under-pressure'), {
maxEventLoopDelay: 1000,
maxHeapUsedBytes: 100000000,
maxRssBytes: 100000000,
maxEventLoopUtilization:0.98
})
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.
```js
fastify.register(require('under-pressure'), {
maxEventLoopDelay: 1000,
message: 'Under pressure!',
retryAfter: 50
})
```
You can also configure custom Error instance `under-pressure` will throw.
```js
class CustomError extends Error {
constructor () {
super('Custom error message')
Error.captureStackTrace(this, CustomError)
}
}
fastify.register(require('under-pressure'), {
maxEventLoopDelay: 1000,
customError: CustomError
})
```
The default value for `maxEventLoopDelay`, `maxHeapUsedBytes`, `maxRssBytes` and `maxEventLoopUtilization` is `0`.
If the value is `0` the check will not be performed.
Since [`eventLoopUtilization`](https://nodejs.org/api/perf_hooks.html#perf_hooks_performance_eventlooputilization_utilization1_utilization2) is only available in Node version 14.0.0 and 12.19.0 the check will be disabled in other versions.
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`, `eventLoopDelay` and `eventLoopUtilized`.
```js
console.log(fastify.memoryUsage())
```
#### Pressure Handler
You can provide a pressure handler in the options to handle the pressure errors. The advantage is that you know why the error occurred. Moreover, the request can be handled as if nothing happened.
```js
const fastify = require('fastify')()
const underPressure = require('under-pressure')()
fastify.register(underPressure, {
maxHeapUsedBytes: 100000000,
maxRssBytes: 100000000,
pressureHandler: (req, rep, type, value) => {
if (type === underPressure.TYPE_HEAP_USED_BYTES) {
fastify.log.warn(`too many heap bytes used: ${value}`)
} else if (type === underPressure.TYPE_RSS_BYTES) {
fastify.log.warn(`too many rss bytes used: ${value}`)
}
rep.send('out of memory') // if you omit this line, the request will be handled normally
}
})
```
It is possible as well to return a Promise that will call `reply.send` (or something else).
```js
fastify.register(underPressure, {
maxHeapUsedBytes: 100000000,
pressureHandler: (req, rep, type, value) => {
return getPromise().then(() => reply.send({hello: 'world'}))
}
})
```
Any other return value than a promise or nullish will be sent to client with `reply.send`.
#### 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.
To configure the endpoint more specifically you can pass an object. This consists of
- *routeOpts* - Any Fastify [route options](https://www.fastify.io/docs/latest/Reference/Routes/#routes-options) except `schema`
- *routeSchemaOpts* - As per the Fastify route options, an object containing the schema for request
- *routeResponseSchemaOpts* - An object containing the schema for additional response items to be merged with the default response schema, see below
- *url* - The URL to expose the status route on
```js
fastify.register(require('under-pressure'), {
maxEventLoopDelay: 1000,
exposeStatusRoute: {
routeOpts: {
logLevel: 'debug',
config: {
someAttr: 'value'
}
},
routeSchemaOpts: { // If you also want to set a custom route schema
hide: true
},
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 `/alive` route to be `debug`.
If you need to return other information in the response, you can return an object from the `healthCheck` function (see next paragraph) and use the `routeResponseSchemaOpts` property to describe your custom response schema (**note**: `status` will always be present in the response)
```js
fastify.register(underPressure, {
...
exposeStatusRoute: {
routeResponseSchemaOpts: {
extraValue: { type: 'string' },
metrics: {
type: 'object',
properties: {
eventLoopDelay: { type: 'number' },
rssBytes: { type: 'number' },
heapUsed: { type: 'number' },
eventLoopUtilized: { type: 'number' },
},
},
// ...
}
},
healthCheck: async (fastifyInstance) => {
return {
extraValue: await getExtraValue(),
metrics: fastifyInstance.memoryUsage(),
// ...
}
},
}
```
#### 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 that resolves to a boolean value or to an object. 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.
```js
const fastify = require('fastify')()
fastify.register(require('under-pressure'), {
healthCheck: async function (fastifyInstance) {
// do some magic to check if your db connection is healthy, etc...
return true
},
healthCheckInterval: 500
})
```
<a name="sample-interval"></a>
#### Sample interval
You can set a custom value for sampling the metrics returned by `memoryUsage` using the `sampleInterval` option, which accepts a number that represents the interval in milliseconds.
The default value is different depending on which Node version is used. In version 8 and 10 it is `5`, while on version 11.10.0 and up it is `1000`. This difference is because from version 11.10.0 the event loop delay can be sampled with [`monitorEventLoopDelay`](https://nodejs.org/docs/latest-v12.x/api/perf_hooks.html#perf_hooks_perf_hooks_monitoreventloopdelay_options) and this allows to increase the interval value.
```js
const fastify = require('fastify')()
fastify.register(require('under-pressure'), {
sampleInterval: <your custom sample interval in ms>
})
```
<a name="acknowledgements"></a>
## Acknowledgements
This project is kindly sponsored by [LetzDoIt](https://www.letzdoitapp.com/).
<a name="license"></a>
## License
Licensed under [MIT](./LICENSE).
`under-pressure@6.1.0` has been deprecated. Please use
`@fastify/under-pressure@7.0.0` instead.
# Set the default behavior, in case people don't have core.autocrlf set
* text=auto
# Require Unix line endings
* text eol=lf
version: 2
updates:
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "monthly"
open-pull-requests-limit: 10
- package-ecosystem: "npm"
directory: "/"
schedule:
interval: "weekly"
open-pull-requests-limit: 10
# Number of days of inactivity before an issue becomes stale
daysUntilStale: 15
# Number of days of inactivity before a stale issue is closed
daysUntilClose: 7
# Issues with these labels will never be considered stale
exemptLabels:
- "discussion"
- "feature request"
- "bug"
- "help wanted"
- "plugin suggestion"
- "good first issue"
# Label to use when marking an issue as stale
staleLabel: stale
# Comment to post when marking an issue as stale. Set to `false` to disable
markComment: >
This issue has been automatically marked as stale because it has not had
recent activity. It will be closed if no further activity occurs. Thank you
for your contributions.
# Comment to post when closing a stale issue. Set to `false` to disable
closeComment: false
name: CI
on:
push:
paths-ignore:
- 'docs/**'
- '*.md'
pull_request:
paths-ignore:
- 'docs/**'
- '*.md'
jobs:
test:
uses: fastify/workflows/.github/workflows/plugins-ci.yml@v3

Sorry, the diff of this file is not supported yet

import {
FastifyInstance,
FastifyPlugin,
FastifyReply,
FastifyRequest
} from "fastify";
export const TYPE_EVENT_LOOP_DELAY = 'eventLoopDelay'
export const TYPE_HEAP_USED_BYTES = 'heapUsedBytes'
export const TYPE_RSS_BYTES = 'rssBytes'
export const TYPE_HEALTH_CHECK = 'healthCheck'
export const TYPE_EVENT_LOOP_UTILIZATION = 'eventLoopUtilization'
declare namespace underPressure {
interface UnderPressureOptions {
maxEventLoopDelay?: number;
maxEventLoopUtilization?: number;
maxHeapUsedBytes?: number;
maxRssBytes?: number;
message?: string;
retryAfter?: number;
healthCheck?: (fastify: FastifyInstance) => Promise<Record<string, unknown> | boolean>;
healthCheckInterval?: number;
pressureHandler?: (request: FastifyRequest, reply: FastifyReply, type: string, value: number | undefined) => Promise<void> | void;
sampleInterval?: number;
exposeStatusRoute?: boolean | string | { routeOpts: object; routeSchemaOpts?: object; routeResponseSchemaOpts?: object; url?: string };
customError?: Error;
}
}
declare module "fastify" {
interface FastifyInstance {
memoryUsage(): { heapUsed: number; rssBytes: number; eventLoopDelay: number; eventLoopUtilized: number };
}
}
declare let underPressure: FastifyPlugin<
underPressure.UnderPressureOptions
>;
export default underPressure;
import underPressure from ".";
import fastify from "fastify";
const server = fastify();
() => {
server.register(underPressure, {
maxEventLoopDelay: 1000,
maxHeapUsedBytes: 100000000,
maxRssBytes: 100000000
});
server.register(underPressure);
server.get("/", (req, reply) => {
reply.send({ hello: "world" });
});
server.listen(3000, err => {
if (err) throw err;
});
};
() => {
server.register(underPressure, {
maxEventLoopDelay: 1000,
message: "Under pressure!",
retryAfter: 50
});
};
() => {
const memoryUsage = server.memoryUsage();
console.log(memoryUsage.heapUsed);
console.log(memoryUsage.rssBytes);
console.log(memoryUsage.eventLoopDelay);
};
() => {
server.register(underPressure, {
healthCheck: async function (fastifyInstance) {
// do some magic to check if your db connection is healthy, etc...
return fastifyInstance.register === server.register;
},
healthCheckInterval: 500
});
};
() => {
server.register(underPressure, {
sampleInterval: 10
});
}
() => {
server.register(underPressure, {
exposeStatusRoute: '/v2/status',
});
server.register(underPressure, {
exposeStatusRoute: true
});
server.register(underPressure, {
exposeStatusRoute: {
routeOpts: {
logLevel: 'silent',
config: {}
},
url: '/alive'
}
});
server.register(underPressure, {
exposeStatusRoute: {
routeOpts: {
logLevel: 'silent'
}
}
});
server.register(underPressure, {
exposeStatusRoute: {
routeOpts: {
logLevel: 'silent'
},
routeSchemaOpts: {
hide: true
}
}
})
server.register(underPressure, {
customError: new Error('custom error message')
});
};
MIT License
Copyright (c) 2017 Fastify
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
'use strict'
const { test } = require('tap')
const { promisify } = require('util')
const sget = require('simple-get').concat
const Fastify = require('fastify')
const { monitorEventLoopDelay } = require('perf_hooks')
const underPressure = require('../index')
const { valid, satisfies, coerce } = require('semver')
const wait = promisify(setTimeout)
const isSupportedVersion = satisfies(valid(coerce(process.version)), '12.19.0 || >=14.0.0')
function block (msec) {
const start = Date.now()
/* eslint-disable no-empty */
while (Date.now() - start < msec) { }
}
test('health check', async t => {
t.plan(3)
const fastify = Fastify()
fastify.register(underPressure, {
healthCheck: async () => false,
healthCheckInterval: 1,
pressureHandler: (req, rep, type, value) => {
t.equal(type, underPressure.TYPE_HEALTH_CHECK)
t.equal(value, undefined)
rep.send('B')
}
})
fastify.get('/', (req, rep) => rep.send('A'))
t.equal((await fastify.inject().get('/').end()).body, 'B')
})
test('health check - delayed handling with promise success', async t => {
t.plan(1)
const fastify = Fastify()
fastify.register(underPressure, {
healthCheck: async () => false,
healthCheckInterval: 1,
pressureHandler: async (req, rep, type, value) => {
await wait(250)
rep.send('B')
}
})
fastify.get('/', (req, rep) => rep.send('A'))
t.equal((await fastify.inject().get('/').end()).body, 'B')
})
test('health check - delayed handling with promise error', async t => {
t.plan(2)
const fastify = Fastify()
const errorMessage = 'promiseError'
fastify.register(underPressure, {
healthCheck: async () => false,
healthCheckInterval: 1,
pressureHandler: async (req, rep, type, value) => {
await wait(250)
throw new Error(errorMessage)
}
})
fastify.get('/', (req, rep) => rep.send('A'))
const response = await fastify.inject().get('/').end()
t.equal(response.statusCode, 500)
t.equal(JSON.parse(response.body).message, errorMessage)
})
test('health check - no handling', async t => {
t.plan(1)
const fastify = Fastify()
fastify.register(underPressure, {
healthCheck: async () => false,
healthCheckInterval: 1,
pressureHandler: (req, rep, type, value) => { }
})
fastify.get('/', (req, rep) => rep.send('A'))
t.equal((await fastify.inject().get('/').end()).body, 'A')
})
test('health check - return response', async t => {
t.plan(1)
const fastify = Fastify()
fastify.register(underPressure, {
healthCheck: async () => false,
healthCheckInterval: 1,
pressureHandler: (req, rep, type, value) => 'B'
})
fastify.get('/', (req, rep) => rep.send('A'))
t.equal((await fastify.inject().get('/').end()).body, 'B')
})
test('event loop delay', { skip: !monitorEventLoopDelay }, t => {
t.plan(5)
const fastify = Fastify()
fastify.register(underPressure, {
maxEventLoopDelay: 1,
pressureHandler: (req, rep, type, value) => {
t.equal(type, underPressure.TYPE_EVENT_LOOP_DELAY)
t.ok(value > 1)
rep.send('B')
}
})
fastify.get('/', (req, rep) => rep.send('A'))
fastify.listen({ port: 0 }, async (err, address) => {
t.error(err)
fastify.server.unref()
await wait(500)
process.nextTick(() => block(1500))
sget({
method: 'GET',
url: address + '/'
}, (err, response, body) => {
t.error(err)
t.equal(body.toString(), 'B')
fastify.close()
})
})
})
test('heap bytes', t => {
t.plan(5)
const fastify = Fastify()
fastify.register(underPressure, {
maxHeapUsedBytes: 1,
pressureHandler: (req, rep, type, value) => {
t.equal(type, underPressure.TYPE_HEAP_USED_BYTES)
t.ok(value > 1)
rep.send('B')
}
})
fastify.get('/', (req, rep) => rep.send('A'))
fastify.listen({ port: 0 }, (err, address) => {
t.error(err)
fastify.server.unref()
process.nextTick(() => block(monitorEventLoopDelay ? 1500 : 500))
sget({
method: 'GET',
url: address
}, (err, response, body) => {
t.error(err)
t.equal(body.toString(), 'B')
fastify.close()
})
})
})
test('rss bytes', t => {
t.plan(5)
const fastify = Fastify()
fastify.register(underPressure, {
maxRssBytes: 1,
pressureHandler: (req, rep, type, value) => {
t.equal(type, underPressure.TYPE_RSS_BYTES)
t.ok(value > 1)
rep.send('B')
}
})
fastify.get('/', (req, rep) => rep.send('A'))
fastify.listen({ port: 0 }, (err, address) => {
t.error(err)
fastify.server.unref()
process.nextTick(() => block(monitorEventLoopDelay ? 1500 : 500))
sget({
method: 'GET',
url: address
}, (err, response, body) => {
t.error(err)
t.equal(body.toString(), 'B')
fastify.close()
})
})
})
test('event loop utilization', { skip: !isSupportedVersion }, t => {
t.plan(5)
const fastify = Fastify()
fastify.register(underPressure, {
maxEventLoopUtilization: 0.01,
pressureHandler: (req, rep, type, value) => {
t.equal(type, underPressure.TYPE_EVENT_LOOP_UTILIZATION)
t.ok(value > 0.01 && value <= 1)
rep.send('B')
}
})
fastify.get('/', async (req, rep) => rep.send('A'))
fastify.listen({ port: 0 }, (err, address) => {
t.error(err)
fastify.server.unref()
process.nextTick(() => block(1000))
sget({
method: 'GET',
url: address
}, (err, response, body) => {
t.error(err)
t.equal(body.toString(), 'B')
fastify.close()
})
})
})
test('event loop delay (NaN)', { skip: !isSupportedVersion }, t => {
t.plan(5)
const mockedUnderPressure = t.mock('../index', {
perf_hooks: {
monitorEventLoopDelay: () => ({
enable: () => { },
reset: () => { },
mean: NaN
}),
performance: {
eventLoopUtilization: () => { }
}
}
})
const fastify = Fastify()
fastify.register(mockedUnderPressure, {
maxEventLoopDelay: 1000,
pressureHandler: (req, rep, type, value) => {
t.equal(type, underPressure.TYPE_EVENT_LOOP_DELAY)
t.equal(value, Infinity)
rep.send('B')
}
})
fastify.get('/', async (req, rep) => rep.send('A'))
fastify.listen({ port: 0 }, (err, address) => {
t.error(err)
fastify.server.unref()
process.nextTick(() => block(1000))
sget({
method: 'GET',
url: address
}, (err, response, body) => {
t.error(err)
t.equal(body.toString(), 'B')
fastify.close()
})
})
})
'use strict'
const { test } = require('tap')
const sget = require('simple-get').concat
const Fastify = require('fastify')
const { monitorEventLoopDelay } = require('perf_hooks')
const underPressure = require('../index')
function block (msec) {
const start = Date.now()
/* eslint-disable no-empty */
while (Date.now() - start < msec) { }
}
test('Expose status route', t => {
t.plan(4)
const fastify = Fastify()
fastify.register(underPressure, {
exposeStatusRoute: true
})
fastify.listen({ port: 0 }, (err, address) => {
t.error(err)
fastify.server.unref()
process.nextTick(() => block(monitorEventLoopDelay ? 1500 : 500))
sget({
method: 'GET',
url: `${address}/status`
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.same(JSON.parse(body), { status: 'ok' })
fastify.close()
})
})
})
test('Expose custom status route', t => {
t.plan(5)
const fastify = Fastify()
t.teardown(() => fastify.close())
fastify.register(underPressure, {
exposeStatusRoute: '/alive'
})
fastify.inject({
url: '/status'
}, (err, response) => {
t.error(err)
t.equal(response.statusCode, 404)
})
fastify.inject({
url: '/alive'
}, (err, response) => {
t.error(err)
t.equal(response.statusCode, 200)
t.same(JSON.parse(response.payload), { status: 'ok' })
})
})
test('Expose status route with additional route options', t => {
t.plan(3)
const customConfig = {
customVal: 'someVal'
}
const fastify = Fastify({ exposeHeadRoutes: false })
fastify.addHook('onRoute', (routeOptions) => {
fastify.server.unref()
process.nextTick(() => block(500))
t.equal(routeOptions.url, '/alive')
t.equal(routeOptions.logLevel, 'silent', 'log level not set')
t.same(routeOptions.config, customConfig, 'config not set')
fastify.close()
})
fastify.register(underPressure, {
exposeStatusRoute: {
routeOpts: {
logLevel: 'silent',
config: customConfig
},
url: '/alive'
}
})
fastify.ready()
})
test('Expose status route with additional route options and default url', t => {
t.plan(2)
const fastify = Fastify({ exposeHeadRoutes: false })
fastify.addHook('onRoute', (routeOptions) => {
fastify.server.unref()
process.nextTick(() => block(500))
t.equal(routeOptions.url, '/status')
t.equal(routeOptions.logLevel, 'silent', 'log level not set')
fastify.close()
})
fastify.register(underPressure, {
exposeStatusRoute: {
routeOpts: {
logLevel: 'silent'
}
}
})
fastify.ready()
})
test('Expose status route with additional route options, route schema options', t => {
const routeSchemaOpts = { hide: true }
const fastify = Fastify({ exposeHeadRoutes: false })
fastify.addHook('onRoute', (routeOptions) => {
fastify.server.unref()
process.nextTick(() => block(500))
t.equal(routeOptions.url, '/alive')
t.equal(routeOptions.logLevel, 'silent', 'log level not set')
t.same(routeOptions.schema, Object.assign({}, routeSchemaOpts, {
response: {
200: {
type: 'object',
properties: {
status: { type: 'string' }
}
}
}
}), 'config not set')
fastify.close()
t.end()
})
fastify.register(underPressure, {
exposeStatusRoute: {
routeOpts: {
logLevel: 'silent'
},
routeSchemaOpts,
url: '/alive'
}
})
fastify.ready()
})
test('Expose status route with additional route options, route schema options and default url', t => {
const routeSchemaOpts = { hide: true }
const fastify = Fastify({ exposeHeadRoutes: false })
fastify.addHook('onRoute', (routeOptions) => {
fastify.server.unref()
process.nextTick(() => block(500))
t.equal(routeOptions.url, '/status')
t.equal(routeOptions.logLevel, 'silent', 'log level not set')
t.same(routeOptions.schema, Object.assign({}, routeSchemaOpts, {
response: {
200: {
type: 'object',
properties: {
status: { type: 'string' }
}
}
}
}), 'config not set')
fastify.close()
t.end()
})
fastify.register(underPressure, {
exposeStatusRoute: {
routeOpts: {
logLevel: 'silent'
},
routeSchemaOpts
}
})
fastify.ready()
})
'use strict'
const { test } = require('tap')
const { promisify } = require('util')
const sget = require('simple-get').concat
const Fastify = require('fastify')
const { monitorEventLoopDelay } = require('perf_hooks')
const underPressure = require('../index')
const { valid, satisfies, coerce } = require('semver')
const wait = promisify(setTimeout)
test('Should return 503 on maxEventLoopDelay', t => {
t.plan(5)
const fastify = Fastify()
fastify.register(underPressure, {
maxEventLoopDelay: 15
})
fastify.get('/', (req, reply) => {
reply.send({ hello: 'world' })
})
fastify.listen({ port: 0 }, async (err, address) => {
t.error(err)
fastify.server.unref()
// If using monitorEventLoopDelay give it time to collect
// some samples
if (monitorEventLoopDelay) {
await wait(500)
}
// Increased to prevent Travis to fail
process.nextTick(() => block(1000))
sget({
method: 'GET',
url: address
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 503)
t.equal(response.headers['retry-after'], '10')
t.same(JSON.parse(body), {
code: 'FST_UNDER_PRESSURE',
error: 'Service Unavailable',
message: 'Service Unavailable',
statusCode: 503
})
fastify.close()
})
})
})
const isSupportedVersion = satisfies(valid(coerce(process.version)), '12.19.0 || >=14.0.0')
test('Should return 503 on maxEventloopUtilization', { skip: !isSupportedVersion }, t => {
t.plan(5)
const fastify = Fastify()
fastify.register(underPressure, {
maxEventLoopUtilization: 0.60
})
fastify.get('/', (req, reply) => {
reply.send({ hello: 'world' })
})
fastify.listen({ port: 0 }, async (err, address) => {
t.error(err)
fastify.server.unref()
// Increased to prevent Travis to fail
process.nextTick(() => block(1000))
sget({
method: 'GET',
url: address
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 503)
t.equal(response.headers['retry-after'], '10')
t.same(JSON.parse(body), {
code: 'FST_UNDER_PRESSURE',
error: 'Service Unavailable',
message: 'Service Unavailable',
statusCode: 503
})
fastify.close()
})
})
})
test('Should return 503 on maxHeapUsedBytes', t => {
t.plan(5)
const fastify = Fastify()
fastify.register(underPressure, {
maxHeapUsedBytes: 1
})
fastify.get('/', (req, reply) => {
reply.send({ hello: 'world' })
})
fastify.listen({ port: 0 }, (err, address) => {
t.error(err)
fastify.server.unref()
process.nextTick(() => block(monitorEventLoopDelay ? 1500 : 500))
sget({
method: 'GET',
url: address
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 503)
t.equal(response.headers['retry-after'], '10')
t.same(JSON.parse(body), {
code: 'FST_UNDER_PRESSURE',
error: 'Service Unavailable',
message: 'Service Unavailable',
statusCode: 503
})
fastify.close()
})
})
})
test('Should return 503 on maxRssBytes', t => {
t.plan(5)
const fastify = Fastify()
fastify.register(underPressure, {
maxRssBytes: 1
})
fastify.get('/', (req, reply) => {
reply.send({ hello: 'world' })
})
fastify.listen({ port: 0 }, (err, address) => {
t.error(err)
fastify.server.unref()
process.nextTick(() => block(monitorEventLoopDelay ? 1500 : 500))
sget({
method: 'GET',
url: address
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 503)
t.equal(response.headers['retry-after'], '10')
t.same(JSON.parse(body), {
code: 'FST_UNDER_PRESSURE',
error: 'Service Unavailable',
message: 'Service Unavailable',
statusCode: 503
})
fastify.close()
})
})
})
test('Custom message and retry after header', t => {
t.plan(5)
const fastify = Fastify()
fastify.register(underPressure, {
maxRssBytes: 1,
message: 'Under pressure!',
retryAfter: 50
})
fastify.get('/', (req, reply) => {
reply.send({ hello: 'world' })
})
fastify.listen({ port: 0 }, (err, address) => {
t.error(err)
fastify.server.unref()
process.nextTick(() => block(monitorEventLoopDelay ? 1500 : 500))
sget({
method: 'GET',
url: address
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 503)
t.equal(response.headers['retry-after'], '50')
t.same(JSON.parse(body), {
code: 'FST_UNDER_PRESSURE',
error: 'Service Unavailable',
message: 'Under pressure!',
statusCode: 503
})
fastify.close()
})
})
})
test('Custom error instance', t => {
t.plan(5)
class CustomError extends Error {
constructor () {
super('Custom error message')
this.statusCode = 418
this.code = 'FST_CUSTOM_ERROR'
Error.captureStackTrace(this, CustomError)
}
}
const fastify = Fastify()
fastify.register(underPressure, {
maxRssBytes: 1,
customError: CustomError
})
fastify.get('/', (req, reply) => {
reply.send({ hello: 'world' })
})
fastify.setErrorHandler((err, req, reply) => {
t.ok(err instanceof Error)
return reply.code(err.statusCode).send(err)
})
fastify.listen({ port: 0 }, (err, address) => {
t.error(err)
fastify.server.unref()
process.nextTick(() => block(monitorEventLoopDelay ? 1500 : 500))
sget({
method: 'GET',
url: address
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 418)
t.same(JSON.parse(body), {
code: 'FST_CUSTOM_ERROR',
error: 'I\'m a Teapot',
message: 'Custom error message',
statusCode: 418
})
fastify.close()
})
})
})
test('memoryUsage name space', t => {
t.plan(9)
const fastify = Fastify()
fastify.register(underPressure, {
maxEventLoopDelay: 1000,
maxHeapUsedBytes: 100000000,
maxRssBytes: 100000000,
maxEventLoopUtilization: 0.85
})
fastify.get('/', (req, reply) => {
t.ok(fastify.memoryUsage().eventLoopDelay > 0)
t.ok(fastify.memoryUsage().heapUsed > 0)
t.ok(fastify.memoryUsage().rssBytes > 0)
t.ok(fastify.memoryUsage().eventLoopUtilized >= 0)
reply.send({ hello: 'world' })
})
fastify.listen({ port: 0 }, async (err, address) => {
t.error(err)
t.equal(typeof fastify.memoryUsage, 'function')
fastify.server.unref()
// If using monitorEventLoopDelay give it time to collect
// some samples
if (monitorEventLoopDelay) {
await wait(500)
}
process.nextTick(() => block(monitorEventLoopDelay ? 1500 : 500))
sget({
method: 'GET',
url: address
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.same(JSON.parse(body), { hello: 'world' })
fastify.close()
})
})
})
test('memoryUsage name space (without check)', t => {
t.plan(9)
const fastify = Fastify()
fastify.register(underPressure)
fastify.get('/', (req, reply) => {
t.ok(fastify.memoryUsage().eventLoopDelay > 0)
t.ok(fastify.memoryUsage().heapUsed > 0)
t.ok(fastify.memoryUsage().rssBytes > 0)
t.ok(fastify.memoryUsage().eventLoopUtilized >= 0)
reply.send({ hello: 'world' })
})
fastify.listen({ port: 0 }, async (err, address) => {
t.error(err)
t.equal(typeof fastify.memoryUsage, 'function')
fastify.server.unref()
// If using monitorEventLoopDelay give it time to collect
// some samples
if (monitorEventLoopDelay) {
await wait(500)
}
process.nextTick(() => block(monitorEventLoopDelay ? 1500 : 500))
sget({
method: 'GET',
url: address
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.same(JSON.parse(body), { hello: 'world' })
fastify.close()
})
})
})
test('Custom health check', t => {
t.plan(8)
t.test('should return 503 when custom health check returns false for healthCheck', t => {
t.plan(5)
const fastify = Fastify()
fastify.register(underPressure, {
healthCheck: async () => {
return false
},
healthCheckInterval: 1000
})
fastify.get('/', (req, reply) => {
reply.send({ hello: 'world' })
})
fastify.listen({ port: 0 }, (err, address) => {
t.error(err)
fastify.server.unref()
sget({
method: 'GET',
url: address
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 503)
t.equal(response.headers['retry-after'], '10')
t.same(JSON.parse(body), {
code: 'FST_UNDER_PRESSURE',
error: 'Service Unavailable',
message: 'Service Unavailable',
statusCode: 503
})
fastify.close()
})
})
})
t.test('should return 200 when custom health check returns true for healthCheck', t => {
t.plan(4)
const fastify = Fastify()
fastify.register(underPressure, {
healthCheck: async () => true,
healthCheckInterval: 1000
})
fastify.get('/', (req, reply) => {
reply.send({ hello: 'world' })
})
fastify.listen({ port: 0 }, (err, address) => {
t.error(err)
fastify.server.unref()
sget({
method: 'GET',
url: address
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.same(JSON.parse(body), {
hello: 'world'
})
fastify.close()
})
})
})
t.test('healthCheckInterval option', t => {
t.plan(8)
const fastify = Fastify()
let check = true
fastify.register(underPressure, {
healthCheck: async () => check,
healthCheckInterval: 100
})
fastify.get('/', (req, reply) => {
reply.send({ hello: 'world' })
})
fastify.listen({ port: 0 }, (err, address) => {
t.error(err)
fastify.server.unref()
sget({
method: 'GET',
url: address
}, (err, response, body) => {
check = false
t.error(err)
t.equal(response.statusCode, 200)
t.same(JSON.parse(body), {
hello: 'world'
})
})
setTimeout(function () {
sget({
method: 'GET',
url: address
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 503)
t.equal(response.headers['retry-after'], '10')
t.same(JSON.parse(body), {
code: 'FST_UNDER_PRESSURE',
error: 'Service Unavailable',
message: 'Service Unavailable',
statusCode: 503
})
fastify.close()
})
}, 100)
})
})
t.test('should wait for the initial healthCheck call before initialising the server', t => {
t.plan(3)
let called = false
const fastify = Fastify()
fastify.register(underPressure, {
healthCheck: async () => {
await wait(100)
t.notOk(called)
called = true
},
healthCheckInterval: 1000
})
fastify.listen({ port: 0 }, (err) => {
t.error(err)
t.ok(called)
fastify.close()
})
})
t.test('should call the external health at every status route', t => {
t.plan(7)
const fastify = Fastify()
let check = true
fastify.register(underPressure, {
healthCheck: async () => {
t.pass('healthcheck called')
return check
},
exposeStatusRoute: true
})
fastify.listen({ port: 0 }, (err, address) => {
t.error(err)
fastify.server.unref()
check = false
sget({
method: 'GET',
url: address + '/status'
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 503)
t.equal(response.headers['retry-after'], '10')
t.same(JSON.parse(body), {
code: 'FST_UNDER_PRESSURE',
error: 'Service Unavailable',
message: 'Service Unavailable',
statusCode: 503
})
fastify.close()
})
})
})
t.test('should call the external health at every status route, healthCheck throws', t => {
t.plan(7)
const fastify = Fastify()
let check = true
fastify.register(underPressure, {
healthCheck: async () => {
t.pass('healthcheck called')
if (check === false) {
throw new Error('kaboom')
}
return true
},
exposeStatusRoute: true
})
fastify.listen({ port: 0 }, (err, address) => {
t.error(err)
fastify.server.unref()
check = false
sget({
method: 'GET',
url: address + '/status'
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 503)
t.equal(response.headers['retry-after'], '10')
t.same(JSON.parse(body), {
code: 'FST_UNDER_PRESSURE',
error: 'Service Unavailable',
message: 'Service Unavailable',
statusCode: 503
})
fastify.close()
})
})
})
t.test('should return custom response if returned from the healthCheck function', t => {
t.plan(6)
const fastify = Fastify()
fastify.register(underPressure, {
healthCheck: async () => {
t.pass('healthcheck called')
return {
some: 'value',
anotherValue: 'another',
status: 'overrride status'
}
},
exposeStatusRoute: {
routeResponseSchemaOpts: {
some: { type: 'string' },
anotherValue: { type: 'string' }
}
}
})
fastify.listen({ port: 0 }, (err, address) => {
t.error(err)
fastify.server.unref()
sget({
method: 'GET',
url: address + '/status'
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.same(JSON.parse(body), {
some: 'value',
anotherValue: 'another',
status: 'overrride status'
})
fastify.close()
})
})
})
t.test('should be fastify instance as argument in the healthCheck function', t => {
t.plan(6)
const fastify = Fastify()
fastify.register(underPressure, {
healthCheck: async (fastifyInstance) => {
t.pass('healthcheck called')
return {
fastifyInstanceOk: fastifyInstance === fastify,
status: 'overrride status'
}
},
exposeStatusRoute: {
routeResponseSchemaOpts: {
fastifyInstanceOk: { type: 'boolean' }
}
}
})
fastify.listen({ port: 0 }, (err, address) => {
t.error(err)
fastify.server.unref()
sget({
method: 'GET',
url: address + '/status'
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.same(JSON.parse(body), {
fastifyInstanceOk: true,
status: 'overrride status'
})
fastify.close()
})
})
})
})
function block (msec) {
const start = Date.now()
/* eslint-disable no-empty */
while (Date.now() - start < msec) { }
}