mirror of https://github.com/hashicorp/consul
consul: ensure conn pool shutdown is fast
parent
e173e7eeff
commit
18b4e51bb5
|
@ -40,6 +40,7 @@ type ConnPool struct {
|
||||||
|
|
||||||
// Used to indicate the pool is shutdown
|
// Used to indicate the pool is shutdown
|
||||||
shutdown bool
|
shutdown bool
|
||||||
|
shutdownCh chan struct{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewPool is used to make a new connection pool
|
// NewPool is used to make a new connection pool
|
||||||
|
@ -50,6 +51,7 @@ func NewPool(maxConns int, maxTime time.Duration) *ConnPool {
|
||||||
maxConns: maxConns,
|
maxConns: maxConns,
|
||||||
maxTime: maxTime,
|
maxTime: maxTime,
|
||||||
pool: make(map[string][]*Conn),
|
pool: make(map[string][]*Conn),
|
||||||
|
shutdownCh: make(chan struct{}),
|
||||||
}
|
}
|
||||||
if maxTime > 0 {
|
if maxTime > 0 {
|
||||||
go pool.reap()
|
go pool.reap()
|
||||||
|
@ -68,8 +70,12 @@ func (p *ConnPool) Shutdown() error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
p.pool = make(map[string][]*Conn)
|
p.pool = make(map[string][]*Conn)
|
||||||
p.shutdown = true
|
|
||||||
|
|
||||||
|
if p.shutdown {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
p.shutdown = true
|
||||||
|
close(p.shutdownCh)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -187,7 +193,11 @@ func (p *ConnPool) RPC(addr net.Addr, method string, args interface{}, reply int
|
||||||
func (p *ConnPool) reap() {
|
func (p *ConnPool) reap() {
|
||||||
for !p.shutdown {
|
for !p.shutdown {
|
||||||
// Sleep for a while
|
// Sleep for a while
|
||||||
time.Sleep(time.Second)
|
select {
|
||||||
|
case <-time.After(time.Second):
|
||||||
|
case <-p.shutdownCh:
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Reap all old conns
|
// Reap all old conns
|
||||||
p.Lock()
|
p.Lock()
|
||||||
|
|
Loading…
Reference in New Issue