mirror of https://github.com/panjf2000/ants.git
update
This commit is contained in:
parent
004caa6636
commit
0453f88168
8
ants.go
8
ants.go
|
@ -25,6 +25,7 @@ package ants
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"math"
|
"math"
|
||||||
|
"runtime"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -68,3 +69,10 @@ var (
|
||||||
ErrPoolSizeInvalid = errors.New("invalid size for pool")
|
ErrPoolSizeInvalid = errors.New("invalid size for pool")
|
||||||
ErrPoolClosed = errors.New("this pool has been closed")
|
ErrPoolClosed = errors.New("this pool has been closed")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var workerArgsCap = func() int {
|
||||||
|
if runtime.GOMAXPROCS(0) == 1 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
return 1
|
||||||
|
}()
|
||||||
|
|
7
pool.go
7
pool.go
|
@ -177,20 +177,19 @@ func (p *Pool) getWorker() *Worker {
|
||||||
if wp == nil {
|
if wp == nil {
|
||||||
w = &Worker{
|
w = &Worker{
|
||||||
pool: p,
|
pool: p,
|
||||||
task: make(chan f),
|
task: make(chan f, workerArgsCap),
|
||||||
}
|
}
|
||||||
w.run()
|
|
||||||
atomic.AddInt32(&p.running, 1)
|
|
||||||
} else {
|
} else {
|
||||||
w = wp.(*Worker)
|
w = wp.(*Worker)
|
||||||
}
|
}
|
||||||
|
w.run()
|
||||||
|
p.workerPool.Put(w)
|
||||||
}
|
}
|
||||||
return w
|
return w
|
||||||
}
|
}
|
||||||
|
|
||||||
// putWorker puts a worker back into free pool, recycling the goroutines.
|
// putWorker puts a worker back into free pool, recycling the goroutines.
|
||||||
func (p *Pool) putWorker(worker *Worker) {
|
func (p *Pool) putWorker(worker *Worker) {
|
||||||
p.workerPool.Put(worker)
|
|
||||||
p.lock.Lock()
|
p.lock.Lock()
|
||||||
p.workers = append(p.workers, worker)
|
p.workers = append(p.workers, worker)
|
||||||
p.lock.Unlock()
|
p.lock.Unlock()
|
||||||
|
|
|
@ -178,20 +178,19 @@ func (p *PoolWithFunc) getWorker() *WorkerWithFunc {
|
||||||
if wp == nil {
|
if wp == nil {
|
||||||
w = &WorkerWithFunc{
|
w = &WorkerWithFunc{
|
||||||
pool: p,
|
pool: p,
|
||||||
args: make(chan interface{}),
|
args: make(chan interface{}, workerArgsCap),
|
||||||
}
|
}
|
||||||
w.run()
|
|
||||||
atomic.AddInt32(&p.running, 1)
|
|
||||||
} else {
|
} else {
|
||||||
w = wp.(*WorkerWithFunc)
|
w = wp.(*WorkerWithFunc)
|
||||||
}
|
}
|
||||||
|
w.run()
|
||||||
|
p.workerPool.Put(w)
|
||||||
}
|
}
|
||||||
return w
|
return w
|
||||||
}
|
}
|
||||||
|
|
||||||
// putWorker puts a worker back into free pool, recycling the goroutines.
|
// putWorker puts a worker back into free pool, recycling the goroutines.
|
||||||
func (p *PoolWithFunc) putWorker(worker *WorkerWithFunc) {
|
func (p *PoolWithFunc) putWorker(worker *WorkerWithFunc) {
|
||||||
p.workerPool.Put(worker)
|
|
||||||
p.lock.Lock()
|
p.lock.Lock()
|
||||||
p.workers = append(p.workers, worker)
|
p.workers = append(p.workers, worker)
|
||||||
p.lock.Unlock()
|
p.lock.Unlock()
|
||||||
|
|
|
@ -40,6 +40,7 @@ type Worker struct {
|
||||||
// run will start a goroutine to repeat the process
|
// run will start a goroutine to repeat the process
|
||||||
// that perform the function calls.
|
// that perform the function calls.
|
||||||
func (w *Worker) run() {
|
func (w *Worker) run() {
|
||||||
|
atomic.AddInt32(&w.pool.running, 1)
|
||||||
go func() {
|
go func() {
|
||||||
for f := range w.task {
|
for f := range w.task {
|
||||||
if f == nil {
|
if f == nil {
|
||||||
|
|
|
@ -40,6 +40,7 @@ type WorkerWithFunc struct {
|
||||||
// run will start a goroutine to repeat the process
|
// run will start a goroutine to repeat the process
|
||||||
// that perform the function calls.
|
// that perform the function calls.
|
||||||
func (w *WorkerWithFunc) run() {
|
func (w *WorkerWithFunc) run() {
|
||||||
|
atomic.AddInt32(&w.pool.running, 1)
|
||||||
go func() {
|
go func() {
|
||||||
for args := range w.args {
|
for args := range w.args {
|
||||||
if args == nil {
|
if args == nil {
|
||||||
|
|
Loading…
Reference in New Issue