Browse Source

Merge pull request #332 from brandscreen/master

Wait until we're done connecting before returning from fillPool
Ben Hood 10 years ago
parent
commit
af32d99e1d
2 changed files with 7 additions and 0 deletions
  1. 1 0
      AUTHORS
  2. 6 0
      connectionpool.go

+ 1 - 0
AUTHORS

@@ -44,3 +44,4 @@ Ashwin Purohit <purohit@gmail.com>
 Dan Kinder <dkinder.is.me@gmail.com>
 Oliver Beattie <oliver@obeattie.com>
 Justin Corpron <justin@retailnext.com>
+Miles Delahunty <miles.delahunty@gmail.com>

+ 6 - 0
connectionpool.go

@@ -227,6 +227,7 @@ func (c *SimplePool) fillPool() {
 	c.hostMu.RLock()
 
 	//Walk through list of defined hosts
+	var wg sync.WaitGroup
 	for host := range c.hosts {
 		addr := JoinHostPort(host, c.cfg.Port)
 
@@ -251,7 +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.
+		wg.Add(1)
 		go func(a string, conns int) {
+			defer wg.Done()
 			for ; conns < c.cfg.NumConns; conns++ {
 				c.connect(a)
 			}
@@ -259,6 +262,9 @@ func (c *SimplePool) fillPool() {
 	}
 
 	c.hostMu.RUnlock()
+
+	//Wait until we're finished connecting to each host before returning
+	wg.Wait()
 }
 
 // Should only be called if c.mu is locked