Optimize the logic when putting worker back to sync.Pool

This commit is contained in:
Andy Pan 2019-10-14 02:50:13 +08:00
parent 023672e877
commit 8cbfb5dd67
2 changed files with 2 additions and 2 deletions

View File

@ -49,6 +49,7 @@ func (w *goWorker) run() {
go func() { go func() {
defer func() { defer func() {
w.pool.decRunning() w.pool.decRunning()
w.pool.workerCache.Put(w)
if p := recover(); p != nil { if p := recover(); p != nil {
if ph := w.pool.options.PanicHandler; ph != nil { if ph := w.pool.options.PanicHandler; ph != nil {
ph(p) ph(p)
@ -59,7 +60,6 @@ func (w *goWorker) run() {
log.Printf("worker exits from panic: %s\n", string(buf[:n])) log.Printf("worker exits from panic: %s\n", string(buf[:n]))
} }
} }
w.pool.workerCache.Put(w)
}() }()
for f := range w.task { for f := range w.task {

View File

@ -49,6 +49,7 @@ func (w *goWorkerWithFunc) run() {
go func() { go func() {
defer func() { defer func() {
w.pool.decRunning() w.pool.decRunning()
w.pool.workerCache.Put(w)
if p := recover(); p != nil { if p := recover(); p != nil {
if ph := w.pool.options.PanicHandler; ph != nil { if ph := w.pool.options.PanicHandler; ph != nil {
ph(p) ph(p)
@ -59,7 +60,6 @@ func (w *goWorkerWithFunc) run() {
log.Printf("worker with func exits from panic: %s\n", string(buf[:n])) log.Printf("worker with func exits from panic: %s\n", string(buf[:n]))
} }
} }
w.pool.workerCache.Put(w)
}() }()
for args := range w.args { for args := range w.args {