Code Monkey home page Code Monkey logo

http-close-channel's Introduction

License: MIT NPM Version

http-close-channel

Express middleware for managing and closing HTTP channels. This middleware sends HTTP Connection Close headers to prompt the client to close the socket connection. This is useful for the following cases:

  1. ensuring that HTTP traffic is evenly distributed across application replicas (e.g., even load distribution across pods in Kubernetes)
  2. closing sockets during graceful shutdown

Middleware Options

The middleware takes the following options:

option type description default from version
gracefulShutdown Boolean Enable closing sockets after the app receives a SIGTERM signal false 1.0.0
maxRequests Integer Set the number of requests per socket connection before the middleware will close the socket 100 1.0.0
logger Object A logger object (see example below) null no logging 1.0.0

Example usage

The middleware can be added to an express app with the following steps...

  1. include the package...
npm install http-close-channel --save
  1. apply the middleware to the app...
// include the middleware
const httpCloseChannel = require('http-close-channel');

// create the app object
const app = express();

// add the middleware to the apps stack...
app.use(httpCloseChannel({
  maxRequests: 50,
  gracefulShutdown: true,
}));

Providing a logger

The middleware will make standard logging calls (e.g., logger.info(...), logger.warn(...)). This option has been tested with a bunyan logger. For example:

const bunyan = require('bunyan');

// create the app object
const app = express();

// create a bunyan based app logger
const loggerConfig = {
  name: 'k8s-test-utils',
  streams: [
    {
      level: 'debug',
      type: 'stream',
      stream: process.stdout,
    },
  ],
};
const appLogger = bunyan.createLogger(loggerConfig);

// pass the logger to the middleware
app.use(httpCloseChannel({
  logger: appLogger,
}));

http-close-channel's People

Contributors

dougsc avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Forkers

dougsc cesine

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.