From e99d428a5f5775e41936e4335fa5346f195a8d90 Mon Sep 17 00:00:00 2001 From: andy pan Date: Wed, 8 Aug 2018 18:08:06 +0800 Subject: [PATCH] fixed #7 --- pool.go | 7 ++++++- pool_func.go | 6 +++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/pool.go b/pool.go index 5a13182..c3f5568 100644 --- a/pool.go +++ b/pool.go @@ -55,6 +55,7 @@ type Pool struct { once sync.Once } + // clear expired workers periodically. func (p *Pool) periodicallyPurge() { heartbeat := time.NewTicker(p.expiryDuration) @@ -77,7 +78,11 @@ func (p *Pool) periodicallyPurge() { } n++ if n > 0 { - p.workers = idleWorkers[n:] + if n >= cap(idleWorkers) { + p.workers = idleWorkers[:0] + } else { + p.workers = idleWorkers[n:] + } } p.lock.Unlock() } diff --git a/pool_func.go b/pool_func.go index ee23b9e..3e6dc5e 100644 --- a/pool_func.go +++ b/pool_func.go @@ -79,7 +79,11 @@ func (p *PoolWithFunc) periodicallyPurge() { } n++ if n > 0 { - p.workers = idleWorkers[n:] + if n >= cap(idleWorkers) { + p.workers = idleWorkers[:0] + } else { + p.workers = idleWorkers[n:] + } } p.lock.Unlock() }