API/api.medcify.app/node_modules/pm2-axon/lib/plugins/round-robin.js

56 lines
873 B
JavaScript
Raw Normal View History

2022-09-26 06:11:44 +00:00
/**
* Deps.
*/
var slice = require('../utils').slice;
/**
* Round-robin plugin.
*
* Provides a `send` method which will
* write the `msg` to all connected peers.
*
* @param {Object} options
* @api private
*/
module.exports = function(options){
options = options || {};
var fallback = options.fallback || function(){};
return function(sock){
/**
* Bind callback to `sock`.
*/
fallback = fallback.bind(sock);
/**
* Initialize counter.
*/
var n = 0;
/**
* Sends `msg` to all connected peers round-robin.
*/
sock.send = function(){
var socks = this.socks;
var len = socks.length;
var sock = socks[n++ % len];
var msg = slice(arguments);
if (sock && sock.writable) {
sock.write(this.pack(msg));
} else {
fallback(msg);
}
};
};
};