simplify and optimize goroutine-worker

This commit is contained in:
Andy Pan 2018-07-15 00:52:27 +08:00
parent 3afa151dd9
commit 813cc218fe
2 changed files with 4 additions and 14 deletions

View File

@ -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

View File

@ -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
}