Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/npm_and_yarn/multi-cf87d80143
Browse files Browse the repository at this point in the history
  • Loading branch information
karankraina authored Oct 19, 2024
2 parents 93ca381 + 8cbc131 commit 4e03fb6
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 39 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs

name: Node.js Unit Tests

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [18.x, 20.x, 22.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm ci
- run: npm run build --if-present
- run: npm test
39 changes: 0 additions & 39 deletions .github/workflows/unit-tests.yml

This file was deleted.

4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,14 @@ underPressure(app, {
maxEventLoopDelay: 1000, // Maximum event loop delay in milliseconds
maxHeapUsedBytes: 200 * 1024 * 1024, // Maximum heap used in bytes
maxRssBytes: 300 * 1024 * 1024, // Maximum RSS memory used in bytes
maxEventLoopUtilization: 0.98 // Maximum event loop utilisation
message: 'Server Under Pressure', // Custom response message
});

app.get('/', (req, res) => {
if (app.locals.isUnderPressure()) {
// Avoind heavy computations here
}
res.send('Hello World!');
});

Expand Down
20 changes: 20 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,25 @@ export function underPressure(

const retryAfter = opts.retryAfter || 10;

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

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

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

return (
checkMaxEventLoopUtilization &&
eventLoopUtilized > maxEventLoopUtilization
);
}

function updateEventLoopDelay() {
eventLoopDelay = Math.max(0, histogram.mean / 1e6 - resolution);

Expand Down Expand Up @@ -143,5 +162,6 @@ export function underPressure(
next();
}

app.locals.isUnderPressure = isUnderPressure;
app.use(underPressureHandler);
}
11 changes: 11 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,14 @@ test('working with event Loop delay', async (t) => {
t.equal(response.text, 'Service Unavailable');
t.equal(response.headers['retry-after'], '20');
});

test('isUnderPressure should be made available to app.locals', async (t) => {
const app = express();

underPressure(app, {
maxHeapUsedBytes: 100,
retryAfter: 20,
});

t.equal(typeof app.locals.isUnderPressure, 'function');
});

0 comments on commit 4e03fb6

Please sign in to comment.