Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ 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.
Fastify ^4.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
Expand All @@ -35,6 +35,9 @@ fastify.register(require('@fastify/under-pressure'), {
})

fastify.get('/', (req, reply) => {
if (fastify.isUnderPressure()) {
// skip complex computation
}
reply.send({ hello: 'world'})
})

Expand Down
25 changes: 25 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ async function fastifyUnderPressure (fastify, opts) {
}

fastify.decorate('memoryUsage', memoryUsage)
fastify.decorate('isUnderPressure', isUnderPressure)

const timer = setTimeout(beginMemoryUsageUpdate, sampleInterval)
timer.unref()
Expand Down Expand Up @@ -173,6 +174,30 @@ async function fastifyUnderPressure (fastify, opts) {
updateEventLoopUtilization()
}

function isUnderPressure () {
if (checkMaxEventLoopDelay && eventLoopDelay > maxEventLoopDelay) {
return true
}

if (checkMaxHeapUsedBytes && heapUsed > maxHeapUsedBytes) {
return true
}

if (checkMaxRssBytes && rssBytes > maxRssBytes) {
return true
}

if (!externalsHealthy) {
return true
}

if (checkMaxEventLoopUtilization && eventLoopUtilized > maxEventLoopUtilization) {
return true
}

return false
}

function onRequest (req, reply, next) {
if (checkMaxEventLoopDelay && eventLoopDelay > maxEventLoopDelay) {
handlePressure(req, reply, next, TYPE_EVENT_LOOP_DELAY, eventLoopDelay)
Expand Down
18 changes: 12 additions & 6 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const { valid, satisfies, coerce } = require('semver')
const wait = promisify(setTimeout)

test('Should return 503 on maxEventLoopDelay', t => {
t.plan(5)
t.plan(6)

const fastify = Fastify()
fastify.register(underPressure, {
Expand Down Expand Up @@ -42,6 +42,7 @@ test('Should return 503 on maxEventLoopDelay', t => {
message: 'Service Unavailable',
statusCode: 503
})
t.equal(fastify.isUnderPressure(), true)
fastify.close()
})

Expand All @@ -51,7 +52,7 @@ test('Should return 503 on maxEventLoopDelay', t => {

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)
t.plan(6)
const fastify = Fastify()
fastify.register(underPressure, {
maxEventLoopUtilization: 0.60
Expand All @@ -75,6 +76,7 @@ test('Should return 503 on maxEventloopUtilization', { skip: !isSupportedVersion
message: 'Service Unavailable',
statusCode: 503
})
t.equal(fastify.isUnderPressure(), true)
fastify.close()
})

Expand All @@ -83,7 +85,7 @@ test('Should return 503 on maxEventloopUtilization', { skip: !isSupportedVersion
})

test('Should return 503 on maxHeapUsedBytes', t => {
t.plan(5)
t.plan(6)

const fastify = Fastify()
fastify.register(underPressure, {
Expand All @@ -108,6 +110,7 @@ test('Should return 503 on maxHeapUsedBytes', t => {
message: 'Service Unavailable',
statusCode: 503
})
t.equal(fastify.isUnderPressure(), true)
fastify.close()
})

Expand All @@ -116,7 +119,7 @@ test('Should return 503 on maxHeapUsedBytes', t => {
})

test('Should return 503 on maxRssBytes', t => {
t.plan(5)
t.plan(6)

const fastify = Fastify()
fastify.register(underPressure, {
Expand All @@ -141,6 +144,7 @@ test('Should return 503 on maxRssBytes', t => {
message: 'Service Unavailable',
statusCode: 503
})
t.equal(fastify.isUnderPressure(), true)
fastify.close()
})

Expand Down Expand Up @@ -231,7 +235,7 @@ test('Custom error instance', t => {
})

test('memoryUsage name space', t => {
t.plan(9)
t.plan(10)

const fastify = Fastify()
fastify.register(underPressure, {
Expand Down Expand Up @@ -264,6 +268,7 @@ test('memoryUsage name space', t => {
t.error(err)
t.equal(response.statusCode, 200)
t.same(JSON.parse(body), { hello: 'world' })
t.equal(fastify.isUnderPressure(), true)
fastify.close()
})

Expand Down Expand Up @@ -311,7 +316,7 @@ 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)
t.plan(6)

const fastify = Fastify()
fastify.register(underPressure, {
Expand Down Expand Up @@ -339,6 +344,7 @@ test('Custom health check', t => {
message: 'Service Unavailable',
statusCode: 503
})
t.equal(fastify.isUnderPressure(), true)
fastify.close()
})
})
Expand Down
1 change: 1 addition & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
declare module "fastify" {
interface FastifyInstance {
memoryUsage(): { heapUsed: number; rssBytes: number; eventLoopDelay: number; eventLoopUtilized: number };
isUnderPressure(): boolean;
}
}

Expand Down
3 changes: 2 additions & 1 deletion types/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ const server = fastify();
server.register(fastifyUnderPressure);

server.get("/", (req, reply) => {
reply.send({ hello: "world" });

reply.send({ hello: "world", underPressure: server.isUnderPressure() });
});

server.listen({port: 3000}, err => {
Expand Down