Bladeren bron

make closing the connection pool return when closed

Chris Bannister 10 jaren geleden
bovenliggende
commit
d914c36699
1 gewijzigde bestanden met toevoegingen van 12 en 10 verwijderingen
  1. 12 10
      connectionpool.go

+ 12 - 10
connectionpool.go

@@ -168,7 +168,7 @@ func (p *policyConnPool) SetHosts(hosts []*HostInfo) {
 	for addr := range toRemove {
 		pool := p.hostConnPools[addr]
 		delete(p.hostConnPools, addr)
-		pool.Close()
+		go pool.Close()
 	}
 
 	// update the policy
@@ -279,7 +279,7 @@ func (p *policyConnPool) removeHost(addr string) {
 	delete(p.hostConnPools, addr)
 	p.mu.Unlock()
 
-	pool.Close()
+	go pool.Close()
 }
 
 func (p *policyConnPool) hostUp(host *HostInfo) {
@@ -381,8 +381,7 @@ func (pool *hostConnPool) Close() {
 	}
 	pool.closed = true
 
-	// drain, but don't wait
-	go pool.drain()
+	pool.drainLocked()
 }
 
 // Fill the connection pool
@@ -569,14 +568,10 @@ func (pool *hostConnPool) HandleError(conn *Conn, err error, closed bool) {
 	}
 }
 
-// removes and closes all connections from the pool
-func (pool *hostConnPool) drain() {
-	pool.mu.Lock()
-	defer pool.mu.Unlock()
-
+func (pool *hostConnPool) drainLocked() {
 	// empty the pool
 	conns := pool.conns
-	pool.conns = pool.conns[:0:0]
+	pool.conns = nil
 
 	// update the policy
 	pool.policy.SetConns(nil)
@@ -586,3 +581,10 @@ func (pool *hostConnPool) drain() {
 		conn.Close()
 	}
 }
+
+// removes and closes all connections from the pool
+func (pool *hostConnPool) drain() {
+	pool.mu.Lock()
+	defer pool.mu.Unlock()
+	pool.drainLocked()
+}