From 18b4e51bb5b7c315d60919287de88f8556379ba3 Mon Sep 17 00:00:00 2001 From: Armon Dadgar Date: Wed, 5 Feb 2014 14:20:18 -0800 Subject: [PATCH] consul: ensure conn pool shutdown is fast --- consul/pool.go | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/consul/pool.go b/consul/pool.go index c4e96c48e1..4d13d34ead 100644 --- a/consul/pool.go +++ b/consul/pool.go @@ -39,7 +39,8 @@ type ConnPool struct { pool map[string][]*Conn // Used to indicate the pool is shutdown - shutdown bool + shutdown bool + shutdownCh chan struct{} } // NewPool is used to make a new connection pool @@ -47,9 +48,10 @@ type ConnPool struct { // Set maxTime to 0 to disable reaping. func NewPool(maxConns int, maxTime time.Duration) *ConnPool { pool := &ConnPool{ - maxConns: maxConns, - maxTime: maxTime, - pool: make(map[string][]*Conn), + maxConns: maxConns, + maxTime: maxTime, + pool: make(map[string][]*Conn), + shutdownCh: make(chan struct{}), } if maxTime > 0 { go pool.reap() @@ -68,8 +70,12 @@ func (p *ConnPool) Shutdown() error { } } p.pool = make(map[string][]*Conn) - p.shutdown = true + if p.shutdown { + return nil + } + p.shutdown = true + close(p.shutdownCh) return nil } @@ -187,7 +193,11 @@ func (p *ConnPool) RPC(addr net.Addr, method string, args interface{}, reply int func (p *ConnPool) reap() { for !p.shutdown { // Sleep for a while - time.Sleep(time.Second) + select { + case <-time.After(time.Second): + case <-p.shutdownCh: + return + } // Reap all old conns p.Lock()