mirror of https://github.com/panjf2000/ants.git
simplify and optimize goroutine-worker
This commit is contained in:
parent
3afa151dd9
commit
813cc218fe
|
@ -75,7 +75,7 @@ func (p *PoolWithFunc) monitorAndClear() {
|
|||
break
|
||||
}
|
||||
n = i
|
||||
w.stop()
|
||||
w.args <- nil
|
||||
idleWorkers[i] = nil
|
||||
p.running--
|
||||
}
|
||||
|
@ -123,7 +123,7 @@ func (p *PoolWithFunc) Serve(args interface{}) error {
|
|||
return ErrPoolClosed
|
||||
}
|
||||
w := p.getWorker()
|
||||
w.sendTask(args)
|
||||
w.args <- args
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -148,7 +148,7 @@ func (p *PoolWithFunc) Release() error {
|
|||
p.release <- sig{}
|
||||
running := p.Running()
|
||||
for i := 0; i < running; i++ {
|
||||
p.getWorker().stop()
|
||||
p.getWorker().args <- nil
|
||||
}
|
||||
for i := range p.workers {
|
||||
p.workers[i] = nil
|
||||
|
@ -162,7 +162,7 @@ func (p *PoolWithFunc) ReSize(size int) {
|
|||
if size < p.Cap() {
|
||||
diff := p.Cap() - size
|
||||
for i := 0; i < diff; i++ {
|
||||
p.getWorker().stop()
|
||||
p.getWorker().args <- nil
|
||||
}
|
||||
} else if size == p.Cap() {
|
||||
return
|
||||
|
|
|
@ -56,13 +56,3 @@ func (w *WorkerWithFunc) run() {
|
|||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// stop this worker.
|
||||
func (w *WorkerWithFunc) stop() {
|
||||
w.sendTask(nil)
|
||||
}
|
||||
|
||||
// sendTask sends a task to this worker.
|
||||
func (w *WorkerWithFunc) sendTask(args interface{}) {
|
||||
w.args <- args
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue