-
Notifications
You must be signed in to change notification settings - Fork 0
/
follower.go
53 lines (45 loc) · 1.03 KB
/
follower.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package raft
import "context"
var _ server = (*follower)(nil)
// follower 实现一致性模型在 Follower 下的行为
type follower struct {
*raft
}
func (f *follower) Run() (server, error) {
for {
select {
case <-f.Done():
return nil, ErrStopped
case args := <-f.rpcArgs:
server, converted, err := f.reactToRPCArgs(args)
if err != nil {
return nil, err
}
if converted {
return server, nil
}
case <-f.ticker.C:
if !f.raft.configs.GetConfig().IncludePeer(f.Id()) {
continue
}
f.debug("Election timeout")
// If election timeout elapses without receiving AppendEntries
// RPC from current leader or granting vote to candidate:
// convert to candidate
return f.toCandidate(), nil
}
}
}
func (f *follower) Handle(context.Context, ...Command) error {
return ErrIsNotLeader
}
func (f *follower) ResetTimer() {
timeout := f.randomElectionTimeout()
f.ticker.Reset(timeout)
}
func (*follower) String() string {
return "Follower"
}
func (*follower) IsLeader() bool {
return false
}