node.go 507 B

12345678910111213141516171819202122232425262728293031
  1. package raft
  2. import "sync"
  3. type Interface interface {
  4. Step(m Message)
  5. }
  6. type Node struct {
  7. lk sync.Mutex
  8. sm *stateMachine
  9. }
  10. func New(k, addr int, next Interface) Interface {
  11. n := &Node{
  12. sm: newStateMachine(k, addr, next),
  13. }
  14. return n
  15. }
  16. // Propose asynchronously proposes data be applied to the underlying state machine.
  17. func (n *Node) Propose(data []byte) {
  18. m := Message{Type: msgHup, Data: data}
  19. n.Step(m)
  20. }
  21. func (n *Node) Step(m Message) {
  22. n.lk.Lock()
  23. defer n.lk.Unlock()
  24. n.sm.Step(m)
  25. }