forked from mirror/ants
🐲 An optimization in worker channel
This commit is contained in:
parent
c829c6f622
commit
b091435432
32
ants.go
32
ants.go
|
@ -25,6 +25,7 @@ package ants
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"math"
|
"math"
|
||||||
|
"runtime"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -35,6 +36,30 @@ const (
|
||||||
DefaultCleanIntervalTime = 5
|
DefaultCleanIntervalTime = 5
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
// Error types for the Ants API.
|
||||||
|
ErrInvalidPoolSize = errors.New("invalid size for pool")
|
||||||
|
ErrInvalidPoolExpiry = errors.New("invalid expiry for pool")
|
||||||
|
ErrPoolClosed = errors.New("this pool has been closed")
|
||||||
|
|
||||||
|
// workerChanCap determines whether the channel of a worker should be a buffered channel
|
||||||
|
// to get the best performance, inspired by fasthttp.
|
||||||
|
// https://github.com/valyala/fasthttp/blob/master/workerpool.go#L139
|
||||||
|
workerChanCap = func() int {
|
||||||
|
// Use blocking workerChan if GOMAXPROCS=1.
|
||||||
|
// This immediately switches Serve to WorkerFunc, which results
|
||||||
|
// in higher performance (under go1.5 at least).
|
||||||
|
if runtime.GOMAXPROCS(0) == 1 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use non-blocking workerChan if GOMAXPROCS>1,
|
||||||
|
// since otherwise the Serve caller (Acceptor) may lag accepting
|
||||||
|
// new connections if WorkerFunc is CPU-bound.
|
||||||
|
return 1
|
||||||
|
}()
|
||||||
|
)
|
||||||
|
|
||||||
// Init a instance pool when importing ants.
|
// Init a instance pool when importing ants.
|
||||||
var defaultAntsPool, _ = NewPool(DefaultAntsPoolSize)
|
var defaultAntsPool, _ = NewPool(DefaultAntsPoolSize)
|
||||||
|
|
||||||
|
@ -62,10 +87,3 @@ func Free() int {
|
||||||
func Release() {
|
func Release() {
|
||||||
defaultAntsPool.Release()
|
defaultAntsPool.Release()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Error types for the Ants API.
|
|
||||||
var (
|
|
||||||
ErrInvalidPoolSize = errors.New("invalid size for pool")
|
|
||||||
ErrInvalidPoolExpiry = errors.New("invalid expiry for pool")
|
|
||||||
ErrPoolClosed = errors.New("this pool has been closed")
|
|
||||||
)
|
|
||||||
|
|
4
pool.go
4
pool.go
|
@ -28,8 +28,6 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type sig struct{}
|
|
||||||
|
|
||||||
type f func()
|
type f func()
|
||||||
|
|
||||||
// Pool accept the tasks from client,it limits the total
|
// Pool accept the tasks from client,it limits the total
|
||||||
|
@ -222,7 +220,7 @@ func (p *Pool) getWorker() *Worker {
|
||||||
} else if w == nil {
|
} else if w == nil {
|
||||||
w = &Worker{
|
w = &Worker{
|
||||||
pool: p,
|
pool: p,
|
||||||
task: make(chan f, 1),
|
task: make(chan f, workerChanCap),
|
||||||
}
|
}
|
||||||
w.run()
|
w.run()
|
||||||
p.incRunning()
|
p.incRunning()
|
||||||
|
|
|
@ -224,7 +224,7 @@ func (p *PoolWithFunc) getWorker() *WorkerWithFunc {
|
||||||
} else if w == nil {
|
} else if w == nil {
|
||||||
w = &WorkerWithFunc{
|
w = &WorkerWithFunc{
|
||||||
pool: p,
|
pool: p,
|
||||||
args: make(chan interface{}, 1),
|
args: make(chan interface{}, workerChanCap),
|
||||||
}
|
}
|
||||||
w.run()
|
w.run()
|
||||||
p.incRunning()
|
p.incRunning()
|
||||||
|
|
Loading…
Reference in New Issue