From 220424c7d192256662d2c9c327d9b1513c61c040 Mon Sep 17 00:00:00 2001 From: Philip O'Toole Date: Mon, 14 Nov 2022 10:13:40 -0500 Subject: [PATCH] Simple refactor, better Go style --- store/server.go | 14 +++++++------- store/server_test.go | 25 +++++++++++++------------ store/store.go | 4 ++-- 3 files changed, 22 insertions(+), 21 deletions(-) diff --git a/store/server.go b/store/server.go index f9b70629..dd8029e5 100644 --- a/store/server.go +++ b/store/server.go @@ -23,22 +23,18 @@ func NewServer(id, addr string, voter bool) *Server { // 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) { +func (s Servers) IsReadOnly(id string) (readOnly bool, found bool) { readOnly = false found = false - if servers == nil || id == "" { + if s == nil || id == "" { return } - for _, n := range servers { + for _, n := range s { if n != nil && n.ID == id { readOnly = n.Suffrage == "Nonvoter" found = true @@ -47,3 +43,7 @@ func IsReadOnly(servers []*Server, id string) (readOnly bool, found bool) { } 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] } diff --git a/store/server_test.go b/store/server_test.go index de4fc696..7cbdf391 100644 --- a/store/server_test.go +++ b/store/server_test.go @@ -5,41 +5,42 @@ import ( ) 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") } - servers = make([]*Server, 1) - if _, found := IsReadOnly(servers, ""); found { + nodes := make([]*Server, 1) + servers = Servers(nodes) + if _, found := servers.IsReadOnly(""); found { t.Fatalf("found should be false") } - if _, found := IsReadOnly(servers, "node1"); found { + if _, found := servers.IsReadOnly("node1"); found { t.Fatalf("found should be false") } - servers[0] = &Server{ + nodes[0] = &Server{ ID: "node1", Addr: "localhost:4002", 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) } - servers[0] = &Server{ + nodes[0] = &Server{ ID: "node1", Addr: "localhost:4002", 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) } - servers[0] = &Server{ + nodes[0] = &Server{ ID: "node1", Addr: "localhost:4002", Suffrage: "Nonvoter", } - if ro, found := IsReadOnly(servers, "node1"); !ro || !found { - t.Fatalf("IsReadOnly returned ro: %t, found: %t", ro, found) + if ro, found := servers.IsReadOnly("node1"); !ro || !found { + t.Fatalf("servers.IsReadOnly returned ro: %t, found: %t", ro, found) } } diff --git a/store/store.go b/store/store.go index 24a03db7..1ba11825 100644 --- a/store/store.go +++ b/store/store.go @@ -1355,11 +1355,11 @@ func (s *Store) observe() (closeCh, doneCh chan struct{}) { if err != nil { s.logger.Printf("failed to get nodes configuration during reap check: %s", err.Error()) } - + servers := Servers(nodes) id := string(signal.PeerID) dur := time.Since(signal.LastContact) - isReadOnly, found := IsReadOnly(nodes, id) + isReadOnly, found := servers.IsReadOnly(id) if !found { s.logger.Printf("node %s is not present in configuration", id) break