From a59f51fefdb0a7419f334d884f7531dc2519fa14 Mon Sep 17 00:00:00 2001 From: Andy Pan Date: Sun, 20 May 2018 01:38:50 +0800 Subject: [PATCH] update --- ants.go | 4 +--- pool.go | 4 ++-- worker.go | 4 ++-- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/ants.go b/ants.go index 4abef68..ba16dbe 100644 --- a/ants.go +++ b/ants.go @@ -1,8 +1,6 @@ package ants -import "math" - -const DEFAULT_POOL_SIZE = math.MaxInt32 +const DEFAULT_POOL_SIZE = 10000 var defaultPool = NewPool(DEFAULT_POOL_SIZE) diff --git a/pool.go b/pool.go index 1a27abf..297b400 100644 --- a/pool.go +++ b/pool.go @@ -105,18 +105,18 @@ func (p *Pool) newWorker() *Worker { exit: make(chan sig), } worker.run() + atomic.AddInt32(&p.running, 1) return worker } func (p *Pool) getWorker() *Worker { - defer atomic.AddInt32(&p.running, 1) if w := p.workers.pop(); w != nil { return w.(*Worker) } return p.newWorker() } -func (p *Pool) PutWorker(worker *Worker) { +func (p *Pool) putWorker(worker *Worker) { p.workers.push(worker) if p.reachLimit() { p.freeSignal <- sig{} diff --git a/worker.go b/worker.go index 3131f83..21240cc 100644 --- a/worker.go +++ b/worker.go @@ -18,7 +18,7 @@ func (w *Worker) run() { select { case f := <-w.task: f() - w.pool.workers.push(w) + w.pool.putWorker(w) w.pool.wg.Done() case <-w.exit: atomic.AddInt32(&w.pool.running, -1) @@ -58,7 +58,7 @@ func (q *ConcurrentQueue) push(v interface{}) { func (q *ConcurrentQueue) pop() interface{} { defer q.m.Unlock() q.m.Lock() - if elem := q.queue.Back(); elem != nil{ + if elem := q.queue.Back(); elem != nil { return q.queue.Remove(elem) } return nil