From 3afa151dd98ee02d085278113e4c2936a811ec89 Mon Sep 17 00:00:00 2001 From: Andy Pan Date: Sun, 15 Jul 2018 00:52:18 +0800 Subject: [PATCH 1/3] simplify and optimize goroutine-worker --- pool.go | 8 ++++---- worker.go | 10 ---------- 2 files changed, 4 insertions(+), 14 deletions(-) diff --git a/pool.go b/pool.go index d5069ef..e7836e5 100644 --- a/pool.go +++ b/pool.go @@ -74,7 +74,7 @@ func (p *Pool) monitorAndClear() { break } n = i - w.stop() + w.task <- nil idleWorkers[i] = nil p.running-- } @@ -118,7 +118,7 @@ func (p *Pool) Submit(task f) error { return ErrPoolClosed } w := p.getWorker() - w.sendTask(task) + w.task <- task return nil } @@ -143,7 +143,7 @@ func (p *Pool) Release() error { p.release <- sig{} running := p.Running() for i := 0; i < running; i++ { - p.getWorker().stop() + p.getWorker().task <- nil } for i := range p.workers { p.workers[i] = nil @@ -157,7 +157,7 @@ func (p *Pool) ReSize(size int) { if size < p.Cap() { diff := p.Cap() - size for i := 0; i < diff; i++ { - p.getWorker().stop() + p.getWorker().task <- nil } } else if size == p.Cap() { return diff --git a/worker.go b/worker.go index babb8af..9031f57 100644 --- a/worker.go +++ b/worker.go @@ -56,13 +56,3 @@ func (w *Worker) run() { } }() } - -// stop this worker. -func (w *Worker) stop() { - w.sendTask(nil) -} - -// sendTask sends a task to this worker. -func (w *Worker) sendTask(task f) { - w.task <- task -} From 813cc218fec1c13abb9982abd13af4190e5e2a88 Mon Sep 17 00:00:00 2001 From: Andy Pan Date: Sun, 15 Jul 2018 00:52:27 +0800 Subject: [PATCH 2/3] simplify and optimize goroutine-worker --- pool_func.go | 8 ++++---- worker_func.go | 10 ---------- 2 files changed, 4 insertions(+), 14 deletions(-) diff --git a/pool_func.go b/pool_func.go index b2ef61e..407651a 100644 --- a/pool_func.go +++ b/pool_func.go @@ -75,7 +75,7 @@ func (p *PoolWithFunc) monitorAndClear() { break } n = i - w.stop() + w.args <- nil idleWorkers[i] = nil p.running-- } @@ -123,7 +123,7 @@ func (p *PoolWithFunc) Serve(args interface{}) error { return ErrPoolClosed } w := p.getWorker() - w.sendTask(args) + w.args <- args return nil } @@ -148,7 +148,7 @@ func (p *PoolWithFunc) Release() error { p.release <- sig{} running := p.Running() for i := 0; i < running; i++ { - p.getWorker().stop() + p.getWorker().args <- nil } for i := range p.workers { p.workers[i] = nil @@ -162,7 +162,7 @@ func (p *PoolWithFunc) ReSize(size int) { if size < p.Cap() { diff := p.Cap() - size for i := 0; i < diff; i++ { - p.getWorker().stop() + p.getWorker().args <- nil } } else if size == p.Cap() { return diff --git a/worker_func.go b/worker_func.go index 6467bac..a3b5bb6 100644 --- a/worker_func.go +++ b/worker_func.go @@ -56,13 +56,3 @@ func (w *WorkerWithFunc) run() { } }() } - -// stop this worker. -func (w *WorkerWithFunc) stop() { - w.sendTask(nil) -} - -// sendTask sends a task to this worker. -func (w *WorkerWithFunc) sendTask(args interface{}) { - w.args <- args -} From d77f01cf9a0bdbafc616f5ebbf92ed3db023e634 Mon Sep 17 00:00:00 2001 From: Andy Pan Date: Sun, 15 Jul 2018 00:57:32 +0800 Subject: [PATCH 3/3] optimization for GC --- pool.go | 1 + pool_func.go | 1 + 2 files changed, 2 insertions(+) diff --git a/pool.go b/pool.go index e7836e5..53471e2 100644 --- a/pool.go +++ b/pool.go @@ -148,6 +148,7 @@ func (p *Pool) Release() error { for i := range p.workers { p.workers[i] = nil } + p.workers = nil }) return nil } diff --git a/pool_func.go b/pool_func.go index 407651a..d99269f 100644 --- a/pool_func.go +++ b/pool_func.go @@ -153,6 +153,7 @@ func (p *PoolWithFunc) Release() error { for i := range p.workers { p.workers[i] = nil } + p.workers = nil }) return nil }