Wednesday, August 11, 2021

Node Server too busy rpm

 toobusy polls the node.js event loop and keeps track of "lag", which is long requests wait in node's event queue to be processed. When lag crosses a threshold, toobusy tells you that you're too busy. At this point you can stop request processing early (before you spend too much time on them and compound the problem), and return a "Server Too Busy" response. This allows your server to stay responsive under extreme load, and continue serving as many requests as possible.



ar toobusy = require('toobusy-js'),

    express = require('express');


var app = express();


// middleware which blocks requests when we're too busy

app.use(function(req, res, next) {

  if (toobusy()) {

    res.send(503, "I'm busy right now, sorry.");

  } else {

    next();

  }

});


app.get('/', function(req, res) {

  // processing the request requires some work!

  var i = 0;

  while (i < 1e5) i++;

  res.send("I counted to " + i);

});


var server = app.listen(3000);


process.on('SIGINT', function() {

  server.close();

  // calling .shutdown allows your process to exit normally

  toobusy.shutdown();

  process.exit();

});


References 

https://github.com/STRML/node-toobusy 

No comments:

Post a Comment