Browse Source

fix a race where the policy and connection pool share a single slice of connections but have independant locks

Chris Bannister 10 years ago
parent
commit
245c78980f
1 changed files with 9 additions and 3 deletions
  1. 9 3
      connectionpool.go

+ 9 - 3
connectionpool.go

@@ -482,7 +482,11 @@ func (pool *hostConnPool) connect() error {
 	}
 	}
 
 
 	pool.conns = append(pool.conns, conn)
 	pool.conns = append(pool.conns, conn)
-	pool.policy.SetConns(pool.conns)
+
+	conns := make([]*Conn, len(pool.conns))
+	copy(conns, pool.conns)
+	pool.policy.SetConns(conns)
+
 	return nil
 	return nil
 }
 }
 
 
@@ -510,7 +514,9 @@ func (pool *hostConnPool) HandleError(conn *Conn, err error, closed bool) {
 			pool.conns[i], pool.conns = pool.conns[len(pool.conns)-1], pool.conns[:len(pool.conns)-1]
 			pool.conns[i], pool.conns = pool.conns[len(pool.conns)-1], pool.conns[:len(pool.conns)-1]
 
 
 			// update the policy
 			// update the policy
-			pool.policy.SetConns(pool.conns)
+			conns := make([]*Conn, len(pool.conns))
+			copy(conns, pool.conns)
+			pool.policy.SetConns(conns)
 
 
 			// lost a connection, so fill the pool
 			// lost a connection, so fill the pool
 			go pool.fill()
 			go pool.fill()
@@ -529,7 +535,7 @@ func (pool *hostConnPool) drain() {
 	pool.conns = pool.conns[:0:0]
 	pool.conns = pool.conns[:0:0]
 
 
 	// update the policy
 	// update the policy
-	pool.policy.SetConns(pool.conns)
+	pool.policy.SetConns(nil)
 
 
 	// close the connections
 	// close the connections
 	for _, conn := range conns {
 	for _, conn := range conns {