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

express-hystrix-toobusy

v1.0.0

Published

The module provides express too-busy middleware that has a more reliable logic to detect busy state of the system

Downloads

497

Readme

express-hystrix-toobusy

The module provides express too-busy handler. The handler is based on hystrix-too-busy module

codecov Build Status NPM Downloads Known Vulnerabilities

The idea

There are many module out there that try to implement detection of the system state and shed traffic automatically when it becomes too busy, mostly when CPU goes to 95% and everything gets slow.

The general logic is to measure difference between expected time and actual time when timer events is fired by event loop. That is called event loop latency and it depends on how busy the event loop is at any given point in time. This logic may misfire a lot due to unpredicted nature of runtime environment, memory profile and application logic.

To avoid the above problem we need an observation over some period of recent time, say 1 minute and based on statistic of too-busy error react accordingly. We might also want to react differently to different requests, give some higher priority than the other. Instead of implementing all this logic why not use hystrixjs, which already provides these capabilities and does even more like runtime metrics?

Install

$ npm install express-hystrix-toobusy -S

Usage

const toobusy = require('express-hystrix-toobusy');
const express = require('express');
const app = express();
app.use(toobusy());

Configuration

The module exposes the configuration of hystrix-too-busy module.

By default, it assumes the same hystrix command for all requests with default hystrix configuration, but you can setup commandResolver that will start distinguishing requests between each other and tune too-busy settings for each command.

If command config is not specified, it will use default one. This maybe useful if ones wants to give priorities to different routes. For example, if some commands are more important than the others, one can create 'important' command and tune the circuit breaker to stay longer close while unimportant ones will get short circuited and denied immediately when the system gets under the stress.

const toobusy = require('express-hystrix-toobusy');
const express = require('express');
const app = express();
app.use(toobusy({
    fallback: (err, req, res) => {
        if (err.message === 'TooBusy') {
            res.status(503).end();
            return;
        }
        res.status(500).end();
    },
    commandResolver: req => {   // optional
        switch(req.path) {
            case '/':
                return 'home';
            case '/foo':
                return 'fooCommand';
            case '/bar':
                return 'fooCommand';
            default:
                return 'other';
        }
    }
    latencyThreshold: 70,
    interval: 500,
     // optional
    default: {
        circuitBreakerErrorThresholdPercentage: 50,
        circuitBreakerRequestVolumeThreshold: 20,
        circuitBreakerSleepWindowInMilliseconds: 5000
    },
    // optional
    commands: {
        fooCommand: {
            circuitBreakerErrorThresholdPercentage: 80
        },
        barCommand: {
            circuitBreakerRequestVolumeThreshold: 1
        }
    }
}));