Browse Source

Use sync.WaitGroup rather than explicit semaphore in fillPool()

Miles Delahunty 10 years ago
parent
commit
9c3f7ccdec
1 changed files with 4 additions and 8 deletions
  1. 4 8
      connectionpool.go

+ 4 - 8
connectionpool.go

@@ -227,8 +227,7 @@ func (c *SimplePool) fillPool() {
 	c.hostMu.RLock()
 
 	//Walk through list of defined hosts
-	cHostConnected := make(chan int, len(c.hosts))
-	hostsPending := 0
+	var wg sync.WaitGroup
 	for host := range c.hosts {
 		addr := JoinHostPort(host, c.cfg.Port)
 
@@ -253,9 +252,9 @@ func (c *SimplePool) fillPool() {
 
 		//This is reached if the host is responsive and needs more connections
 		//Create connections for host synchronously to mitigate flooding the host.
-		hostsPending++
+		wg.Add(1)
 		go func(a string, conns int) {
-			defer func() { cHostConnected <- 1 }()
+			defer func() { wg.Done() }()
 			for ; conns < c.cfg.NumConns; conns++ {
 				c.connect(a)
 			}
@@ -265,10 +264,7 @@ func (c *SimplePool) fillPool() {
 	c.hostMu.RUnlock()
 
 	//Wait until we're finished connecting to each host before returning
-	for hostsPending > 0 {
-		<- cHostConnected
-		hostsPending--
-	}
+	wg.Wait()
 }
 
 // Should only be called if c.mu is locked