From d31b2413c66828ca1806bed13543fe12a1df5345 Mon Sep 17 00:00:00 2001 From: Andy Pan Date: Mon, 16 Jul 2018 02:33:43 +0800 Subject: [PATCH] update --- pool.go | 25 +++++++++++-------------- pool_func.go | 25 +++++++++++-------------- 2 files changed, 22 insertions(+), 28 deletions(-) diff --git a/pool.go b/pool.go index 0207c31..4f8dbae 100644 --- a/pool.go +++ b/pool.go @@ -68,7 +68,8 @@ func (p *Pool) monitorAndClear() { currentTime := time.Now() p.lock.Lock() idleWorkers := p.workers - if len(idleWorkers) == 0 && len(p.release) > 0 { + if len(idleWorkers) == 0 && p.Running() == 0 && len(p.release) > 0 { + p.lock.Unlock() return } n := 0 @@ -142,21 +143,17 @@ func (p *Pool) Cap() int { // ReSize change the capacity of this pool func (p *Pool) ReSize(size int) { - if size < p.Cap() { - diff := p.Cap() - size - p.lock.Lock() - idleWorkers := p.workers - for i := 0; i < diff; i++ { - <-p.freeSignal - idleWorkers[i].task <- nil - idleWorkers[i] = nil - } - p.workers = idleWorkers[diff:] - p.lock.Unlock() - } else if size == p.Cap() { + if size == p.Cap() { return + } else if size < p.Cap() { + diff := p.Cap() - size + atomic.StoreInt32(&p.capacity, int32(size)) + for i := 0; i < diff; i++ { + p.getWorker().task <- nil + } + } else { + atomic.StoreInt32(&p.capacity, int32(size)) } - atomic.StoreInt32(&p.capacity, int32(size)) } // Release Closed this pool diff --git a/pool_func.go b/pool_func.go index 9ca3bfc..6099868 100644 --- a/pool_func.go +++ b/pool_func.go @@ -69,7 +69,8 @@ func (p *PoolWithFunc) monitorAndClear() { currentTime := time.Now() p.lock.Lock() idleWorkers := p.workers - if len(idleWorkers) == 0 && len(p.release) > 0 { + if len(idleWorkers) == 0 && p.Running() == 0 && len(p.release) > 0 { + p.lock.Unlock() return } n := 0 @@ -147,21 +148,17 @@ func (p *PoolWithFunc) Cap() int { // ReSize change the capacity of this pool func (p *PoolWithFunc) ReSize(size int) { - if size < p.Cap() { - diff := p.Cap() - size - p.lock.Lock() - idleWorkers := p.workers - for i := 0; i < diff; i++ { - <-p.freeSignal - idleWorkers[i].args <- nil - idleWorkers[i] = nil - } - p.workers = idleWorkers[diff:] - p.lock.Unlock() - } else if size == p.Cap() { + if size == p.Cap() { return + } else if size < p.Cap() { + diff := p.Cap() - size + atomic.StoreInt32(&p.capacity, int32(size)) + for i := 0; i < diff; i++ { + p.getWorker().args <- nil + } + } else { + atomic.StoreInt32(&p.capacity, int32(size)) } - atomic.StoreInt32(&p.capacity, int32(size)) } // Release Closed this pool