From 1c2b863036b6d32ea4d2a8c12a2b175f62c3a7fd Mon Sep 17 00:00:00 2001 From: Vladimir Mihailenco Date: Thu, 4 Sep 2014 10:22:44 +0300 Subject: [PATCH] rate_limit: break loop when pool is closed. --- pool.go | 6 +++++- rate_limit.go | 3 +++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/pool.go b/pool.go index 7031a45..8a99e3a 100644 --- a/pool.go +++ b/pool.go @@ -116,7 +116,10 @@ func newConnPool(dial func() (*conn, error), opt *options) *connPool { func (p *connPool) new() (*conn, error) { select { - case <-p.rl.C: + case _, ok := <-p.rl.C: + if !ok { + return nil, errClosed + } default: return nil, errRateLimited } @@ -260,6 +263,7 @@ func (p *connPool) Close() error { return nil } p.closed = true + close(p.rl.C) var retErr error for { e := p.conns.Front() diff --git a/rate_limit.go b/rate_limit.go index bd84a24..2534ddc 100644 --- a/rate_limit.go +++ b/rate_limit.go @@ -20,6 +20,9 @@ func newRateLimiter(limit time.Duration, chanSize int) *rateLimiter { } func (rl *rateLimiter) loop(limit time.Duration) { + defer func() { + recover() + }() for { select { case rl.C <- struct{}{}: