raft: attempt first version of Interface

This commit is contained in:
Blake Mizerany
2014-05-18 23:34:55 -07:00
committed by Yicheng Qin
parent 8d7be33dd8
commit 50e0db4038
3 changed files with 58 additions and 33 deletions

31
raft/node.go Normal file
View File

@@ -0,0 +1,31 @@
package raft
import "sync"
type Interface interface {
Step(m Message)
}
type Node struct {
lk sync.Mutex
sm *stateMachine
}
func New(k, addr int, next Interface) Interface {
n := &Node{
sm: newStateMachine(k, addr, next),
}
return n
}
// Propose asynchronously proposes data be applied to the underlying state machine.
func (n *Node) Propose(data []byte) {
m := Message{Type: msgHup, Data: data}
n.Step(m)
}
func (n *Node) Step(m Message) {
n.lk.Lock()
defer n.lk.Unlock()
n.sm.Step(m)
}