context.go 918 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package raft
  2. // Context represents the current state of the server. It is passed into
  3. // a command when the command is being applied since the server methods
  4. // are locked.
  5. type Context interface {
  6. Server() Server
  7. CurrentTerm() uint64
  8. CurrentIndex() uint64
  9. CommitIndex() uint64
  10. }
  11. // context is the concrete implementation of Context.
  12. type context struct {
  13. server Server
  14. currentIndex uint64
  15. currentTerm uint64
  16. commitIndex uint64
  17. }
  18. // Server returns a reference to the server.
  19. func (c *context) Server() Server {
  20. return c.server
  21. }
  22. // CurrentTerm returns current term the server is in.
  23. func (c *context) CurrentTerm() uint64 {
  24. return c.currentTerm
  25. }
  26. // CurrentIndex returns current index the server is at.
  27. func (c *context) CurrentIndex() uint64 {
  28. return c.currentIndex
  29. }
  30. // CommitIndex returns last commit index the server is at.
  31. func (c *context) CommitIndex() uint64 {
  32. return c.commitIndex
  33. }