📜 ⬆️ ⬇️

NodeJS Cluster-hub. Messaging in cluster, requests, interprocess exclusive locks (critical sections)

Working once again with the cluster module, I was faced with the need to exchange messages between worker processes. Unfortunately, the standard functionality of the module allows you to send messages only from the master process to the worker, and in the opposite direction. At the same time, there is no opportunity to get some kind of response to the message, but I would really like to. Therefore, I wrote a cluster-hub module. Maybe someone will use it.

The module allows


Who cares - I ask under the cat.

')

Message exchange


The simplest functionality is simply sending messages to other processes. It is possible to send messages from master to worker, from worker to master, from master-> master.

var Hub = require('cluster-hub'); var cluster = require('cluster'); var hub = new Hub(cluster); if (cluster.isMaster) { var worker = cluster.fork(); hub.on('master-to-master', function (data) { console.log('master-to-master received'); }); hub.on('worker-to-master', function (data) { console.log('worker-to-master received'); }); hub.sendToMaster('master-to-master', 1); hub.sendToWorker(worker, 'master-to-worker'); } else { hub.on('master-to-worker', function () { console.log('master-to-worker received');; process.exit(); }); hub.sendToMaster('worker-to-master', 2); } 


Sending requests


This functionality allows you to send a request from one process to another and get the result inside the callback function. An example will say everything for itself:

 var Hub = require('cluster-hub'); var cluster = require('cluster'); var hub = new Hub(cluster); if (cluster.isMaster) { // in master process hub.on('sum', function (data, sender, callback) { callback(null, data.a + data.b); }); var worker = cluster.fork(); } else { //in worker process hub.requestMaster('sum', {a: 1, b:2}, function (err, sum) { console.log('Sum in worker: ' + sum); process.exit(); }); } 

By analogy, you can use the requestWorker method to call the method on the worker process with the master process.

Exclusive Locks / Critical Sections


This functionality allows you to get exclusive access to any resource to one of the processes (it does not matter - master or one of the worker). If the worker process stops its work without causing unlock for the locked resource, the resource will be freed automatically.

 var Hub = require('cluster-hub'); var cluster = require('cluster'); var hub = new Hub(cluster); if (cluster.isMaster) { var worker = cluster.fork(); hub.lock('foo', function (unlock) { console.log('foo lock in master'); setTimeout(unlock, 1000); }); } else { hub.lock('foo', function (unlock) { console.log('foo lock in worker 1'); setTimeout(unlock, 500); }); hub.lock('bar', function (unlock) { console.log('bar lock in worker'); unlock(); }) hub.lock('foo', function (unlock) { console.log('second foo lock in worker'); unlock(); process.exit(); }) } 


module source codes are available here: github.com/sirian/node-cluster-hub

Source: https://habr.com/ru/post/191192/


All Articles