diff --git a/consul/client.go b/consul/client.go index b3fdb080e8..a037f1eed9 100644 --- a/consul/client.go +++ b/consul/client.go @@ -43,9 +43,9 @@ type Client struct { // Connection pool to consul servers connPool *ConnPool - // consuls tracks the locally known servers - consuls []*serverParts - consulLock sync.RWMutex + // consulServers tracks the locally known servers + consulServers []*serverParts + consulLock sync.RWMutex // eventCh is used to receive events from the // serf cluster in the datacenter @@ -254,9 +254,9 @@ func (c *Client) nodeJoin(me serf.MemberEvent) { // Check if this server is known found := false c.consulLock.Lock() - for idx, existing := range c.consuls { + for idx, existing := range c.consulServers { if existing.Name == parts.Name { - c.consuls[idx] = parts + c.consulServers[idx] = parts found = true break } @@ -264,7 +264,7 @@ func (c *Client) nodeJoin(me serf.MemberEvent) { // Add to the list if not known if !found { - c.consuls = append(c.consuls, parts) + c.consulServers = append(c.consulServers, parts) } c.consulLock.Unlock() @@ -286,11 +286,11 @@ func (c *Client) nodeFail(me serf.MemberEvent) { // Remove the server if known c.consulLock.Lock() - n := len(c.consuls) + n := len(c.consulServers) for i := 0; i < n; i++ { - if c.consuls[i].Name == parts.Name { - c.consuls[i], c.consuls[n-1] = c.consuls[n-1], nil - c.consuls = c.consuls[:n-1] + if c.consulServers[i].Name == parts.Name { + c.consulServers[i], c.consulServers[n-1] = c.consulServers[n-1], nil + c.consulServers = c.consulServers[:n-1] break } } @@ -339,13 +339,14 @@ func (c *Client) RPC(method string, args interface{}, reply interface{}) error { // Bail if we can't find any servers c.consulLock.RLock() - if len(c.consuls) == 0 { + numConsulServers = len(c.consulServers) + if numConsulServers == 0 { c.consulLock.RUnlock() return structs.ErrNoServers } // Select a random addr - server = c.consuls[rand.Int31()%int32(len(c.consuls))] + server = c.consulServers[rand.Int31n(int32(numConsulServers))] c.consulLock.RUnlock() // Forward to remote Consul @@ -371,7 +372,7 @@ func (c *Client) Stats() map[string]map[string]string { stats := map[string]map[string]string{ "consul": map[string]string{ "server": "false", - "known_servers": toString(uint64(len(c.consuls))), + "known_servers": toString(uint64(len(c.consulServers))), }, "serf_lan": c.serf.Stats(), "runtime": runtimeStats(),