Use rand.Int31n() vs unconditionally using modulus

This commit is contained in:
Sean Chittenden 2016-01-30 15:47:58 -08:00
parent 7fb0045bbe
commit 0c83b1b692
1 changed files with 4 additions and 2 deletions

View File

@ -334,6 +334,7 @@ func (c *Client) localEvent(event serf.UserEvent) {
func (c *Client) RPC(method string, args interface{}, reply interface{}) error {
// Check the last rpc time
now := time.Now()
var numConsulServers int
lastRPCTime := now.Sub(c.lastRPCTime)
var server *serverParts
if c.lastServer != nil && lastRPCTime < clientRPCConnMaxIdle {
@ -345,13 +346,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.consuls)
if numConsulServers == 0 {
c.consulLock.RUnlock()
return structs.ErrNoServers
}
// Select a random addr
server = c.consuls[rand.Int31()%int32(len(c.consuls))]
server = c.consuls[rand.Int31n(int32(numConsulServers))]
c.consulLock.RUnlock()
// Forward to remote Consul