1
0
Fork 0

Simple refactor, better Go style

master
Philip O'Toole 2 years ago
parent 1979876b0a
commit 220424c7d1

@ -23,22 +23,18 @@ func NewServer(id, addr string, voter bool) *Server {
// Servers is a set of Servers. // Servers is a set of Servers.
type Servers []*Server 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, // 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 // is a read-only (non-voting) node. If no node is found with the given ID
// then found will be false. // then found will be false.
func IsReadOnly(servers []*Server, id string) (readOnly bool, found bool) { func (s Servers) IsReadOnly(id string) (readOnly bool, found bool) {
readOnly = false readOnly = false
found = false found = false
if servers == nil || id == "" { if s == nil || id == "" {
return return
} }
for _, n := range servers { for _, n := range s {
if n != nil && n.ID == id { if n != nil && n.ID == id {
readOnly = n.Suffrage == "Nonvoter" readOnly = n.Suffrage == "Nonvoter"
found = true found = true
@ -47,3 +43,7 @@ func IsReadOnly(servers []*Server, id string) (readOnly bool, found bool) {
} }
return return
} }
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] }

@ -5,41 +5,42 @@ import (
) )
func Test_IsReadOnly(t *testing.T) { func Test_IsReadOnly(t *testing.T) {
var servers []*Server var servers Servers
if _, found := IsReadOnly(nil, "1"); found { if _, found := servers.IsReadOnly("1"); found {
t.Fatalf("found should be false") t.Fatalf("found should be false")
} }
servers = make([]*Server, 1) nodes := make([]*Server, 1)
if _, found := IsReadOnly(servers, ""); found { servers = Servers(nodes)
if _, found := servers.IsReadOnly(""); found {
t.Fatalf("found should be false") t.Fatalf("found should be false")
} }
if _, found := IsReadOnly(servers, "node1"); found { if _, found := servers.IsReadOnly("node1"); found {
t.Fatalf("found should be false") t.Fatalf("found should be false")
} }
servers[0] = &Server{ nodes[0] = &Server{
ID: "node1", ID: "node1",
Addr: "localhost:4002", Addr: "localhost:4002",
Suffrage: "Voter", Suffrage: "Voter",
} }
if ro, found := IsReadOnly(servers, "node1"); ro || !found { if ro, found := servers.IsReadOnly("node1"); ro || !found {
t.Fatalf("IsReadOnly returned ro: %t, found: %t", ro, found) t.Fatalf("IsReadOnly returned ro: %t, found: %t", ro, found)
} }
servers[0] = &Server{ nodes[0] = &Server{
ID: "node1", ID: "node1",
Addr: "localhost:4002", Addr: "localhost:4002",
Suffrage: "Voter", Suffrage: "Voter",
} }
if ro, found := IsReadOnly(servers, "node2"); found { if ro, found := servers.IsReadOnly("node2"); found {
t.Fatalf("IsReadOnly returned ro: %t, found: %t", ro, found) t.Fatalf("IsReadOnly returned ro: %t, found: %t", ro, found)
} }
servers[0] = &Server{ nodes[0] = &Server{
ID: "node1", ID: "node1",
Addr: "localhost:4002", Addr: "localhost:4002",
Suffrage: "Nonvoter", Suffrage: "Nonvoter",
} }
if ro, found := IsReadOnly(servers, "node1"); !ro || !found { if ro, found := servers.IsReadOnly("node1"); !ro || !found {
t.Fatalf("IsReadOnly returned ro: %t, found: %t", ro, found) t.Fatalf("servers.IsReadOnly returned ro: %t, found: %t", ro, found)
} }
} }

@ -1355,11 +1355,11 @@ func (s *Store) observe() (closeCh, doneCh chan struct{}) {
if err != nil { if err != nil {
s.logger.Printf("failed to get nodes configuration during reap check: %s", err.Error()) s.logger.Printf("failed to get nodes configuration during reap check: %s", err.Error())
} }
servers := Servers(nodes)
id := string(signal.PeerID) id := string(signal.PeerID)
dur := time.Since(signal.LastContact) dur := time.Since(signal.LastContact)
isReadOnly, found := IsReadOnly(nodes, id) isReadOnly, found := servers.IsReadOnly(id)
if !found { if !found {
s.logger.Printf("node %s is not present in configuration", id) s.logger.Printf("node %s is not present in configuration", id)
break break

Loading…
Cancel
Save