Skip to content

Commit

Permalink
Restrict the handler accepts the response of master which has the sam…
Browse files Browse the repository at this point in the history
…e requestId
  • Loading branch information
johnteee committed Jun 28, 2018
1 parent 98b9973 commit b641957
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions wsgilite.js
Original file line number Diff line number Diff line change
Expand Up @@ -385,16 +385,18 @@ class WSGILite extends DefSubRoute {
this.clusterMasterResponseHandlers = this.clusterMasterResponseHandlers.filter((item)=>item !== clusterMasterResponseHandler);
}
requestActionOnClusterMaster(data) {
let msgRequest = {event: MSG_WSGILITE_DO_THINGS_MASTER, data};
let requestId = `${process.pid}_${Date.now()}_${Math.floor(Math.random()*Number.MAX_SAFE_INTEGER)}`;
let msgRequest = {event: MSG_WSGILITE_DO_THINGS_MASTER, requestId, data};

if (cluster.isMaster) {
return new Promise((resolve, reject) => {
let worker;
let handle;
let msgResponse = {event: MSG_WSGILITE_DO_THINGS_WORKER_SUCCESS};
let msgResponse = {event: MSG_WSGILITE_DO_THINGS_WORKER_SUCCESS, requestId};

this.handleClusterMasterRequest(worker, msgRequest, handle).catch((e)=>{
msgResponse.error = e;
msgResponse.event = MSG_WSGILITE_DO_THINGS_WORKER_FAILURE;
reject(msgResponse);
}).then((result)=>{
msgResponse.result = result;
Expand All @@ -405,26 +407,29 @@ class WSGILite extends DefSubRoute {

return new Promise((resolve, reject) => {
let handler = (msg, handle) => {
if (msg && msg.event === MSG_WSGILITE_DO_THINGS_WORKER_SUCCESS) {
resolve(msg, handle);
} else {
reject(msg, handle);
if (msg.requestId === requestId) {
if (msg && msg.event === MSG_WSGILITE_DO_THINGS_WORKER_SUCCESS) {
resolve(msg, handle);
} else {
reject(msg, handle);
}
this.removeClusterMasterResponseHandler(handler);
}
this.removeClusterMasterResponseHandler(handler);
};
this.addClusterMasterResponseHandler(handler);
process.send(msgRequest);
});
}
handleClusterMasterRequest(worker, msg, handle) {
let requestId = msg.requestId;
let result = [];
let errorHandled = false;

const self = this;
const errorHandler = (e) => {
if (worker && (!errorHandled)) {
console.log(e);
worker.send({event: MSG_WSGILITE_DO_THINGS_WORKER_FAILURE, error: e, errorMessage: e.toString(), errorStacktrace: e.stacktrace});
worker.send({event: MSG_WSGILITE_DO_THINGS_WORKER_FAILURE, requestId, error: e, errorMessage: e.toString(), errorStacktrace: e.stacktrace});
}
errorHandled = true;
return Promise.reject(e);
Expand Down Expand Up @@ -454,7 +459,7 @@ class WSGILite extends DefSubRoute {
result.push(anyResult);
}
if (worker) {
worker.send({event: MSG_WSGILITE_DO_THINGS_WORKER_SUCCESS, result});
worker.send({event: MSG_WSGILITE_DO_THINGS_WORKER_SUCCESS, requestId, result});
}

return result;
Expand Down Expand Up @@ -534,9 +539,10 @@ class WSGILite extends DefSubRoute {
return;
}
if (msg.event === MSG_WSGILITE_DO_THINGS_MASTER) {
let request = msg.requestId;
let result = [];
if (this.clusterMasterRequestHandlers.length <= 0) {
worker.send({event: MSG_WSGILITE_DO_THINGS_WORKER_SUCCESS, result});
worker.send({event: MSG_WSGILITE_DO_THINGS_WORKER_SUCCESS, requestId, result});
return;
}

Expand Down

0 comments on commit b641957

Please sign in to comment.