forked from LearnBoost/up
/
up.js
298 lines (244 loc) · 6.18 KB
/
up.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/**
* Module dependencies.
*/
var fork = require('child_process').fork
, qs = require('querystring')
, eq = require('eq')
, os = require('os')
, ms = require('ms')
, env = process.env.NODE_ENV
, Distributor = require('distribute')
, EventEmitter = require('events').EventEmitter
, debug = require('debug')('up')
/**
* Module exports.
*/
module.exports = exports = UpServer;
/**
* Version.
*
* @api public
*/
exports.version = '0.1.6';
/**
* Worker constructor.
*
* @api public
*/
exports.Worker = Worker;
/**
* Number of CPUs available.
*/
var cpus = os.cpus().length;
/**
* Default worker timeout.
*/
var workerTimeout = 'development' == env ? '500ms' : '10m';
/**
* Default number of workers.
*/
var numWorkers = 'development' == env ? 1 : cpus;
/**
* UpServer factory/constructor.
*
* @param {String} module file
* @param {Object} options
* @api public
*/
function UpServer (server, file, opts) {
if (this == global) return new UpServer(server, file, opts);
Distributor.call(this, server);
var self = this;
opts = opts || {};
this.file = file;
this.numWorkers = eq(opts.numWorkers || numWorkers, { cpus: cpus });
this.workerTimeout = ms(null != opts.workerTimeout
? opts.workerTimeout : workerTimeout);
this.requires = opts.requires || [];
if (false !== opts.workerPingInterval) {
this.workerPingInterval = ms(opts.workerPingInterval || '1m');
}
this.workers = [];
this.spawning = [];
this.lastIndex = -1;
// setup workers
this.spawnWorkers(this.numWorkers);
};
/**
* Inherits from EventEmitter.
*/
UpServer.prototype.__proto__ = Distributor.prototype;
/**
* Reloads the workers.
*
* @param {Function} callback
* @return {UpServer} for chaining
* @api public
*/
UpServer.prototype.reload = function (fn) {
if (this.reloading) {
debug('reloading in process - ignoring reload');
return this;
}
// remove all workers in the spawning state
for (var i = 0, l = this.spawning.length; i < l; i++) {
this.spawning[i].shutdown();
}
// snapshot what workers we'll shut down
var reload = [].concat(this.workers)
, self = this
debug('reloading - spawning %d new workers', this.numWorkers);
this.spawnWorkers(this.numWorkers);
this.once('spawn', function (worker) {
debug('worker %s spawned - removing old workers', worker.pid);
self.emit('reload');
fn && fn();
// shut down old workers
for (var i = 0, l = reload.length; i < l; i++) {
reload[i].shutdown();
}
});
return this;
};
/**
* Helper function to spawn multiple workers.
*
* @param {Number} number of workers to spawn
* @api public
*/
UpServer.prototype.spawnWorkers = function (n) {
debug('spawning %d workers from master %d', n, process.pid);
for (var i = 0, l = n; i < l; i++) {
this.spawnWorker();
}
};
/**
* Spawns a worker that binds to an available port.
*
* @api public
*/
UpServer.prototype.spawnWorker = function (fn) {
var w = new Worker(this)
, self = this
// keep track that we're spawning
this.spawning.push(w);
w.on('stateChange', function () {
switch (w.readyState) {
case 'spawned':
self.spawning.splice(self.spawning.indexOf(w), 1);
self.workers.push(w);
self.emit('spawn', w);
break;
case 'terminating':
case 'terminated':
if (~self.spawning.indexOf(self.spawning.indexOf(w))) {
self.spawning.splice(self.spawning.indexOf(w), 1);
}
if (~self.workers.indexOf(w)) {
self.workers.splice(self.workers.indexOf(w), 1);
self.lastIndex = -1;
// @TODO: auto-add workers ?
}
break;
}
});
};
/**
* Gets the next port in the round.
*
* @api private
*/
UpServer.prototype.nextWorker = function () {
this.lastIndex++;
if (!this.workers[this.lastIndex]) this.lastIndex = 0;
return this.workers[this.lastIndex];
};
/**
* Default HTTP/WS handler (overridden).
* By default, `up` distributes based on a round robin.
*
* @api private
*/
UpServer.prototype.defaultHTTP =
UpServer.prototype.defaultWS = function (req, res, next) {
if (this.workers.length) {
next(this.nextWorker().port);
} else {
var self = this;
this.once('spawn', function () {
next(self.nextWorker().port);
});
}
};
/**
* Worker constructor.
*
* @api private
*/
function Worker (server) {
this.server = server;
this.readyState = 'spawning';
var opts = JSON.stringify({
file: server.file
, requires: server.requires
, pingInterval: server.workerPingInterval
});
this.proc = fork(__dirname + '/worker.js', [opts], { env: process.env });
this.proc.on('message', this.onMessage.bind(this));
this.proc.on('exit', this.onExit.bind(this));
this.pid = this.proc.pid;
debug('worker %s created', this.pid);
}
/**
* Inherits from EventEmitter.
*/
Worker.prototype.__proto__ = EventEmitter.prototype;
/**
* Called upon worker exit.
* Sudden exits will mean the worker won't go through `terminating` state
*
* @api private
*/
Worker.prototype.onExit = function () {
debug('worker %s exited '
+ ('terminating' != this.readyState ? 'unexpectedly': ''), this.pid);
this.readyState = 'terminated';
this.emit('stateChange');
};
/**
* Handles an incoming message from the worker.
*
* @api private
*/
Worker.prototype.onMessage = function (msg) {
switch (msg.type) {
case 'addr':
// avoid spawns after SIGHUP was sent
if ('spawning' == this.readyState) {
debug('worker %s listening on port %s', this.pid, msg.addr.port);
this.port = msg.addr.port;
this.readyState = 'spawned';
this.emit('stateChange');
}
break;
}
};
/**
* Shuts down a worker.
*
* @api private
*/
Worker.prototype.shutdown = function () {
if ('spawned' == this.readyState) {
var timeout = this.server.workerTimeout;
debug('telling worker %s to exit in %dms', this.proc.pid, timeout);
this.proc.send({ type: 'die', time: timeout });
this.readyState = 'terminating';
this.emit('stateChange');
} else if ('spawning' == this.readyState) {
debug('killing spawning worker %s', this.pid);
this.proc.kill('SIGHUP');
this.readyState = 'terminating';
this.emit('stateChange');
}
};