raft: start tick

release-2.0
Blake Mizerany 2014-08-21 00:30:29 -07:00 committed by Yicheng Qin
parent 8d37587e47
commit e17f79ee70
2 changed files with 15 additions and 1 deletions

View File

@ -19,6 +19,7 @@ type Node struct {
propc chan proposal
recvc chan Message
statec chan stateResp
tickc chan struct{}
}
func Start(ctx context.Context, name string, election, heartbeat int) *Node {
@ -27,6 +28,7 @@ func Start(ctx context.Context, name string, election, heartbeat int) *Node {
propc: make(chan proposal),
recvc: make(chan Message),
statec: make(chan stateResp),
tickc: make(chan struct{}),
}
r := &raft{
name: name,
@ -55,6 +57,8 @@ func (n *Node) run(r *raft) {
r.propose(p.id, p.data)
case m := <-n.recvc:
r.step(m)
case <-n.tickc:
r.tick()
case n.statec <- stateResp{r.State, r.ents, r.msgs}:
r.resetState()
case <-n.ctx.Done():
@ -63,6 +67,15 @@ func (n *Node) run(r *raft) {
}
}
func (n *Node) Tick() error {
select {
case n.tickc <- struct{}{}:
return nil
case <-n.ctx.Done():
return n.ctx.Err()
}
}
// Propose proposes data be appended to the log.
func (n *Node) Propose(id int64, data []byte) error {
select {
@ -83,7 +96,7 @@ func (n *Node) Step(m Message) error {
}
}
// ReadMessages returns the current point-in-time state.
// ReadState returns the current point-in-time state.
func (n *Node) ReadState() (State, []Entry, []Message, error) {
select {
case sr := <-n.statec:

View File

@ -32,3 +32,4 @@ func (sm *raft) hasLeader() bool { return false }
func (sm *raft) step(m Message) {}
func (sm *raft) resetState() {}
func (sm *raft) propose(id int64, data []byte) {}
func (sm *raft) tick() {}