mirror of https://github.com/panjf2000/ants.git
🐝 Performance improvement: use sync.Pool to cache active workers
This commit is contained in:
parent
29f47ced64
commit
30bf38e6a8
|
@ -23,6 +23,7 @@
|
||||||
package ants
|
package ants
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"log"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -43,6 +44,7 @@ type Worker struct {
|
||||||
// run starts a goroutine to repeat the process
|
// run starts a goroutine to repeat the process
|
||||||
// that performs the function calls.
|
// that performs the function calls.
|
||||||
func (w *Worker) run() {
|
func (w *Worker) run() {
|
||||||
|
w.pool.incRunning()
|
||||||
go func() {
|
go func() {
|
||||||
defer func() {
|
defer func() {
|
||||||
if p := recover(); p != nil {
|
if p := recover(); p != nil {
|
||||||
|
@ -50,7 +52,7 @@ func (w *Worker) run() {
|
||||||
if w.pool.PanicHandler != nil {
|
if w.pool.PanicHandler != nil {
|
||||||
w.pool.PanicHandler(p)
|
w.pool.PanicHandler(p)
|
||||||
} else {
|
} else {
|
||||||
panic(p)
|
log.Printf("worker exits from a panic: %v", p)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
@ -58,11 +60,11 @@ func (w *Worker) run() {
|
||||||
for f := range w.task {
|
for f := range w.task {
|
||||||
if f == nil {
|
if f == nil {
|
||||||
w.pool.decRunning()
|
w.pool.decRunning()
|
||||||
w.pool.cachePool.Put(w)
|
w.pool.workerCache.Put(w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
f()
|
f()
|
||||||
w.pool.putWorker(w)
|
w.pool.revertWorker(w)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,6 +23,7 @@
|
||||||
package ants
|
package ants
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"log"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -43,6 +44,7 @@ type WorkerWithFunc struct {
|
||||||
// run starts a goroutine to repeat the process
|
// run starts a goroutine to repeat the process
|
||||||
// that performs the function calls.
|
// that performs the function calls.
|
||||||
func (w *WorkerWithFunc) run() {
|
func (w *WorkerWithFunc) run() {
|
||||||
|
w.pool.incRunning()
|
||||||
go func() {
|
go func() {
|
||||||
defer func() {
|
defer func() {
|
||||||
if p := recover(); p != nil {
|
if p := recover(); p != nil {
|
||||||
|
@ -50,7 +52,7 @@ func (w *WorkerWithFunc) run() {
|
||||||
if w.pool.PanicHandler != nil {
|
if w.pool.PanicHandler != nil {
|
||||||
w.pool.PanicHandler(p)
|
w.pool.PanicHandler(p)
|
||||||
} else {
|
} else {
|
||||||
panic(p)
|
log.Printf("worker exits from a panic: %v", p)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
@ -58,11 +60,11 @@ func (w *WorkerWithFunc) run() {
|
||||||
for args := range w.args {
|
for args := range w.args {
|
||||||
if args == nil {
|
if args == nil {
|
||||||
w.pool.decRunning()
|
w.pool.decRunning()
|
||||||
w.pool.cachePool.Put(w)
|
w.pool.workerCache.Put(w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
w.pool.poolFunc(args)
|
w.pool.poolFunc(args)
|
||||||
w.pool.putWorker(w)
|
w.pool.revertWorker(w)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue