From 00294fd50ba422b4add132059d39a26a34c6d543 Mon Sep 17 00:00:00 2001 From: Andy Pan Date: Sun, 14 Apr 2019 11:05:01 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=98=8F=20Optimization=20of=20the=20struct?= =?UTF-8?q?ure=20and=20style?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ants.go | 13 ++++++++----- ants_test.go | 10 +++++----- pool.go | 10 +++++----- pool_func.go | 10 +++++----- worker.go | 2 +- worker_func.go | 2 +- 6 files changed, 25 insertions(+), 22 deletions(-) diff --git a/ants.go b/ants.go index d840e8d..df1f88b 100644 --- a/ants.go +++ b/ants.go @@ -29,11 +29,14 @@ import ( ) const ( - // DefaultAntsPoolSize is the default capacity for a default goroutine pool. - DefaultAntsPoolSize = math.MaxInt32 + // DEFAULT_ANTS_POOL_SIZE is the default capacity for a default goroutine pool. + DEFAULT_ANTS_POOL_SIZE = math.MaxInt32 - // DefaultCleanIntervalTime is the interval time to clean up goroutines. - DefaultCleanIntervalTime = 1 + // DEFAULT_CLEAN_INTERVAL_TIME is the interval time to clean up goroutines. + DEFAULT_CLEAN_INTERVAL_TIME = 1 + + // CLOSED represents that the pool is closed. + CLOSED = 1 ) var ( @@ -65,7 +68,7 @@ var ( return 1 }() - defaultAntsPool, _ = NewPool(DefaultAntsPoolSize) + defaultAntsPool, _ = NewPool(DEFAULT_ANTS_POOL_SIZE) ) // Init a instance pool when importing ants. diff --git a/ants_test.go b/ants_test.go index f8ac1bb..9d094d8 100644 --- a/ants_test.go +++ b/ants_test.go @@ -103,7 +103,7 @@ func TestAntsPoolGetWorkerFromCache(t *testing.T) { for i := 0; i < AntsSize; i++ { p.Submit(demoFunc) } - time.Sleep(2 * ants.DefaultCleanIntervalTime * time.Second) + time.Sleep(2 * ants.DEFAULT_CLEAN_INTERVAL_TIME * time.Second) p.Submit(demoFunc) t.Logf("pool, running workers number:%d", p.Running()) mem := runtime.MemStats{} @@ -121,7 +121,7 @@ func TestAntsPoolWithFuncGetWorkerFromCache(t *testing.T) { for i := 0; i < AntsSize; i++ { p.Invoke(dur) } - time.Sleep(2 * ants.DefaultCleanIntervalTime * time.Second) + time.Sleep(2 * ants.DEFAULT_CLEAN_INTERVAL_TIME * time.Second) p.Invoke(dur) t.Logf("pool with func, running workers number:%d", p.Running()) mem := runtime.MemStats{} @@ -251,7 +251,7 @@ func TestPurge(t *testing.T) { t.Fatalf("create TimingPool failed: %s", err.Error()) } p.Submit(demoFunc) - time.Sleep(3 * ants.DefaultCleanIntervalTime * time.Second) + time.Sleep(3 * ants.DEFAULT_CLEAN_INTERVAL_TIME * time.Second) if p.Running() != 0 { t.Error("all p should be purged") } @@ -261,7 +261,7 @@ func TestPurge(t *testing.T) { t.Fatalf("create TimingPoolWithFunc failed: %s", err.Error()) } p1.Invoke(1) - time.Sleep(3 * ants.DefaultCleanIntervalTime * time.Second) + time.Sleep(3 * ants.DEFAULT_CLEAN_INTERVAL_TIME * time.Second) if p.Running() != 0 { t.Error("all p should be purged") } @@ -296,7 +296,7 @@ func TestRestCodeCoverage(t *testing.T) { for i := 0; i < n; i++ { p.Invoke(Param) } - time.Sleep(ants.DefaultCleanIntervalTime * time.Second) + time.Sleep(ants.DEFAULT_CLEAN_INTERVAL_TIME * time.Second) t.Logf("pool with func, capacity:%d", p.Cap()) t.Logf("pool with func, running workers number:%d", p.Running()) t.Logf("pool with func, free workers number:%d", p.Free()) diff --git a/pool.go b/pool.go index cf951d2..dcd71b4 100644 --- a/pool.go +++ b/pool.go @@ -63,7 +63,7 @@ type Pool struct { PanicHandler func(interface{}) } -// clear expired workers periodically. +// Clear expired workers periodically. func (p *Pool) periodicallyPurge() { heartbeat := time.NewTicker(p.expiryDuration) defer heartbeat.Stop() @@ -72,7 +72,7 @@ func (p *Pool) periodicallyPurge() { currentTime := time.Now() p.lock.Lock() idleWorkers := p.workers - if atomic.LoadInt32(&p.release) == 1 { + if CLOSED == atomic.LoadInt32(&p.release) { p.lock.Unlock() return } @@ -98,7 +98,7 @@ func (p *Pool) periodicallyPurge() { // NewPool generates an instance of ants pool. func NewPool(size int) (*Pool, error) { - return NewTimingPool(size, DefaultCleanIntervalTime) + return NewTimingPool(size, DEFAULT_CLEAN_INTERVAL_TIME) } // NewTimingPool generates an instance of ants pool with a custom timed task. @@ -122,7 +122,7 @@ func NewTimingPool(size, expiry int) (*Pool, error) { // Submit submits a task to this pool. func (p *Pool) Submit(task func()) error { - if 1 == atomic.LoadInt32(&p.release) { + if CLOSED == atomic.LoadInt32(&p.release) { return ErrPoolClosed } p.retrieveWorker().task <- task @@ -226,7 +226,7 @@ func (p *Pool) retrieveWorker() *Worker { // revertWorker puts a worker back into free pool, recycling the goroutines. func (p *Pool) revertWorker(worker *Worker) bool { - if 1 == atomic.LoadInt32(&p.release) { + if CLOSED == atomic.LoadInt32(&p.release) { return false } worker.recycleTime = time.Now() diff --git a/pool_func.go b/pool_func.go index 7d3d799..623c60f 100644 --- a/pool_func.go +++ b/pool_func.go @@ -66,7 +66,7 @@ type PoolWithFunc struct { PanicHandler func(interface{}) } -// clear expired workers periodically. +// Clear expired workers periodically. func (p *PoolWithFunc) periodicallyPurge() { heartbeat := time.NewTicker(p.expiryDuration) defer heartbeat.Stop() @@ -75,7 +75,7 @@ func (p *PoolWithFunc) periodicallyPurge() { currentTime := time.Now() p.lock.Lock() idleWorkers := p.workers - if atomic.LoadInt32(&p.release) == 1 { + if CLOSED == atomic.LoadInt32(&p.release) { p.lock.Unlock() return } @@ -101,7 +101,7 @@ func (p *PoolWithFunc) periodicallyPurge() { // NewPoolWithFunc generates an instance of ants pool with a specific function. func NewPoolWithFunc(size int, pf func(interface{})) (*PoolWithFunc, error) { - return NewTimingPoolWithFunc(size, DefaultCleanIntervalTime, pf) + return NewTimingPoolWithFunc(size, DEFAULT_CLEAN_INTERVAL_TIME, pf) } // NewTimingPoolWithFunc generates an instance of ants pool with a specific function and a custom timed task. @@ -126,7 +126,7 @@ func NewTimingPoolWithFunc(size, expiry int, pf func(interface{})) (*PoolWithFun // Invoke submits a task to pool. func (p *PoolWithFunc) Invoke(args interface{}) error { - if 1 == atomic.LoadInt32(&p.release) { + if CLOSED == atomic.LoadInt32(&p.release) { return ErrPoolClosed } p.retrieveWorker().args <- args @@ -230,7 +230,7 @@ func (p *PoolWithFunc) retrieveWorker() *WorkerWithFunc { // revertWorker puts a worker back into free pool, recycling the goroutines. func (p *PoolWithFunc) revertWorker(worker *WorkerWithFunc) bool { - if 1 == atomic.LoadInt32(&p.release) { + if CLOSED == atomic.LoadInt32(&p.release) { return false } worker.recycleTime = time.Now() diff --git a/worker.go b/worker.go index e428d75..1e0de31 100644 --- a/worker.go +++ b/worker.go @@ -59,7 +59,7 @@ func (w *Worker) run() { }() for f := range w.task { - if f == nil { + if nil == f { w.pool.decRunning() w.pool.workerCache.Put(w) return diff --git a/worker_func.go b/worker_func.go index 53d183e..9b716ca 100644 --- a/worker_func.go +++ b/worker_func.go @@ -59,7 +59,7 @@ func (w *WorkerWithFunc) run() { }() for args := range w.args { - if args == nil { + if nil == args { w.pool.decRunning() w.pool.workerCache.Put(w) return