Giter VIP home page Giter VIP logo

koa-limit-connections's Introduction

koa-limit-connections

Simple middleware to limit concurrency for Koa v1

Need to restrict how much traffic your application can take at any given point in time? With koa-limit-connections you can easily define your limits both statically or dynamically.

Getting started

Install the module to your project

$ npm install --save koa-limit-connections

Add it to your middleware stack. It's highly recommended that it's included near the top before any downstream computation occurs to prevent unnecessary load on your application. It's a fail fast approach.

const limitConnections = require("koa-limit-connections");
const Koa = require('koa');
const app = new Koa();

app.use(limitConnections({
    // Override default options
}));

// response
app.use(ctx => {
  ctx.body = 'Hello Koa';
});

app.listen(3000);

Options

max

number - Default 100

Use any value > 0 to set your threshold.

app.use(limitConnections({
    max: 10
}));

dynamicMax

function - Has Koa context

Allows you to update the max threshold dynamically as long as it's parseable and value exceeds 0 else falls back to max

app.use(limitConnections({
    max: 20,
    dynamicMax: function () {
        // get request header
        return this.get("limit-connections");
    }
}));

onConnection

function([connections][, max]) - Has Koa context

Called on initial request.

app.use(limitConnections({
    onConnection: function () {
        // Do stuff ...
    }
}));

onMax

function([connections][, max]) - Has Koa context

Called when threshold is met preventing the rest of the middelware stack being called. Use this as an opportunity to customize your response. By default it sets response status to 503 and response body to Too Busy

app.use(limitConnections({
    onMax: function () {
        this.body = "<h1>Application is too busy to handle your request</h1>";
    }
}));

catchExceptions

boolean - Default true

Control whether you want to catch downstream exceptions.

app.use(limitConnections({
    catchExceptions: false
}));

onException

function([exception]) - Has Koa context

Added as an additional security measure koa-limit-connections catches unhandled exceptions to ensure that connections are decremented even in the case of exceptions caused from downstream middleware or other application faults.

app.use(limitConnections({
    onException: function (exception) {
        // Handle exception ...
    }
}));

koa-limit-connections's People

Contributors

robgraham avatar

Stargazers

Yurii Vlasiuk avatar  avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.