From 8cbfb5dd672c68c9cf852ee8350027d6f48017fe Mon Sep 17 00:00:00 2001 From: Andy Pan Date: Mon, 14 Oct 2019 02:50:13 +0800 Subject: [PATCH] Optimize the logic when putting worker back to sync.Pool --- worker.go | 2 +- worker_func.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/worker.go b/worker.go index 0eecd2c..1b1f817 100644 --- a/worker.go +++ b/worker.go @@ -49,6 +49,7 @@ func (w *goWorker) run() { go func() { defer func() { w.pool.decRunning() + w.pool.workerCache.Put(w) if p := recover(); p != nil { if ph := w.pool.options.PanicHandler; ph != nil { ph(p) @@ -59,7 +60,6 @@ func (w *goWorker) run() { log.Printf("worker exits from panic: %s\n", string(buf[:n])) } } - w.pool.workerCache.Put(w) }() for f := range w.task { diff --git a/worker_func.go b/worker_func.go index 0d119df..3f46d72 100644 --- a/worker_func.go +++ b/worker_func.go @@ -49,6 +49,7 @@ func (w *goWorkerWithFunc) run() { go func() { defer func() { w.pool.decRunning() + w.pool.workerCache.Put(w) if p := recover(); p != nil { if ph := w.pool.options.PanicHandler; ph != nil { ph(p) @@ -59,7 +60,6 @@ func (w *goWorkerWithFunc) run() { log.Printf("worker with func exits from panic: %s\n", string(buf[:n])) } } - w.pool.workerCache.Put(w) }() for args := range w.args {