fixed workers leak

This commit is contained in:
andy pan 2018-08-29 09:53:07 +08:00
parent 50918eb4a9
commit 8556a67f0f
2 changed files with 6 additions and 8 deletions

View File

@ -67,7 +67,7 @@ func (p *Pool) periodicallyPurge() {
p.lock.Unlock() p.lock.Unlock()
return return
} }
n := 0 n := -1
for i, w := range idleWorkers { for i, w := range idleWorkers {
if currentTime.Sub(w.recycleTime) <= p.expiryDuration { if currentTime.Sub(w.recycleTime) <= p.expiryDuration {
break break
@ -76,12 +76,11 @@ func (p *Pool) periodicallyPurge() {
w.task <- nil w.task <- nil
idleWorkers[i] = nil idleWorkers[i] = nil
} }
n++ if n > -1 {
if n > 0 {
if n >= len(idleWorkers) { if n >= len(idleWorkers) {
p.workers = idleWorkers[:0] p.workers = idleWorkers[:0]
} else { } else {
p.workers = idleWorkers[n:] p.workers = idleWorkers[n+1:]
} }
} }
p.lock.Unlock() p.lock.Unlock()

View File

@ -68,7 +68,7 @@ func (p *PoolWithFunc) periodicallyPurge() {
p.lock.Unlock() p.lock.Unlock()
return return
} }
n := 0 n := -1
for i, w := range idleWorkers { for i, w := range idleWorkers {
if currentTime.Sub(w.recycleTime) <= p.expiryDuration { if currentTime.Sub(w.recycleTime) <= p.expiryDuration {
break break
@ -77,12 +77,11 @@ func (p *PoolWithFunc) periodicallyPurge() {
w.args <- nil w.args <- nil
idleWorkers[i] = nil idleWorkers[i] = nil
} }
n++ if n > -1 {
if n > 0 {
if n >= len(idleWorkers) { if n >= len(idleWorkers) {
p.workers = idleWorkers[:0] p.workers = idleWorkers[:0]
} else { } else {
p.workers = idleWorkers[n:] p.workers = idleWorkers[n+1:]
} }
} }
p.lock.Unlock() p.lock.Unlock()