Skip to content

Commit

Permalink
Minor refactor to sending pings ahead of damping
Browse files Browse the repository at this point in the history
  • Loading branch information
jwolski committed Apr 2, 2015
1 parent 3539650 commit 5810660
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 27 deletions.
12 changes: 5 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var hammock = require('uber-hammock');
var metrics = require('metrics');

var Gossip = require('./lib/swim/gossip');
var PingSender = require('./lib/swim/ping-sender');
var sendPing = require('./lib/swim/ping-sender.js');
var sendPingReq = require('./lib/swim/ping-req-sender.js');
var Suspicion = require('./lib/swim/suspicion');

Expand Down Expand Up @@ -569,7 +569,10 @@ RingPop.prototype.pingMemberNow = function pingMemberNow(callback) {
var self = this;
this.isPinging = true;
var start = new Date();
this.sendPing(member, function(isOk, body) {
sendPing({
ringpop: self,
target: member
}, function(isOk, body) {
self.stat('timing', 'ping', start);
if (isOk) {
self.isPinging = false;
Expand Down Expand Up @@ -645,11 +648,6 @@ RingPop.prototype.seedBootstrapHosts = function seedBootstrapHosts(file) {
}
};

RingPop.prototype.sendPing = function sendPing(member, callback) {
this.stat('increment', 'ping.send');
return new PingSender(this, member, callback);
};

RingPop.prototype.setDebugFlag = function setDebugFlag(flag) {
this.debugFlags[flag] = true;
};
Expand Down
7 changes: 6 additions & 1 deletion lib/swim/ping-req-recvr.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
// THE SOFTWARE.
'use strict';

var sendPing = require('./ping-sender.js');

module.exports = function recvPingReq(opts, callback) {
var ringpop = opts.ringpop;

Expand All @@ -36,7 +38,10 @@ module.exports = function recvPingReq(opts, callback) {
ringpop.debugLog('ping-req send ping source=' + source + ' target=' + target, 'p');

var start = new Date();
ringpop.sendPing(target, function (isOk, body) {
sendPing({
ringpop: ringpop,
target: target
}, function (isOk, body) {
ringpop.stat('timing', 'ping-req-ping', start);
ringpop.debugLog('ping-req recv ping source=' + source + ' target=' + target + ' isOk=' + isOk, 'p');

Expand Down
45 changes: 26 additions & 19 deletions lib/swim/ping-sender.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,6 @@ function PingSender(ring, member, callback) {
this.ring = ring;
this.address = member.address || member;
this.callback = callback;

var options = {
host: this.address,
timeout: ring.pingTimeout
};
var changes = ring.dissemination.issueChanges();
var body = JSON.stringify({
checksum: ring.membership.checksum,
changes: changes,
source: ring.whoami()
});

this.ring.debugLog('ping send member=' + this.address + ' changes=' + JSON.stringify(changes), 'p');

var self = this;
this.ring.channel.send(options, '/protocol/ping', null, body, function(err, res1, res2) {
self.onPing(err, res1, res2);
});
}

PingSender.prototype.onPing = function onPing(err, res1, res2) {
Expand Down Expand Up @@ -71,4 +53,29 @@ PingSender.prototype.doCallback = function doCallback(isOk, bodyObj) {
}
};

module.exports = PingSender;
PingSender.prototype.send = function send() {
var options = {
host: this.address,
timeout: this.ring.pingTimeout
};
var changes = this.ring.dissemination.issueChanges();
var body = JSON.stringify({
checksum: this.ring.membership.checksum,
changes: changes,
source: this.ring.whoami()
});

this.ring.debugLog('ping send member=' + this.address + ' changes=' + JSON.stringify(changes), 'p');

var self = this;
this.ring.channel.send(options, '/protocol/ping', null, body, function(err, res1, res2) {
self.onPing(err, res1, res2);
});
};

module.exports = function sendPing(opts, callback) {
opts.ringpop.stat('increment', 'ping.send');

var sender = new PingSender(opts.ringpop, opts.target, callback);
sender.send();
};

0 comments on commit 5810660

Please sign in to comment.