-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathWorkerPool.js
170 lines (147 loc) · 4.66 KB
/
WorkerPool.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
"use strict";
var Worker = require('./Worker');
var Deferred = require('./lib/Deferred');
function WorkerPool(numWorkers, workerPath, workerArgs, options) {
options = options || {};
this._numWorkers = numWorkers;
this._workerArgs = workerArgs;
this._workerPath = workerPath;
this._opts = options;
this._availableWorkers = [];
this._allWorkers = [];
this._isDestroyed = false;
this._allPendingResponses = [];
this._queuedMessages = [];
this._queuedWorkerSpecificMessages = {};
this._workerPendingResponses = {};
if (!options.lazyBoot) {
this._eagerBootAllWorkers();
}
};
WorkerPool.prototype._bootNewWorker = function() {
var workerID = this._allWorkers.length;
var worker = new Worker(this._workerPath, this._workerArgs, {
initData: this._opts.initData,
printChildResponses: !!this._opts.printChildResponses,
workerName: workerID
});
this._allWorkers.push(worker);
this._availableWorkers.push(workerID);
};
WorkerPool.prototype._eagerBootAllWorkers = function() {
while (this._allWorkers.length < this._numWorkers) {
this._bootNewWorker();
}
};
WorkerPool.prototype._sendMessageToWorker = function(workerID, msg) {
var worker = this._allWorkers[workerID];
var settle = function() {
var queuedMsg;
if (this._queuedWorkerSpecificMessages.hasOwnProperty(workerID)
&& this._queuedWorkerSpecificMessages[workerID].length > 0) {
queuedMsg = this._queuedWorkerSpecificMessages[workerID].shift();
} else if (this._queuedMessages.length > 0) {
queuedMsg = this._queuedMessages.shift();
}
if (queuedMsg) {
this._sendMessageToWorker(workerID, queuedMsg.msg)
.then(function(response) {
queuedMsg.deferred.resolve(response);
}, function(error) {
queuedMsg.deferred.reject(error);
});
} else {
this._availableWorkers.push(workerID);
delete this._workerPendingResponses[workerID];
}
}.bind(this);
var pendingResponse = worker.sendMessage(msg).then(
function(response) {
return Promise.resolve(settle()).then(function() {
return response;
});
},
function(error) {
return Promise.resolve(settle()).then(function() {
throw error;
});
}
);
return this._workerPendingResponses[workerID] = pendingResponse;
};
WorkerPool.prototype.sendMessage = function(msg) {
if (this._isDestroyed) {
throw new Error(
'Attempted to send a message after the worker pool has alread been ' +
'(or is in the process of) shutting down!'
);
}
if (this._opts.lazyBoot && this._allWorkers.length < this._numWorkers) {
this._bootNewWorker();
}
var responsePromise;
if (this._availableWorkers.length > 0) {
responsePromise = this._sendMessageToWorker(
this._availableWorkers.shift(),
msg
);
} else {
var queuedMsgID = this._queuedMessages.length;
var deferred = Deferred();
this._queuedMessages.push({
deferred: deferred,
msg: msg
});
responsePromise = deferred.promise;
}
this._allPendingResponses.push(responsePromise);
return responsePromise;
};
WorkerPool.prototype.sendMessageToAllWorkers = function(msg) {
if (this._isDestroyed) {
throw new Error(
'Attempted to send a message after the worker pool has alread been ' +
'(or is in the process of) shutting down!'
);
}
// Queue the message up for all currently busy workers
var busyWorkerResponses = [];
for (var workerID in this._workerPendingResponses) {
var deferred = Deferred();
if (!this._queuedWorkerSpecificMessages.hasOwnProperty(workerID)) {
this._queuedWorkerSpecificMessages[workerID] = [];
}
this._queuedWorkerSpecificMessages[workerID].push({
deferred: deferred,
msg: msg
});
busyWorkerResponses.push(deferred.promise);
}
// Send out the message to all workers that aren't currently busy
var availableWorkerResponses = this._availableWorkers.map(function(workerID) {
return this._sendMessageToWorker(workerID, msg);
}, this);
this._availableWorkers = [];
return Promise.all(availableWorkerResponses.concat(busyWorkerResponses));
};
WorkerPool.prototype.destroy = function() {
var allWorkers = this._allWorkers;
this._isDestroyed = true;
var allPending = this._allPendingResponses;
return new Promise(function(resolve, reject) {
var pending = allPending.length;
function settle() {
if (--pending <= 0) {
resolve();
}
}
allPending.forEach(function(promise) {
promise.then(settle, settle);
});
}).then(function() {
return Promise.all(allWorkers.map(function(worker) {
return worker.destroy();
}));
});
};
module.exports = WorkerPool;