Skip to content

Commit

Permalink
4.8.0
Browse files Browse the repository at this point in the history
  • Loading branch information
pubkey committed Dec 15, 2021
1 parent 3bc74c7 commit ee90c8c
Show file tree
Hide file tree
Showing 12 changed files with 382 additions and 182 deletions.
56 changes: 38 additions & 18 deletions dist/es5node/leader-election.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ var LeaderElection = function LeaderElection(broadcastChannel, options) {

this._lstns = []; // _listeners

this._invs = []; // _intervals

this._dpL = function () {}; // onduplicate listener


Expand Down Expand Up @@ -69,7 +67,8 @@ LeaderElection.prototype = {
* false if not.
* @async
*/
applyOnce: function applyOnce() {
applyOnce: function applyOnce( // true if the applyOnce() call came from the fallbackInterval cycle
isFromFallbackInterval) {
var _this2 = this;

if (this.isLeader) {
Expand Down Expand Up @@ -144,18 +143,31 @@ LeaderElection.prototype = {
};

_this2.broadcastChannel.addEventListener('internal', handleMessage);
/**
* If the applyOnce() call came from the fallbackInterval,
* we can assume that the election runs in the background and
* not critical process is waiting for it.
* When this is true, we give the other intances
* more time to answer to messages in the election cycle.
* This makes it less likely to elect duplicate leaders.
* But also it takes longer which is not a problem because we anyway
* run in the background.
*/


var waitForAnswerTime = isFromFallbackInterval ? _this2._options.responseTime * 4 : _this2._options.responseTime;

var applyPromise = _sendMessage(_this2, 'apply') // send out that this one is applying
.then(function () {
return Promise.race([(0, _util.sleep)(_this2._options.responseTime / 2), stopCriteriaPromise.then(function () {
return Promise.race([(0, _util.sleep)(waitForAnswerTime), stopCriteriaPromise.then(function () {
return Promise.reject(new Error());
})]);
}) // send again in case another instance was just created
.then(function () {
return _sendMessage(_this2, 'apply');
}) // let others time to respond
.then(function () {
return Promise.race([(0, _util.sleep)(_this2._options.responseTime / 2), stopCriteriaPromise.then(function () {
return Promise.race([(0, _util.sleep)(waitForAnswerTime), stopCriteriaPromise.then(function () {
return Promise.reject(new Error());
})]);
})["catch"](function () {}).then(function () {
Expand Down Expand Up @@ -208,12 +220,6 @@ LeaderElection.prototype = {

this._lstns = [];

this._invs.forEach(function (interval) {
return clearInterval(interval);
});

this._invs = [];

this._unl.forEach(function (uFn) {
return uFn.remove();
});
Expand Down Expand Up @@ -247,7 +253,6 @@ function _awaitLeadershipOnce(leaderElector) {
}

resolved = true;
clearInterval(interval);
leaderElector.broadcastChannel.removeEventListener('internal', whenDeathListener);
res(true);
} // try once now
Expand All @@ -257,18 +262,33 @@ function _awaitLeadershipOnce(leaderElector) {
if (leaderElector.isLeader) {
finish();
}
}); // try on fallbackInterval
});
/**
* Try on fallbackInterval
* @recursive
*/

var tryOnFallBack = function tryOnFallBack() {
return (0, _util.sleep)(leaderElector._options.fallbackInterval).then(function () {
if (leaderElector.isDead || resolved) {
return;
}

var interval = setInterval(function () {
leaderElector.applyOnce().then(function () {
if (leaderElector.isLeader) {
finish();
} else {
return leaderElector.applyOnce(true).then(function () {
if (leaderElector.isLeader) {
finish();
} else {
tryOnFallBack();
}
});
}
});
}, leaderElector._options.fallbackInterval);

leaderElector._invs.push(interval); // try when other leader dies
};

tryOnFallBack(); // try when other leader dies

var whenDeathListener = function whenDeathListener(msg) {
if (msg.context === 'leader' && msg.action === 'death') {
Expand Down
56 changes: 38 additions & 18 deletions dist/esbrowser/leader-election.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ var LeaderElection = function LeaderElection(broadcastChannel, options) {

this._lstns = []; // _listeners

this._invs = []; // _intervals

this._dpL = function () {}; // onduplicate listener


Expand Down Expand Up @@ -60,7 +58,8 @@ LeaderElection.prototype = {
* false if not.
* @async
*/
applyOnce: function applyOnce() {
applyOnce: function applyOnce( // true if the applyOnce() call came from the fallbackInterval cycle
isFromFallbackInterval) {
var _this2 = this;

if (this.isLeader) {
Expand Down Expand Up @@ -135,18 +134,31 @@ LeaderElection.prototype = {
};

_this2.broadcastChannel.addEventListener('internal', handleMessage);
/**
* If the applyOnce() call came from the fallbackInterval,
* we can assume that the election runs in the background and
* not critical process is waiting for it.
* When this is true, we give the other intances
* more time to answer to messages in the election cycle.
* This makes it less likely to elect duplicate leaders.
* But also it takes longer which is not a problem because we anyway
* run in the background.
*/


var waitForAnswerTime = isFromFallbackInterval ? _this2._options.responseTime * 4 : _this2._options.responseTime;

var applyPromise = _sendMessage(_this2, 'apply') // send out that this one is applying
.then(function () {
return Promise.race([sleep(_this2._options.responseTime / 2), stopCriteriaPromise.then(function () {
return Promise.race([sleep(waitForAnswerTime), stopCriteriaPromise.then(function () {
return Promise.reject(new Error());
})]);
}) // send again in case another instance was just created
.then(function () {
return _sendMessage(_this2, 'apply');
}) // let others time to respond
.then(function () {
return Promise.race([sleep(_this2._options.responseTime / 2), stopCriteriaPromise.then(function () {
return Promise.race([sleep(waitForAnswerTime), stopCriteriaPromise.then(function () {
return Promise.reject(new Error());
})]);
})["catch"](function () {}).then(function () {
Expand Down Expand Up @@ -199,12 +211,6 @@ LeaderElection.prototype = {

this._lstns = [];

this._invs.forEach(function (interval) {
return clearInterval(interval);
});

this._invs = [];

this._unl.forEach(function (uFn) {
return uFn.remove();
});
Expand Down Expand Up @@ -238,7 +244,6 @@ function _awaitLeadershipOnce(leaderElector) {
}

resolved = true;
clearInterval(interval);
leaderElector.broadcastChannel.removeEventListener('internal', whenDeathListener);
res(true);
} // try once now
Expand All @@ -248,18 +253,33 @@ function _awaitLeadershipOnce(leaderElector) {
if (leaderElector.isLeader) {
finish();
}
}); // try on fallbackInterval
});
/**
* Try on fallbackInterval
* @recursive
*/

var tryOnFallBack = function tryOnFallBack() {
return sleep(leaderElector._options.fallbackInterval).then(function () {
if (leaderElector.isDead || resolved) {
return;
}

var interval = setInterval(function () {
leaderElector.applyOnce().then(function () {
if (leaderElector.isLeader) {
finish();
} else {
return leaderElector.applyOnce(true).then(function () {
if (leaderElector.isLeader) {
finish();
} else {
tryOnFallBack();
}
});
}
});
}, leaderElector._options.fallbackInterval);

leaderElector._invs.push(interval); // try when other leader dies
};

tryOnFallBack(); // try when other leader dies

var whenDeathListener = function whenDeathListener(msg) {
if (msg.context === 'leader' && msg.action === 'death') {
Expand Down
56 changes: 38 additions & 18 deletions dist/esnode/leader-election.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ var LeaderElection = function LeaderElection(broadcastChannel, options) {

this._lstns = []; // _listeners

this._invs = []; // _intervals

this._dpL = function () {}; // onduplicate listener


Expand Down Expand Up @@ -60,7 +58,8 @@ LeaderElection.prototype = {
* false if not.
* @async
*/
applyOnce: function applyOnce() {
applyOnce: function applyOnce( // true if the applyOnce() call came from the fallbackInterval cycle
isFromFallbackInterval) {
var _this2 = this;

if (this.isLeader) {
Expand Down Expand Up @@ -135,18 +134,31 @@ LeaderElection.prototype = {
};

_this2.broadcastChannel.addEventListener('internal', handleMessage);
/**
* If the applyOnce() call came from the fallbackInterval,
* we can assume that the election runs in the background and
* not critical process is waiting for it.
* When this is true, we give the other intances
* more time to answer to messages in the election cycle.
* This makes it less likely to elect duplicate leaders.
* But also it takes longer which is not a problem because we anyway
* run in the background.
*/


var waitForAnswerTime = isFromFallbackInterval ? _this2._options.responseTime * 4 : _this2._options.responseTime;

var applyPromise = _sendMessage(_this2, 'apply') // send out that this one is applying
.then(function () {
return Promise.race([sleep(_this2._options.responseTime / 2), stopCriteriaPromise.then(function () {
return Promise.race([sleep(waitForAnswerTime), stopCriteriaPromise.then(function () {
return Promise.reject(new Error());
})]);
}) // send again in case another instance was just created
.then(function () {
return _sendMessage(_this2, 'apply');
}) // let others time to respond
.then(function () {
return Promise.race([sleep(_this2._options.responseTime / 2), stopCriteriaPromise.then(function () {
return Promise.race([sleep(waitForAnswerTime), stopCriteriaPromise.then(function () {
return Promise.reject(new Error());
})]);
})["catch"](function () {}).then(function () {
Expand Down Expand Up @@ -199,12 +211,6 @@ LeaderElection.prototype = {

this._lstns = [];

this._invs.forEach(function (interval) {
return clearInterval(interval);
});

this._invs = [];

this._unl.forEach(function (uFn) {
return uFn.remove();
});
Expand Down Expand Up @@ -238,7 +244,6 @@ function _awaitLeadershipOnce(leaderElector) {
}

resolved = true;
clearInterval(interval);
leaderElector.broadcastChannel.removeEventListener('internal', whenDeathListener);
res(true);
} // try once now
Expand All @@ -248,18 +253,33 @@ function _awaitLeadershipOnce(leaderElector) {
if (leaderElector.isLeader) {
finish();
}
}); // try on fallbackInterval
});
/**
* Try on fallbackInterval
* @recursive
*/

var tryOnFallBack = function tryOnFallBack() {
return sleep(leaderElector._options.fallbackInterval).then(function () {
if (leaderElector.isDead || resolved) {
return;
}

var interval = setInterval(function () {
leaderElector.applyOnce().then(function () {
if (leaderElector.isLeader) {
finish();
} else {
return leaderElector.applyOnce(true).then(function () {
if (leaderElector.isLeader) {
finish();
} else {
tryOnFallBack();
}
});
}
});
}, leaderElector._options.fallbackInterval);

leaderElector._invs.push(interval); // try when other leader dies
};

tryOnFallBack(); // try when other leader dies

var whenDeathListener = function whenDeathListener(msg) {
if (msg.context === 'leader' && msg.action === 'death') {
Expand Down
Loading

0 comments on commit ee90c8c

Please sign in to comment.