Explorar o código

Ensure we hold a read lock to check hosts

When the policy is checking the length of the host list in
roundRobinHostPolicy ensure that we hold the read lock so that
if SetHosts is called we dont data race.
Chris Bannister %!s(int64=10) %!d(string=hai) anos
pai
achega
1d1b72e21a
Modificáronse 1 ficheiros con 2 adicións e 1 borrados
  1. 2 1
      policies.go

+ 2 - 1
policies.go

@@ -87,12 +87,13 @@ func (r *roundRobinHostPolicy) Pick(qry *Query) NextHost {
 	// to the number of hosts known to this policy
 	var i uint32 = 0
 	return func() *HostInfo {
+		r.mu.RLock()
 		if len(r.hosts) == 0 {
+			r.mu.RUnlock()
 			return nil
 		}
 
 		var host *HostInfo
-		r.mu.RLock()
 		// always increment pos to evenly distribute traffic in case of
 		// failures
 		pos := atomic.AddUint32(&r.pos, 1)