Compare commits

...

No commits in common. "b8701b4aa7c06086c1e91cd8887bee9f5d487534" and "713e10d22341fa25a4e031d4aa04ca28b1af33ba" have entirely different histories.

1 changed files with 16 additions and 9 deletions

View File

@ -200,18 +200,25 @@ class TinyRaft extends EventEmitter
this.votes[msg.leader] = this.votes[msg.leader] || [];
this.votes[msg.leader].push(from);
const n = this.votes[msg.leader].length;
if (n == 1 + (0 | this.nodes.length/2) && msg.leader == this.nodeId)
if (n == 1 + (0 | this.nodes.length/2))
{
this.leader = msg.leader;
this.state = LEADER;
this._nextTerm(this.leadershipTimeout > 0 ? this.leadershipTimeout : -1);
this.followers = this.votes[this.nodeId];
for (const follower of this.followers)
if (msg.leader == this.nodeId)
{
// Send a heartbeat to confirm leadership
this.send(follower, { type: PING, term: this.term });
this.leader = msg.leader;
this.state = LEADER;
this._nextTerm(this.leadershipTimeout > 0 ? this.leadershipTimeout : -1);
this.followers = this.votes[this.nodeId];
for (const follower of this.followers)
{
// Send a heartbeat to confirm leadership
this.send(follower, { type: PING, term: this.term });
}
this.emit('change', { state: this.state, term: this.term, leader: this.nodeId, followers: this.votes[this.nodeId] });
}
else
{
this._nextTerm(0);
}
this.emit('change', { state: this.state, term: this.term, leader: this.nodeId, followers: this.votes[this.nodeId] });
}
else if (n > this.nodes.length/2 && this.state == LEADER && msg.leader == this.nodeId)
{