1
0
Fork 0
You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.1 KiB
Go

package store
// Server represents another node in the cluster.
type Server struct {
ID string `json:"id,omitempty"`
Addr string `json:"addr,omitempty"`
Suffrage string `json:"suffrage,omitempty"`
}
// NewServer returns an initialized Server.
func NewServer(id, addr string, voter bool) *Server {
v := "voter"
if !voter {
v = "Nonvoter"
}
return &Server{
ID: id,
Addr: addr,
Suffrage: v,
}
}
// Servers is a set of Servers.
type Servers []*Server
func (s Servers) Less(i, j int) bool { return s[i].ID < s[j].ID }
func (s Servers) Len() int { return len(s) }
func (s Servers) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
// IsReadOnly returns whether the given node, as specified by its Raft ID,
// is a read-only (non-voting) node. If no node is found with the given ID
// then found will be false.
func IsReadOnly(servers []*Server, id string) (readOnly bool, found bool) {
readOnly = false
found = false
if servers == nil || id == "" {
return
}
for _, n := range servers {
if n != nil && n.ID == id {
readOnly = n.Suffrage == "Nonvoter"
found = true
return
}
}
return
}