From cfeee3ce2460d96a59ca889ebf0a73b7a3c9975a Mon Sep 17 00:00:00 2001 From: Matt Jaffee Date: Thu, 21 Sep 2017 21:22:47 -0500 Subject: [PATCH 1/2] round robin hosts --- cluster.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/cluster.go b/cluster.go index 56a7e0f..dbb39a9 100644 --- a/cluster.go +++ b/cluster.go @@ -38,9 +38,10 @@ import ( // Cluster contains hosts in a Pilosa cluster. type Cluster struct { - hosts []*URI - okList []bool - mutex *sync.RWMutex + hosts []*URI + okList []bool + mutex *sync.RWMutex + lastHostIdx int } // DefaultCluster returns the default Cluster. @@ -73,13 +74,16 @@ func (c *Cluster) AddHost(address *URI) { func (c *Cluster) Host() *URI { c.mutex.RLock() var host *URI - for i, ok := range c.okList { + for i, _ := range c.okList { + idx := (i + c.lastHostIdx) % len(c.okList) + ok := c.okList[idx] if ok { - host = c.hosts[i] + host = c.hosts[idx] break } } c.mutex.RUnlock() + c.lastHostIdx++ if host != nil { return host } From 67d22c2afb8cafaca5e87c90654450da5792b9cc Mon Sep 17 00:00:00 2001 From: Matt Jaffee Date: Thu, 21 Sep 2017 21:48:46 -0500 Subject: [PATCH 2/2] roudn robin hosts --- cluster.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cluster.go b/cluster.go index dbb39a9..a2a79ec 100644 --- a/cluster.go +++ b/cluster.go @@ -72,7 +72,7 @@ func (c *Cluster) AddHost(address *URI) { // Host returns a host in the cluster. func (c *Cluster) Host() *URI { - c.mutex.RLock() + c.mutex.Lock() var host *URI for i, _ := range c.okList { idx := (i + c.lastHostIdx) % len(c.okList) @@ -82,8 +82,8 @@ func (c *Cluster) Host() *URI { break } } - c.mutex.RUnlock() c.lastHostIdx++ + c.mutex.Unlock() if host != nil { return host }