raft: only allow one message to Step

This commit is contained in:
Blake Mizerany 2014-08-27 17:22:34 -07:00 committed by Yicheng Qin
parent 38e8f3b764
commit 1eb2512961

View File

@ -2,8 +2,6 @@
package raft
import (
"sort"
"code.google.com/p/go.net/context"
)
@ -111,31 +109,25 @@ func (n *Node) Tick() error {
// Propose proposes data be appended to the log.
func (n *Node) Propose(ctx context.Context, id int64, data []byte) error {
return n.Step(ctx, []Message{{Type: msgProp, Entries: []Entry{{Id: id, Data: data}}}})
return n.Step(ctx, Message{Type: msgProp, Entries: []Entry{{Id: id, Data: data}}})
}
// Step advances the state machine using msgs. Proposals are priotized last so
// that any votes and vote requests will not be wedged behind proposals and
// prevent this cluster from making progress. The ctx.Err() will be returned,
// Step advances the state machine using msgs. The ctx.Err() will be returned,
// if any.
func (n *Node) Step(ctx context.Context, msgs []Message) error {
sort.Sort(sort.Reverse(byMsgType(msgs)))
for _, m := range msgs {
ch := n.recvc
if m.Type == msgProp {
ch = n.propc
}
select {
case ch <- m:
return nil
case <-ctx.Done():
return ctx.Err()
case <-n.ctx.Done():
return n.ctx.Err()
}
func (n *Node) Step(ctx context.Context, m Message) error {
ch := n.recvc
if m.Type == msgProp {
ch = n.propc
}
select {
case ch <- m:
return nil
case <-ctx.Done():
return ctx.Err()
case <-n.ctx.Done():
return n.ctx.Err()
}
return nil
}
// ReadState returns the current point-in-time state.