Git Product home page Git Product logo

cluster-forker's Introduction

cluster-forker

CircleCI NPM Downloads node License MIT

easy node clustering

A single instance of Node.js runs in a single thread. To take advantage of multi-core systems, the user will sometimes want to launch a cluster of Node.js processes to handle the load.

The ClusterForker allows easy creation of child processes that all share server ports.

Highlights

  • Written in Typescript

  • Wrapper for the cluster module

  • Simple API

  • Lazy, doesn't actually fork before the run method is called

  • The worker processes are spawned using the child_process.fork() method, so that they can communicate with the parent via IPC and pass server handles back and forth.

Installation

npm:

$ npm install cluster-forker

yarn:

$ yarn add cluster-forker

Usage

easy node clustering

  //index.js
  const { Cluster } = require('cluster-forker');

  let superCluster = new Cluster();

  let superCluster = new Cluster();

  superCluster
  .master( ( cluster ) => {
    // This block runs only in master worker
    console.log('master is running');
  } )
  .fork( ( cluster ) => {
    http.createServer(function(req, res) {
      res.writeHead(200);
      res.end('process ' + process.pid + ' says hello!');
    }).listen(8000);
  } )
  .on('online', function(worker) {
    console.log('Worker ' + worker.process.pid + ' is online');
  })
  .limit( 4 )
  .forkOnExit((worker, code, signal) => {
    console.log('started new worker fork');
  })
  .onExit( ( worker, code, signal ) => {
    console.log('Worker ' + worker.process.pid + ' died with code: ' + code + ', and signal: ' + signal);
    console.log('Starting a new worker');
  } )
  .run() // Runs the cluster and begins forking
$ node index
> master is running
> Worker 31889 is online
> Worker 31890 is online
> Worker 31891 is online
> Worker 31892 is online

License

MIT © Nivrith

cluster-forker's People

Contributors

nivrith avatar

Stargazers

 avatar

Watchers

 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.