diff options
author | Fedor Indutny <fedor@indutny.com> | 2015-08-25 12:24:41 -0700 |
---|---|---|
committer | Fedor Indutny <fedor@indutny.com> | 2015-09-07 20:07:03 -0700 |
commit | c7be08cec18b0591381126e149cac96a05125966 (patch) | |
tree | ecbf9b10bc151f4265f512cd75fbb8b7f64692f4 /lib/cluster.js | |
parent | 154d3f5865474a407f7917b8645b675d3741cb79 (diff) | |
download | android-node-v8-c7be08cec18b0591381126e149cac96a05125966.tar.gz android-node-v8-c7be08cec18b0591381126e149cac96a05125966.tar.bz2 android-node-v8-c7be08cec18b0591381126e149cac96a05125966.zip |
cluster: allow shared reused dgram sockets
Allow listening on reused dgram ports in cluster workers.
Fix: https://github.com/joyent/node/issues/9261
PR-URL: https://github.com/nodejs/node/pull/2548
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Diffstat (limited to 'lib/cluster.js')
-rw-r--r-- | lib/cluster.js | 39 |
1 files changed, 25 insertions, 14 deletions
diff --git a/lib/cluster.js b/lib/cluster.js index cbccd02605..84d7c766cb 100644 --- a/lib/cluster.js +++ b/lib/cluster.js @@ -57,7 +57,7 @@ Worker.prototype.isConnected = function isConnected() { // Master/worker specific methods are defined in the *Init() functions. -function SharedHandle(key, address, port, addressType, backlog, fd) { +function SharedHandle(key, address, port, addressType, backlog, fd, flags) { this.key = key; this.workers = []; this.handle = null; @@ -66,7 +66,7 @@ function SharedHandle(key, address, port, addressType, backlog, fd) { // FIXME(bnoordhuis) Polymorphic return type for lack of a better solution. var rval; if (addressType === 'udp4' || addressType === 'udp6') - rval = dgram._createSocketHandle(address, port, addressType, fd); + rval = dgram._createSocketHandle(address, port, addressType, fd, flags); else rval = net._createServerHandle(address, port, addressType, fd); @@ -438,7 +438,8 @@ function masterInit() { var args = [message.address, message.port, message.addressType, - message.fd]; + message.fd, + message.index]; var key = args.join(':'); var handle = handles[key]; if (handle === undefined) { @@ -456,7 +457,8 @@ function masterInit() { message.port, message.addressType, message.backlog, - message.fd); + message.fd, + message.flags); } if (!handle.data) handle.data = message.data; @@ -485,7 +487,7 @@ function masterInit() { cluster.emit('listening', worker, info); } - // Round-robin only. Server in worker is closing, remove from list. + // Server in worker is closing, remove from list. function close(worker, message) { var key = message.key; var handle = handles[key]; @@ -500,6 +502,7 @@ function masterInit() { function workerInit() { var handles = {}; + var indexes = {}; // Called from src/node.js cluster._setupWorker = function() { @@ -528,15 +531,22 @@ function workerInit() { }; // obj is a net#Server or a dgram#Socket object. - cluster._getServer = function(obj, address, port, addressType, fd, cb) { - var message = { - addressType: addressType, - address: address, - port: port, + cluster._getServer = function(obj, options, cb) { + const key = [ options.address, + options.port, + options.addressType, + options.fd ].join(':'); + if (indexes[key] === undefined) + indexes[key] = 0; + else + indexes[key]++; + + const message = util._extend({ act: 'queryServer', - fd: fd, + index: indexes[key], data: null - }; + }, options); + // Set custom data on handle (i.e. tls tickets key) if (obj._getServerData) message.data = obj._getServerData(); send(message, function(reply, handle) { @@ -549,9 +559,9 @@ function workerInit() { }); obj.once('listening', function() { cluster.worker.state = 'listening'; - var address = obj.address(); + const address = obj.address(); message.act = 'listening'; - message.port = address && address.port || port; + message.port = address && address.port || options.port; send(message); }); }; @@ -563,6 +573,7 @@ function workerInit() { // closed. Avoids resource leaks when the handle is short-lived. var close = handle.close; handle.close = function() { + send({ act: 'close', key: key }); delete handles[key]; return close.apply(this, arguments); }; |