npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

toobusy-middleware

v0.1.5

Published

Express middleware that blocks requests when the process is to busy.

Downloads

14

Readme

toobusy-middleware

NPM version Build Status Coverage Status Code Climate Dependency Status

Middleware for express applications that checks if the process is taking too long to respond to requests. If so, it skips further requests until the server is able to properly handle them again, protecting the app from crashing during high load peaks.


This is just a wrapper for Lloyd Hilaiel's toobusy module, which is, in fact, the responsible for all the load-handling logic.

Usage

npm install --save toobusy-middleware

NOTE: There may be some issues getting toobusy to work in Windows environments. If that's your case and you just want to get it working for development, you can make toobusy an optionalDependency in the package.json and the middleware will act as a noop.

In your application:

var express = require('express');
var toobusy = require('toobusy-middleware');

var app = express();

app
  // Put toobusy as high as possible in your middleware stack
  .use(toobusy())
  .use(otherMiddlewares());


var server = app.listen(3000);

process.on('SIGINT', function () {
  server.close();
  // `toobusy`'s shutdown method is exposed by the middleware
  toobusy.shutdown();
  process.exit();
});

Parameters

The middleware accepts two parameters:

  • handler (optional): A function that will be called before sending any headers, allowing you to customize how the request is handled. Use this to call external services (bring more nodes up, notify someone, etc) or to override the default response sent to the user.

  • options (optional): Middleware's settings:

    • maxLag: Maximum time (in ms) the server can be behind before the process is considered to be overloaded. See [toobusy's tunable parameters] maxlag for more info.

    • message: Message to be displayed/returned when the server is too busy to process the request. If it's an object or an array, the response will be sent as JSON. Otherwise, it's sent as 'text/html'.

    • status: HTTP status code to send when the server is too busy. Defaults to status code 503 (Service Unavailable).

Examples

Uses default behavior.

app.use(toobusy());

Simple customization:

app.use(toobusy({
  maxLag : 100,
  message : 'Woah! Too busy here! Try again later.',
  status : 500
});

Sending a JSON response instead of HTML:

app.use(toobusy({
  message : {
    message : 'Too busy!',
    solution : 'Try again later...'
  }
});

Taking action during high load:

// Sends the default response after taking the required action
app.use(toobusy(function () {
  addEmergencyNodes();
  notifySomeone();
}));

Overriding the default response:

app.use(toobusy(function (req, res, next) {
  res.status(503).sendFile('error/toobusy.html');
}));

Using both handler and options:

function notifySomeone() {
  doSomething();
}

app.use(toobusy(notifySomeone, {
  maxLag : 75
}));

License

This project is licensed under the MIT license. See the LICENSE file for more details.