Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

run multi-threaded node.js network applications using the native cluster module

License

NotificationsYou must be signed in to change notification settings

npkgz/cluster-magic

Repository files navigation

run multi-threaded node.js network applications using the nativecluster module

Features

  • Run network applications within multiple processes multiplexed by node.js
  • Easy to use, configless
  • Standalone, no external process managers required
  • Respawn dead/failed workers
  • Gracefull application shutdown viasigterm
  • Hot-Reload/Hot-Restart viasighup
  • Delayed restart of failed processes to avoid infinite restart loops

Install

$ npm install cluster-magic --save$ yarn add cluster-magic

Usage

A working snippet is available in theexamples directory. Just runnode examples/startup.js

File: startup.js

Initializes the cluster application

const_cluster=require('cluster-magic');const_app=require('./application.js');// start the clustered app (8 workers)_cluster.init(_app,{numWorkers:8});

File: application.js

Your socket based application which should be multiplexed

constnet=require('net');functionstartup(){constserver=net.createServer((socket)=>{// connections never end});server.listen(8000);}module.exports={// init hookinit:startup};

Hot-Restart / Hot-Reload

To hot-restart (zero downtime) an application, just send aSIGHUP to the master process.This spawns new workers and disconnects all current workers from thecluster-proxy

Example

# send SIGHUP to process 12345kill -HUP 12345

Signals

  • SIGHUP Restart workers
  • SIGTERM Gracefull application shutdown
  • SIGINT Gracefull application shutdown

Delayed Restarts

cluster-magic comes with as simple delayed-restart policy which suppresses infinite restart loops.

  • In case a process dies, an internal counter will be incremented.
  • If a threshold of10 is reached the process restart is delayed bycounter*200ms to avoid infinite restart loops on internal application errors.Well..this is may not what you expect from a "clustered application" but such an error requires that amanual fix will take place by the operations team (_logger.alert event is triggerd whichSHOULD be observed)
  • The counter is decremented by 1 once per minute.

Environment

To specify the number of workers you can easily pass the environment variableNUM_WORKERS to the nodejs process. Default is set tonum_cpus*2

License

cluster-magic is OpenSource and licensed under the Terms ofThe MIT License (X11) - your're welcome to contribute

About

run multi-threaded node.js network applications using the native cluster module

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp