Skip to content

Commit

Permalink
cluster: remove bind() and self
Browse files Browse the repository at this point in the history
This commit removes the use of self and bind() from the cluster
module in favor of arrow functions.

PR-URL: #7710
Reviewed-By: Michaël Zasso <mic.besace@gmail.com>
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
Reviewed-By: Minwoo Jung <jmwsoft@gmail.com>
  • Loading branch information
cjihrig authored and MylesBorins committed Oct 26, 2016
1 parent b0e2f9a commit 227db0a
Showing 1 changed file with 16 additions and 18 deletions.
34 changes: 16 additions & 18 deletions lib/cluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,31 +114,29 @@ function RoundRobinHandle(key, address, port, addressType, backlog, fd) {
else
this.server.listen(address); // UNIX socket path.

var self = this;
this.server.once('listening', function() {
self.handle = self.server._handle;
self.handle.onconnection = self.distribute.bind(self);
self.server._handle = null;
self.server = null;
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(worker.id in this.all === false);
this.all[worker.id] = worker;

var self = this;
function done() {
if (self.handle.getsockname) {
const done = () => {
if (this.handle.getsockname) {
var out = {};
self.handle.getsockname(out);
this.handle.getsockname(out);
// TODO(bnoordhuis) Check err.
send(null, { sockname: out }, null);
} else {
send(null, null, null); // UNIX socket.
}
self.handoff(worker); // In case there are connections pending.
}
this.handoff(worker); // In case there are connections pending.
};

if (this.server === null) return done();
// Still busy binding.
Expand Down Expand Up @@ -180,13 +178,13 @@ RoundRobinHandle.prototype.handoff = function(worker) {
return;
}
var message = { act: 'newconn', key: this.key };
var self = this;
sendHelper(worker.process, message, handle, function(reply) {

sendHelper(worker.process, message, handle, (reply) => {
if (reply.accepted)
handle.close();
else
self.distribute(0, handle); // Worker is shutting down. Send to another.
self.handoff(worker);
this.distribute(0, handle); // Worker is shutting down. Send to another.
this.handoff(worker);
});
};

Expand Down Expand Up @@ -399,7 +397,7 @@ function masterInit() {
cluster.disconnect = function(cb) {
var workers = Object.keys(cluster.workers);
if (workers.length === 0) {
process.nextTick(intercom.emit.bind(intercom, 'disconnect'));
process.nextTick(() => intercom.emit('disconnect'));
} else {
for (var key in workers) {
key = workers[key];
Expand All @@ -421,7 +419,7 @@ function masterInit() {
signo = signo || 'SIGTERM';
var proc = this.process;
if (this.isConnected()) {
this.once('disconnect', proc.kill.bind(proc, signo));
this.once('disconnect', () => proc.kill(signo));
this.disconnect();
return;
}
Expand Down

0 comments on commit 227db0a

Please sign in to comment.