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 break
} }
n = i n = i
w.stop() w.args <- nil
idleWorkers[i] = nil idleWorkers[i] = nil
p.running-- p.running--
} }
@ -123,7 +123,7 @@ func (p *PoolWithFunc) Serve(args interface{}) error {
return ErrPoolClosed return ErrPoolClosed
} }
w := p.getWorker() w := p.getWorker()
w.sendTask(args) w.args <- args
return nil return nil
} }
@ -148,7 +148,7 @@ func (p *PoolWithFunc) Release() error {
p.release <- sig{} p.release <- sig{}
running := p.Running() running := p.Running()
for i := 0; i < running; i++ { for i := 0; i < running; i++ {
p.getWorker().stop() p.getWorker().args <- nil
} }
for i := range p.workers { for i := range p.workers {
p.workers[i] = nil p.workers[i] = nil
@ -162,7 +162,7 @@ func (p *PoolWithFunc) ReSize(size int) {
if size < p.Cap() { if size < p.Cap() {
diff := p.Cap() - size diff := p.Cap() - size
for i := 0; i < diff; i++ { for i := 0; i < diff; i++ {
p.getWorker().stop() p.getWorker().args <- nil
} }
} else if size == p.Cap() { } else if size == p.Cap() {
return 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
}