From d3bcc5d10bdff394e210414579d1ce805acc2eba Mon Sep 17 00:00:00 2001 From: Andy Pan Date: Sat, 26 Jan 2019 12:26:26 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=B3=20Use=20the=20nil=20of=20chan=20to?= =?UTF-8?q?=20indicate=20the=20pool=20closed=20or=20not?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pool.go | 9 ++++----- pool_func.go | 9 ++++----- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/pool.go b/pool.go index b1599e9..05b8b43 100644 --- a/pool.go +++ b/pool.go @@ -72,7 +72,7 @@ func (p *Pool) periodicallyPurge() { currentTime := time.Now() p.lock.Lock() idleWorkers := p.workers - if len(idleWorkers) == 0 && p.Running() == 0 && len(p.release) > 0 { + if len(idleWorkers) == 0 && p.Running() == 0 && p.release != nil { p.lock.Unlock() return } @@ -111,7 +111,6 @@ func NewTimingPool(size, expiry int) (*Pool, error) { } p := &Pool{ capacity: int32(size), - release: make(chan sig, 1), expiryDuration: time.Duration(expiry) * time.Second, } p.cond = sync.NewCond(&p.lock) @@ -123,7 +122,7 @@ func NewTimingPool(size, expiry int) (*Pool, error) { // Submit submits a task to this pool. func (p *Pool) Submit(task f) error { - if len(p.release) > 0 { + if p.release == nil { return ErrPoolClosed } p.getWorker().task <- task @@ -160,7 +159,7 @@ func (p *Pool) ReSize(size int) { // Release Closes this pool. func (p *Pool) Release() error { p.once.Do(func() { - p.release <- sig{} + p.release = make(chan sig) p.lock.Lock() idleWorkers := p.workers for i, w := range idleWorkers { @@ -233,4 +232,4 @@ func (p *Pool) putWorker(worker *Worker) { // Notify the invoker stuck in 'getWorker()' of there is an available worker in the worker queue. p.cond.Signal() p.lock.Unlock() -} \ No newline at end of file +} diff --git a/pool_func.go b/pool_func.go index e026f50..53b085b 100644 --- a/pool_func.go +++ b/pool_func.go @@ -73,7 +73,7 @@ func (p *PoolWithFunc) periodicallyPurge() { currentTime := time.Now() p.lock.Lock() idleWorkers := p.workers - if len(idleWorkers) == 0 && p.Running() == 0 && len(p.release) > 0 { + if len(idleWorkers) == 0 && p.Running() == 0 && p.release != nil { p.lock.Unlock() return } @@ -112,7 +112,6 @@ func NewTimingPoolWithFunc(size, expiry int, f pf) (*PoolWithFunc, error) { } p := &PoolWithFunc{ capacity: int32(size), - release: make(chan sig, 1), expiryDuration: time.Duration(expiry) * time.Second, poolFunc: f, } @@ -125,7 +124,7 @@ func NewTimingPoolWithFunc(size, expiry int, f pf) (*PoolWithFunc, error) { // Serve submits a task to pool. func (p *PoolWithFunc) Serve(args interface{}) error { - if len(p.release) > 0 { + if p.release != nil { return ErrPoolClosed } p.getWorker().args <- args @@ -162,7 +161,7 @@ func (p *PoolWithFunc) ReSize(size int) { // Release Closed this pool. func (p *PoolWithFunc) Release() error { p.once.Do(func() { - p.release <- sig{} + p.release = make(chan sig) p.lock.Lock() idleWorkers := p.workers for i, w := range idleWorkers { @@ -236,4 +235,4 @@ func (p *PoolWithFunc) putWorker(worker *WorkerWithFunc) { // Notify the invoker stuck in 'getWorker()' of there is an available worker in the worker queue. p.cond.Signal() p.lock.Unlock() -} \ No newline at end of file +}