From 21a109c7f0873c8f466d6710de23474968940011 Mon Sep 17 00:00:00 2001 From: markjiang Date: Wed, 24 Jul 2019 15:39:18 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=B8=85=E7=90=86=E9=80=9F?= =?UTF-8?q?=E5=BA=A6=E5=B9=B6=E4=BF=AE=E5=A4=8D=E5=86=85=E5=AD=98=E6=B3=84?= =?UTF-8?q?=E6=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pool.go | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/pool.go b/pool.go index 338d94f..3c50b4b 100644 --- a/pool.go +++ b/pool.go @@ -68,6 +68,7 @@ func (p *Pool) periodicallyPurge() { heartbeat := time.NewTicker(p.expiryDuration) defer heartbeat.Stop() + var purgeWorkers []*Worker for range heartbeat.C { if CLOSED == atomic.LoadInt32(&p.release) { break @@ -75,23 +76,29 @@ func (p *Pool) periodicallyPurge() { currentTime := time.Now() p.lock.Lock() idleWorkers := p.workers - n := -1 - for i, w := range idleWorkers { - if currentTime.Sub(w.recycleTime) <= p.expiryDuration { - break - } - n = i - w.task <- nil - idleWorkers[i] = nil + n := len(idleWorkers) + i := 0 + for i < n && currentTime.Sub(idleWorkers[i].recycleTime) > p.expiryDuration { + i++ } - if n > -1 { - if n >= len(idleWorkers)-1 { - p.workers = idleWorkers[:0] - } else { - p.workers = idleWorkers[n+1:] + purgeWorkers = append(purgeWorkers[:0], idleWorkers[:i]...) + if i > 0 { + m := copy(idleWorkers, idleWorkers[i:]) + for i = m; i < n; i++ { + idleWorkers[i] = nil } + p.workers = idleWorkers[:m] } p.lock.Unlock() + + // Notify obsolete workers to stop. + // This notification must be outside the p.lock, since w.task + // may be blocking and may consume a lot of time if many workers + // are located on non-local CPUs. + for i, w := range purgeWorkers { + w.task <- nil + purgeWorkers[i] = nil + } } }