Skip to content

Commit

Permalink
Address Nils' PR feedback; use flag to control start/stop of expirati…
Browse files Browse the repository at this point in the history
…on timer
  • Loading branch information
jwolski committed Nov 25, 2015
1 parent 12b5696 commit 48084e7
Showing 1 changed file with 31 additions and 16 deletions.
47 changes: 31 additions & 16 deletions lib/gossip/damper.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,14 @@ function Damper(opts) {
// removes them.
this.dampedMembers = {}; // indexed by address

this.isDampTimerEnabled = false;
// This damp timer runs only when there are flappy members
// add to the flappers collection above.
this.isDampTimerEnabled = false;
this.dampTimer = null;

// The expiration timer runs only when there are damped members
// in the dampedMembers collection above.
this.isExpirationTimerEnabled = false;
this.expirationTimer = null;
}

Expand Down Expand Up @@ -161,7 +162,6 @@ Damper.prototype.addFlapper = function addFlapper(flapper) {
};

Damper.prototype.dampMember = function dampMember(addr) {
var self = this;
var member = this.ringpop.membership.findMemberByAddress(addr);

if (!member) {
Expand All @@ -187,22 +187,11 @@ Damper.prototype.dampMember = function dampMember(addr) {
this.removeFlapper(member.address);
this.addDampedMember(member.address);

if (!this.expirationTimer) {
scheduleExpiration();
this.logger.info('ringpop started damped member expiration timer', {
local: this.ringpop.whoami()
});
if (!this.isExpirationTimerEnabled) {
this._startExpirationTimer();
}

return true;

function scheduleExpiration() {
self.expirationTimer =
self.timers.setTimeout(function onTimeout() {
self.expireDampedMembers();
scheduleExpiration();
}, self.ringpop.config.get('dampedMemberExpirationInterval'));
}
};

Damper.prototype.expireDampedMembers = function expireDampedMembers() {
Expand Down Expand Up @@ -480,6 +469,31 @@ Damper.prototype._startDampTimer = function _startDampTimer() {
}
};

Damper.prototype._startExpirationTimer = function _startExpirationTimer() {
var self = this;

if (this.isExpirationTimerEnabled) {
this.logger.debug('ringpop damper expiration timer already started', {
local: this.ringpop.whoami()
});
return;
}

this.isExpirationTimerEnabled = true;
schedule();
this.logger.debug('ringpop damper expiration timer started', {
local: this.ringpop.whoami()
});

function schedule() {
self.expirationTimer =
self.timers.setTimeout(function onTimeout() {
self.expireDampedMembers();
if (self.isExpirationTimerEnabled) schedule();
}, self.ringpop.config.get('dampedMemberExpirationInterval'));
}
};

Damper.prototype._stopDampTimer = function _stopDampTimer() {
if (!this.isDampTimerEnabled) {
this.logger.debug('ringpop damper already stopped damp timer', {
Expand All @@ -497,7 +511,7 @@ Damper.prototype._stopDampTimer = function _stopDampTimer() {
};

Damper.prototype._stopExpirationTimer = function _stopExpirationTimer() {
if (!this.expirationTimer) {
if (!this.isExpirationTimerEnabled) {
this.logger.debug('ringpop damper expiration timer already stopped', {
local: this.ringpop.whoami()
});
Expand All @@ -506,6 +520,7 @@ Damper.prototype._stopExpirationTimer = function _stopExpirationTimer() {

this.timers.clearTimeout(this.expirationTimer);
this.expirationTimer = null;
this.isExpirationTimerEnabled = false;
this.logger.info('ringpop damper stopped expiration timer', {
local: this.ringpop.whoami()
});
Expand Down

0 comments on commit 48084e7

Please sign in to comment.