summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJackson Tian <shyvo1987@gmail.com>2021-02-15 21:45:00 +0800
committerJackson Tian <shyvo1987@gmail.com>2021-02-16 19:02:01 +0800
commitae2310de7a1018fac64944660cbe230db6d0a1dd (patch)
treea6cb338cd1be19d33b42e1bc8078c0caba929310
parentce4798b55515827f2a68d269d0f433e3ea9828bb (diff)
downloadnode-new-ae2310de7a1018fac64944660cbe230db6d0a1dd.tar.gz
lib: use class instead of prototype for round_robin_handle/shared_handleshared_handle
-rw-r--r--lib/internal/cluster/round_robin_handle.js190
-rw-r--r--lib/internal/cluster/shared_handle.js76
2 files changed, 136 insertions, 130 deletions
diff --git a/lib/internal/cluster/round_robin_handle.js b/lib/internal/cluster/round_robin_handle.js
index 5dc53ef78b..08b63c0209 100644
--- a/lib/internal/cluster/round_robin_handle.js
+++ b/lib/internal/cluster/round_robin_handle.js
@@ -13,115 +13,119 @@ const net = require('net');
const { sendHelper } = require('internal/cluster/utils');
const { constants } = internalBinding('tcp_wrap');
-module.exports = RoundRobinHandle;
-
-function RoundRobinHandle(key, address, { port, fd, flags }) {
- this.key = key;
- this.all = new SafeMap();
- this.free = new SafeMap();
- this.handles = [];
- this.handle = null;
- this.server = net.createServer(assert.fail);
-
- if (fd >= 0)
- this.server.listen({ fd });
- else if (port >= 0) {
- this.server.listen({
- port,
- host: address,
- // Currently, net module only supports `ipv6Only` option in `flags`.
- ipv6Only: Boolean(flags & constants.UV_TCP_IPV6ONLY),
+class RoundRobinHandle {
+ constructor(key, address, { port, fd, flags }) {
+ this.key = key;
+ this.all = new SafeMap();
+ this.free = new SafeMap();
+ this.handles = [];
+ this.handle = null;
+ this.server = net.createServer(assert.fail);
+
+ if (fd >= 0)
+ this.server.listen({ fd });
+ else if (port >= 0) {
+ this.server.listen({
+ port,
+ host: address,
+ // Currently, net module only supports `ipv6Only` option in `flags`.
+ ipv6Only: Boolean(flags & constants.UV_TCP_IPV6ONLY),
+ });
+ } else
+ this.server.listen(address); // UNIX socket path.
+
+ this.server.once('listening', () => {
+ this.handle = this.server._handle;
+ this.handle.onconnection = (err, handle) => this.distribute(err, handle);
+ this.server._handle = null;
+ this.server = null;
});
- } else
- this.server.listen(address); // UNIX socket path.
-
- this.server.once('listening', () => {
- this.handle = this.server._handle;
- this.handle.onconnection = (err, handle) => this.distribute(err, handle);
- this.server._handle = null;
- this.server = null;
- });
-}
+ }
-RoundRobinHandle.prototype.add = function(worker, send) {
- assert(this.all.has(worker.id) === false);
- this.all.set(worker.id, worker);
-
- const done = () => {
- if (this.handle.getsockname) {
- const out = {};
- this.handle.getsockname(out);
- // TODO(bnoordhuis) Check err.
- send(null, { sockname: out }, null);
- } else {
- send(null, null, null); // UNIX socket.
- }
+ add(worker, send) {
+ assert(this.all.has(worker.id) === false);
+ this.all.set(worker.id, worker);
+
+ const done = () => {
+ if (this.handle.getsockname) {
+ const out = {};
+ this.handle.getsockname(out);
+ // TODO(bnoordhuis) Check err.
+ send(null, { sockname: out }, null);
+ } else {
+ send(null, null, null); // UNIX socket.
+ }
+
+ this.handoff(worker); // In case there are connections pending.
+ };
+
+ if (this.server === null)
+ return done();
+
+ // Still busy binding.
+ this.server.once('listening', done);
+ this.server.once('error', (err) => {
+ send(err.errno, null);
+ });
+ }
- this.handoff(worker); // In case there are connections pending.
- };
+ remove(worker) {
+ const existed = this.all.delete(worker.id);
- if (this.server === null)
- return done();
+ if (!existed)
+ return false;
- // Still busy binding.
- this.server.once('listening', done);
- this.server.once('error', (err) => {
- send(err.errno, null);
- });
-};
+ this.free.delete(worker.id);
-RoundRobinHandle.prototype.remove = function(worker) {
- const existed = this.all.delete(worker.id);
+ if (this.all.size !== 0)
+ return false;
- if (!existed)
- return false;
+ for (const handle of this.handles) {
+ handle.close();
+ }
+ this.handles = [];
- this.free.delete(worker.id);
+ this.handle.close();
+ this.handle = null;
+ return true;
+ }
- if (this.all.size !== 0)
- return false;
+ distribute(err, handle) {
+ ArrayPrototypePush(this.handles, handle);
+ const [workerEntry] = this.free;
- for (const handle of this.handles) {
- handle.close();
+ if (ArrayIsArray(workerEntry)) {
+ const [workerId, worker] = workerEntry;
+ this.free.delete(workerId);
+ this.handoff(worker);
+ }
}
- this.handles = [];
- this.handle.close();
- this.handle = null;
- return true;
-};
+ handoff(worker) {
+ if (!this.all.has(worker.id)) {
+ return; // Worker is closing (or has closed) the server.
+ }
-RoundRobinHandle.prototype.distribute = function(err, handle) {
- ArrayPrototypePush(this.handles, handle);
- const [ workerEntry ] = this.free;
+ const handle = ArrayPrototypeShift(this.handles);
- if (ArrayIsArray(workerEntry)) {
- const [ workerId, worker ] = workerEntry;
- this.free.delete(workerId);
- this.handoff(worker);
- }
-};
+ if (handle === undefined) {
+ this.free.set(worker.id, worker); // Add to ready queue again.
+ return;
+ }
-RoundRobinHandle.prototype.handoff = function(worker) {
- if (!this.all.has(worker.id)) {
- return; // Worker is closing (or has closed) the server.
- }
+ const message = { act: 'newconn', key: this.key };
- const handle = ArrayPrototypeShift(this.handles);
+ sendHelper(worker.process, message, handle, (reply) => {
+ if (reply.accepted)
+ handle.close();
+ else {
+ // Worker is shutting down. Send to another.
+ this.distribute(0, handle);
+ }
- if (handle === undefined) {
- this.free.set(worker.id, worker); // Add to ready queue again.
- return;
+ this.handoff(worker);
+ });
}
+}
- const message = { act: 'newconn', key: this.key };
-
- sendHelper(worker.process, message, handle, (reply) => {
- if (reply.accepted)
- handle.close();
- else
- this.distribute(0, handle); // Worker is shutting down. Send to another.
-
- this.handoff(worker);
- });
-};
+module.exports = RoundRobinHandle;
diff --git a/lib/internal/cluster/shared_handle.js b/lib/internal/cluster/shared_handle.js
index 87b83df200..cf8fa7c0b9 100644
--- a/lib/internal/cluster/shared_handle.js
+++ b/lib/internal/cluster/shared_handle.js
@@ -4,42 +4,44 @@ const assert = require('internal/assert');
const dgram = require('internal/dgram');
const net = require('net');
-module.exports = SharedHandle;
-
-function SharedHandle(key, address, { port, addressType, fd, flags }) {
- this.key = key;
- this.workers = new SafeMap();
- this.handle = null;
- this.errno = 0;
-
- let rval;
- if (addressType === 'udp4' || addressType === 'udp6')
- rval = dgram._createSocketHandle(address, port, addressType, fd, flags);
- else
- rval = net._createServerHandle(address, port, addressType, fd, flags);
-
- if (typeof rval === 'number')
- this.errno = rval;
- else
- this.handle = rval;
+class SharedHandle {
+ constructor(key, address, { port, addressType, fd, flags }) {
+ this.key = key;
+ this.workers = new SafeMap();
+ this.handle = null;
+ this.errno = 0;
+
+ let rval;
+ if (addressType === 'udp4' || addressType === 'udp6')
+ rval = dgram._createSocketHandle(address, port, addressType, fd, flags);
+ else
+ rval = net._createServerHandle(address, port, addressType, fd, flags);
+
+ if (typeof rval === 'number')
+ this.errno = rval;
+ else
+ this.handle = rval;
+ }
+
+ add(worker, send) {
+ assert(!this.workers.has(worker.id));
+ this.workers.set(worker.id, worker);
+ send(this.errno, null, this.handle);
+ }
+
+ remove(worker) {
+ if (!this.workers.has(worker.id))
+ return false;
+
+ this.workers.delete(worker.id);
+
+ if (this.workers.size !== 0)
+ return false;
+
+ this.handle.close();
+ this.handle = null;
+ return true;
+ }
}
-SharedHandle.prototype.add = function(worker, send) {
- assert(!this.workers.has(worker.id));
- this.workers.set(worker.id, worker);
- send(this.errno, null, this.handle);
-};
-
-SharedHandle.prototype.remove = function(worker) {
- if (!this.workers.has(worker.id))
- return false;
-
- this.workers.delete(worker.id);
-
- if (this.workers.size !== 0)
- return false;
-
- this.handle.close();
- this.handle = null;
- return true;
-};
+module.exports = SharedHandle;