Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

think-cluster

Package Overview
Dependencies
Maintainers
5
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

think-cluster - npm Package Compare versions

Comparing version 1.1.0 to 1.2.0

57

lib/messenger.js

@@ -7,3 +7,5 @@ const cluster = require('cluster');

const BIND_EVENT = Symbol('bind-event');
const MESSENGER = 'think-messenger';
const mapPromise = new Map();

@@ -38,6 +40,29 @@ let taskId = 1;

bindEvent() {
if (process[BIND_EVENT]) return;
process[BIND_EVENT] = true;
if (cluster.isMaster) {
cluster.on('message', (worker, message) => {
if (message && message.act === MESSENGER) {
const workers = this.getWorkers(message.target, worker);
if (!message || message.act !== MESSENGER) return;
const workers = this.getWorkers(message.target, worker);
if (message.map) {
if (message.mapReturn) {
const map = mapPromise.get(message.action);
map.get(worker).resolve(message.data);
} else {
const map = new Map();
mapPromise.set(message.action, map);
const promises = workers.map(worker => {
worker.send(message);
const defer = helper.defer();
map.set(worker, defer);
return defer.promise;
});
Promise.all(promises).then(data => {
mapPromise.delete(message.action);
message.data = data;
message.action = `${message.action}_ret`;
worker.send(message);
});
}
} else {
workers.forEach(worker => worker.send(message));

@@ -48,3 +73,12 @@ }

process.on('message', message => {
if (message && message.act === MESSENGER) {
if (!message || message.act !== MESSENGER) return;
if (message.map && !message.data) {
const listener = this.listeners(message.action)[0];
assert(helper.isFunction(listener), `${message.action} listener must be a function`);
message.mapReturn = true;
Promise.resolve(listener()).then(data => {
message.data = data;
process.send(message);
});
} else {
this.emit(message.action, message.data);

@@ -70,2 +104,19 @@ }

/**
* map worker task, return worker exec result
* @param {String} action
*/
map(action) {
const defer = helper.defer();
process.send({
act: MESSENGER,
action,
map: true,
target: 'all'
});
this.once(`${action}_ret`, data => {
defer.resolve(data);
});
return defer.promise;
}
/**
* this method will be deprecated

@@ -72,0 +123,0 @@ * @param {Function} callback

@@ -83,2 +83,4 @@ const cluster = require('cluster');

worker.once('listening', address => {
// add prev pid to process.env
env.THINK_PREV_PID = worker.process.pid;
deferred.resolve({worker, address});

@@ -85,0 +87,0 @@ });

2

package.json
{
"name": "think-cluster",
"description": "Cluster manage for ThinkJS",
"version": "1.1.0",
"version": "1.2.0",
"author": {

@@ -6,0 +6,0 @@ "name": "welefen",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc