diff options
author | cjihrig <cjihrig@gmail.com> | 2017-01-11 15:30:55 -0500 |
---|---|---|
committer | cjihrig <cjihrig@gmail.com> | 2017-01-13 17:01:07 -0500 |
commit | 2f885e7a4391c5d69e46f0d837fe23b8cbac72f6 (patch) | |
tree | cf82ba1794586b98e5262c298581f8c228b39063 /lib/internal/cluster/shared_handle.js | |
parent | 57e2ec4356695265d98521df6341f3049b198f02 (diff) | |
download | android-node-v8-2f885e7a4391c5d69e46f0d837fe23b8cbac72f6.tar.gz android-node-v8-2f885e7a4391c5d69e46f0d837fe23b8cbac72f6.tar.bz2 android-node-v8-2f885e7a4391c5d69e46f0d837fe23b8cbac72f6.zip |
cluster: refactor module into multiple files
This commit splits the existing cluster module into several
internal modules. More specifically, the cluster master and
worker implementations are separated, and the various data
structures are separated.
PR-URL: https://github.com/nodejs/node/pull/10746
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Sam Roberts <vieuxtech@gmail.com>
Diffstat (limited to 'lib/internal/cluster/shared_handle.js')
-rw-r--r-- | lib/internal/cluster/shared_handle.js | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/lib/internal/cluster/shared_handle.js b/lib/internal/cluster/shared_handle.js new file mode 100644 index 0000000000..c066377242 --- /dev/null +++ b/lib/internal/cluster/shared_handle.js @@ -0,0 +1,48 @@ +'use strict'; +const assert = require('assert'); +const dgram = require('dgram'); +const net = require('net'); + +module.exports = SharedHandle; + +function SharedHandle(key, address, port, addressType, fd, flags) { + this.key = key; + this.workers = []; + this.handle = null; + this.errno = 0; + + // 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, flags); + else + rval = net._createServerHandle(address, port, addressType, fd); + + if (typeof rval === 'number') + this.errno = rval; + else + this.handle = rval; +} + +SharedHandle.prototype.add = function(worker, send) { + assert(this.workers.indexOf(worker) === -1); + this.workers.push(worker); + send(this.errno, null, this.handle); +}; + +SharedHandle.prototype.remove = function(worker) { + const index = this.workers.indexOf(worker); + + if (index === -1) + return false; // The worker wasn't sharing this handle. + + this.workers.splice(index, 1); + + if (this.workers.length !== 0) + return false; + + this.handle.close(); + this.handle = null; + return true; +}; |