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

connlimit

v0.0.1

Published

Artificially limit the number of open connections to a Node server

Downloads

1,388

Readme

connlimit

connlimit is a NodeJS module for artificially limiting the number of connections to a Node server.

Why would you want to do that? Often in production environments it is valuable to know the limits of your system, so you can better predict how it will behave under load. A truly overloaded server can fail in various bizarre ways, and may have unintended side-effects to other parts of the system.

With limiting in place, you can model your expected maximum load, test that your deployment scales to that many connections, and then limit it. This means that if you get a burst of unexpectedly high load, you'll still reject connections but you'll remain within your known-good capacity, decreasing the likelihood of catastrophic failure.

Another advantage of knowing your limits is that you can plan around them. If you know you're now routinely getting more connections than you originally planned, you can predict the right time to scale horizontally by adding more instances of your application, or you can increase the limit after ensuring that your infrastructure can handle the new limit.

Usage

var connLimit = require('connlimit');

var server = http.createServer(someApp);
connLimit(server, 10000); // only allow 10000 concurrent requests
server.listen(listenPort);

Practical Usage

This sort of limiting is best applied to servers that are running behind an HTTP-layer load balancer, since this provides a means to evenly redistribute requests to other backends when one backend reaches its limit. This will also shield the artificially-limited server from being attacked by a client that simply opens hundreds of connections and occupies all of the available slots, blocking out other clients.

This module has been tested in production behind Perlbal, a reverse-proxy load balancer written in Perl. Perlbal has a feature where it will "connect ahead" to the backend and hold open one additional connection ready to be used by an incoming request, which interacts nicely with connLimit's "immediately close when overloaded" behavior by signalling immediately that the server is not ready to server requests. Similar configurations are presumably possible in other reverse-proxy solutions.

Monitoring

In a production deployment you'll want to monitor for cases where servers reach their limits, since that ought to be an exceptional case. To accommodate this, the connLimit function takes an additional set of options of which the only option is currently onLimit:

function onLimit() {
    // log, emit statsd metrics, etc
}

connLimit(
    server, 10000,
    {
        onLimit: onLimit
    }
);

The limit function will be called whenever a connection is rejected due to hitting the limit.

Of course you should also monitor the non-exceptional usage of your system so you can understand when it is time to scale up. connLimit does not provide a facility for this because it's easy to achieve by using directly the built-in server events, and e.g. connect middlewares already exist for doing this.

Contributing

Development environment is standard: just clone the repo and run npm install within it.

Contributions are welcome and assumed to be licensed under the MIT license, as this package itself is. Please be sure to run the unit tests and the linter on your modified version, and then open a pull request.

npm test
npm run-script lint

The project's code style is represented by the .jshintrc file but please also attempt to remain consistent with the prevailing style even if particular details aren't tested for by the linter.