From d495b26307eebb236eb49aa27927d6d27427c6ea Mon Sep 17 00:00:00 2001 From: Ashley Date: Wed, 13 Sep 2023 17:29:25 +0000 Subject: [PATCH] add toobusy --- server.js | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/server.js b/server.js index ac892698..49957b61 100644 --- a/server.js +++ b/server.js @@ -77,6 +77,7 @@ app.use(modules.useragent.express()); app.use(modules.express.json()); // for parsing application/json app.enable("trust proxy"); + var toobusy = require('toobusy-js') const renderTemplate = async (res, req, template, data = {}) => { res.render( @@ -84,7 +85,26 @@ Object.assign(data) ); }; - + + // Set check interval to a faster value. This will catch more latency spikes +// but may cause the check to be too sensitive. +toobusy.interval(110); + +toobusy.maxLag(2500); + + app.use(function(req, res, next) { + if (toobusy()) { + res.send(503, "I'm busy right now, sorry."); + } else { + next(); + } +}); + + toobusy.onLag(function(currentLag) { + console.log("Event loop lag detected! Latency: " + currentLag + "ms"); +}); + + const random_words = [ "banana pie", "how to buy an atom bomb",