clusterflock is a simple clustering HTTP server for Node. It accepts a single request handler and a hash of options. The goal of clusterflock is to eliminate my own repeated need for a simple clustering server that implements graceful worker shutdown and re-forking of dead workers.
This package is deprecated. I wrote a much more well-tested version without the known bugs of clusterflock called teamster.
$ npm install clusterflock --save
By default, clusterflock will fork the number of workers equal to
os.cpus().length. When it receives a
SIGTERM signal, it will begin attempting to shut down gracefully by ceasing to receive requests and closing all servers after existing requests have been completed.
The simplest use case of clusterflock is to pass it a single request handler function:
var clusterflock = ;;
Since clusterflock essentially just calls
http.createServer in the worker process, anything that can be normally passed to that function can be passed to the
clusterflock main function, including express apps:
var clusterflock =express =app = ;app; // &c.;
When a worker disconnects, the master checks the value of its
suicide attribute. If that value is true, master does nothing. If that value is not true (i.e. the worker died/was killed unintentionally), the master forks a new worker.
clusterflock function accepts an options object:
var clusterflock =app = ;;
||number of worker processes to fork|
||port the workers will listen on|
||amount of time after receiving a graceful shutdown signal that the master will immediately kill workers|
clusterflock responds to signals. heroku, for example, sends
SIGTERM to stop and restart dynos, which will cause clusterflock to initiate a graceful shutdown.
SIGINT, on the other hand, will force clusterflock to shut down immediately.
||Fork an additonal worker|
||Disconnect the least-recently forked worker|
||Kill master process (and therefore workers) immediately.|
||Attempt a graceful shutdown (stop serving requests, serve remaining requests, and shut down).|
To run the tests:
$ npm test
git checkout -b my-clusterflock)
git commit -am "add unicorns")
git push origin my-clusterflock)