From 88fa51e6bfae1c0edeefb09b317f50e6b1491c6d Mon Sep 17 00:00:00 2001 From: Travis Turner Date: Wed, 24 Jan 2018 16:03:36 -0600 Subject: [PATCH 1/2] add lock around Node.status to avoid race condition --- cluster.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/cluster.go b/cluster.go index cdcf50b26..83b26b321 100644 --- a/cluster.go +++ b/cluster.go @@ -17,6 +17,7 @@ package pilosa import ( "encoding/binary" "hash/fnv" + "sync" "time" "github.com/pilosa/pilosa/internal" @@ -41,16 +42,21 @@ type Node struct { Scheme string `json:"scheme"` Host string `json:"host"` + mu sync.Mutex status *internal.NodeStatus `json:"status"` } // SetStatus sets the NodeStatus. func (n *Node) SetStatus(s *internal.NodeStatus) { + n.mu.Lock() n.status = s + n.mu.Unlock() } // SetState sets the Node.status.state. func (n *Node) SetState(s string) { + n.mu.Lock() + defer n.mu.Unlock() if n.status == nil { n.status = &internal.NodeStatus{} } From 878b188155e7fde5a7a4fe27780bb52eef118f60 Mon Sep 17 00:00:00 2001 From: Travis Turner Date: Wed, 24 Jan 2018 17:03:58 -0600 Subject: [PATCH 2/2] add accessor method for Node.status --- cluster.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/cluster.go b/cluster.go index 83b26b321..d51e1f88f 100644 --- a/cluster.go +++ b/cluster.go @@ -42,10 +42,17 @@ type Node struct { Scheme string `json:"scheme"` Host string `json:"host"` - mu sync.Mutex + mu sync.RWMutex status *internal.NodeStatus `json:"status"` } +// Status gets the NodeStatus. +func (n *Node) Status() *internal.NodeStatus { + n.mu.RLock() + defer n.mu.RUnlock() + return n.status +} + // SetStatus sets the NodeStatus. func (n *Node) SetStatus(s *internal.NodeStatus) { n.mu.Lock() @@ -203,7 +210,7 @@ func (c *Cluster) Status() *internal.ClusterStatus { func encodeClusterStatus(a []*Node) []*internal.NodeStatus { other := make([]*internal.NodeStatus, len(a)) for i := range a { - other[i] = a[i].status + other[i] = a[i].Status() } return other }