mirror of
https://github.com/nodejs/node.git
synced 2026-11-26 21:58:26 -05:00
src,lib: the handle keeps loop alive in cluster rr mode
PR-URL: https://github.com/nodejs/node/pull/46161 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
This commit is contained in:
1 parent
3ce39bbcb7
commit
08a6a61575
4 files changed
+88
-7
No files matched your search
@@ -15,6 +15,9 @@ const EventEmitter = require('events');
|
||||
const { owner_symbol } = require('internal/async_hooks').symbols;
|
||||
const Worker = require('internal/cluster/worker');
|
||||
const { internal, sendHelper } = require('internal/cluster/utils');
|
||||
const { TIMEOUT_MAX } = require('internal/timers');
|
||||
const { setInterval, clearInterval } = require('timers');
|
||||
|
||||
const cluster = new EventEmitter();
|
||||
const handles = new SafeMap();
|
||||
const indexes = new SafeMap();
|
||||
@@ -160,6 +163,21 @@ function rr(message, { indexesKey, index }, cb) {
|
||||
|
||||
let key = message.key;
|
||||
|
||||
let fakeHandle = null;
|
||||
|
||||
function ref() {
|
||||
if (!fakeHandle) {
|
||||
fakeHandle = setInterval(noop, TIMEOUT_MAX);
|
||||
}
|
||||
}
|
||||
|
||||
function unref() {
|
||||
if (fakeHandle) {
|
||||
clearInterval(fakeHandle);
|
||||
fakeHandle = null;
|
||||
}
|
||||
}
|
||||
|
||||
function listen(backlog) {
|
||||
// TODO(bnoordhuis) Send a message to the primary that tells it to
|
||||
// update the backlog size. The actual backlog should probably be
|
||||
@@ -175,7 +193,11 @@ function rr(message, { indexesKey, index }, cb) {
|
||||
// the primary.
|
||||
if (key === undefined)
|
||||
return;
|
||||
|
||||
unref();
|
||||
// If the handle is the last handle in process,
|
||||
// the parent process will delete the handle when worker process exits.
|
||||
// So it is ok if the close message get lost.
|
||||
// See the comments of https://github.com/nodejs/node/pull/46161
|
||||
send({ act: 'close', key });
|
||||
handles.delete(key);
|
||||
removeIndexesKey(indexesKey, index);
|
||||
@@ -189,12 +211,10 @@ function rr(message, { indexesKey, index }, cb) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Faux handle. Mimics a TCPWrap with just enough fidelity to get away
|
||||
// with it. Fools net.Server into thinking that it's backed by a real
|
||||
// handle. Use a noop function for ref() and unref() because the control
|
||||
// channel is going to keep the worker alive anyway.
|
||||
const handle = { close, listen, ref: noop, unref: noop };
|
||||
|
||||
// Faux handle. net.Server is not associated with handle,
|
||||
// so we control its state(ref or unref) by setInterval.
|
||||
const handle = { close, listen, ref, unref };
|
||||
handle.ref();
|
||||
if (message.sockname) {
|
||||
handle.getsockname = getsockname; // TCP handles only.
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
'use strict';
|
||||
|
||||
const common = require('../common');
|
||||
const cluster = require('cluster');
|
||||
const net = require('net');
|
||||
|
||||
cluster.schedulingPolicy = cluster.SCHED_RR;
|
||||
|
||||
if (cluster.isPrimary) {
|
||||
const worker = cluster.fork();
|
||||
worker.on('exit', common.mustCall());
|
||||
} else {
|
||||
const server = net.createServer(common.mustNotCall());
|
||||
server.listen(0, common.mustCall(() => {
|
||||
process.channel.unref();
|
||||
server.close();
|
||||
}));
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
'use strict';
|
||||
|
||||
const common = require('../common');
|
||||
const cluster = require('cluster');
|
||||
const net = require('net');
|
||||
const assert = require('assert');
|
||||
|
||||
cluster.schedulingPolicy = cluster.SCHED_RR;
|
||||
|
||||
if (cluster.isPrimary) {
|
||||
let exited = false;
|
||||
const worker = cluster.fork();
|
||||
worker.on('exit', () => {
|
||||
exited = true;
|
||||
});
|
||||
setTimeout(() => {
|
||||
assert.ok(!exited);
|
||||
worker.kill();
|
||||
}, 3000);
|
||||
} else {
|
||||
const server = net.createServer(common.mustNotCall());
|
||||
server.listen(0, common.mustCall(() => process.channel.unref()));
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
'use strict';
|
||||
|
||||
const common = require('../common');
|
||||
const cluster = require('cluster');
|
||||
const net = require('net');
|
||||
|
||||
cluster.schedulingPolicy = cluster.SCHED_RR;
|
||||
|
||||
if (cluster.isPrimary) {
|
||||
const worker = cluster.fork();
|
||||
worker.on('exit', common.mustCall());
|
||||
} else {
|
||||
const server = net.createServer(common.mustNotCall());
|
||||
server.listen(0, common.mustCall(() => {
|
||||
server.ref();
|
||||
server.unref();
|
||||
process.channel.unref();
|
||||
}));
|
||||
server.unref();
|
||||
}
|
||||
Reference in new issue
Block a user