Skip to content
This repository has been archived by the owner on Sep 28, 2022. It is now read-only.

Commit

Permalink
Merge pull request #63 from jaffee/roundrobin
Browse files Browse the repository at this point in the history
Roundrobin
  • Loading branch information
jaffee authored Sep 22, 2017
2 parents 01223c2 + 67d22c2 commit ee14a8a
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -71,15 +72,18 @@ 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, 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++
c.mutex.Unlock()
if host != nil {
return host
}
Expand Down

0 comments on commit ee14a8a

Please sign in to comment.