-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
135 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package bullyelection | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
type NodeEvent uint8 | ||
|
||
const ( | ||
JoinEvent NodeEvent = iota + 1 | ||
LeaveEvent | ||
TransferLeadershipEvent | ||
ElectionEvent | ||
) | ||
|
||
func (evt NodeEvent) String() string { | ||
switch evt { | ||
case JoinEvent: | ||
return "join" | ||
case LeaveEvent: | ||
return "leave" | ||
case TransferLeadershipEvent: | ||
return "transfer_leadership" | ||
case ElectionEvent: | ||
return "election" | ||
} | ||
return "unknown event" | ||
} | ||
|
||
func (b *Bully) readNodeEventLoop(ctx context.Context, ch chan *nodeEventMsg) { | ||
defer b.wg.Done() | ||
|
||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
|
||
case msg := <-ch: | ||
select { | ||
case b.electionQueue <- msg: | ||
// ok | ||
default: | ||
b.opt.onErrorFunc(errors.Wrapf(ErrBullyBusy, "maybe hangup election, drop: %s", msg)) | ||
} | ||
} | ||
} | ||
} | ||
|
||
func (b *Bully) electionRunLoop(ctx context.Context) { | ||
defer b.wg.Done() | ||
|
||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
|
||
case msg := <-b.electionQueue: | ||
switch msg.evt { | ||
case JoinEvent, LeaveEvent, TransferLeadershipEvent: | ||
if err := b.startElection(ctx, msg.evt, msg.id); err != nil { | ||
b.opt.onErrorFunc(errors.Wrapf(err, "election failure")) | ||
continue | ||
} | ||
b.opt.observeFunc(b, msg.evt, msg.id, msg.addr) | ||
case ElectionEvent: | ||
// emit only | ||
b.opt.observeFunc(b, msg.evt, msg.id, msg.addr) | ||
} | ||
} | ||
} | ||
} |