Skip to content

Commit

Permalink
IMPROVE leader election time when other is leader
Browse files Browse the repository at this point in the history
  • Loading branch information
pubkey committed Dec 2, 2021
1 parent 2732120 commit db847eb
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions src/leader-election.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,19 @@ LeaderElection.prototype = {
return PROMISE_RESOLVED_TRUE;
}
let stopCriteria = false;
let stopCriteriaPromiseResolve;
/**
* Resolves when a stop criteria is reached.
* Uses as a performance shortcut so we do not
* have to await the responseTime when it is already clear
* that the election failed.
*/
const stopCriteriaPromise = new Promise(res => {
stopCriteriaPromiseResolve = () => {
stopCriteria = true;
res();
};
});
const recieved = [];
const handleMessage = (msg) => {
if (msg.context === 'leader' && msg.token != this.token) {
Expand All @@ -101,24 +114,31 @@ LeaderElection.prototype = {
* other has higher token
* -> stop applying and let other become leader.
*/
stopCriteria = true;
stopCriteriaPromiseResolve();
}
}

if (msg.action === 'tell') {
// other is already leader
stopCriteria = true;
stopCriteriaPromiseResolve();
this.hasLeader = true;
}
}
};
this.broadcastChannel.addEventListener('internal', handleMessage);
const applyPromise = _sendMessage(this, 'apply') // send out that this one is applying
.then(() => sleep(this._options.responseTime / 2))
.then(() => Promise.race([
sleep(this._options.responseTime / 2),
stopCriteriaPromise.then(() => Promise.reject(new Error()))
]))
// send again in case another instance was just created
.then(() => _sendMessage(this, 'apply'))
// let others time to respond
.then(() => sleep(this._options.responseTime / 2))
.then(() => Promise.race([
sleep(this._options.responseTime / 2),
stopCriteriaPromise.then(() => Promise.reject(new Error()))
]))
.catch(() => { })
.then(() => {
this.broadcastChannel.removeEventListener('internal', handleMessage);
if (!stopCriteria) {
Expand Down

0 comments on commit db847eb

Please sign in to comment.