瀏覽代碼

Merge pull request #380 from Zariel/fix-policy-race

Ensure we hold a read lock to check hosts
Chris Bannister 10 年之前
父節點
當前提交
3404596a9c
共有 1 個文件被更改,包括 2 次插入1 次删除
  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)