mirror of https://github.com/panjf2000/ants.git
解决死循环导致cpu占用率过高
This commit is contained in:
parent
af376f1b7b
commit
70731aff71
24
pool.go
24
pool.go
|
@ -52,7 +52,7 @@ type Pool struct {
|
||||||
|
|
||||||
// lock for synchronous operation.
|
// lock for synchronous operation.
|
||||||
lock sync.Mutex
|
lock sync.Mutex
|
||||||
|
cond *sync.Cond
|
||||||
once sync.Once
|
once sync.Once
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -105,6 +105,7 @@ func NewTimingPool(size, expiry int) (*Pool, error) {
|
||||||
release: make(chan sig, 1),
|
release: make(chan sig, 1),
|
||||||
expiryDuration: time.Duration(expiry) * time.Second,
|
expiryDuration: time.Duration(expiry) * time.Second,
|
||||||
}
|
}
|
||||||
|
p.cond = sync.NewCond(&p.lock)
|
||||||
go p.periodicallyPurge()
|
go p.periodicallyPurge()
|
||||||
return p, nil
|
return p, nil
|
||||||
}
|
}
|
||||||
|
@ -195,20 +196,11 @@ func (p *Pool) getWorker() *Worker {
|
||||||
p.lock.Unlock()
|
p.lock.Unlock()
|
||||||
|
|
||||||
if waiting {
|
if waiting {
|
||||||
for {
|
p.lock.Lock()
|
||||||
p.lock.Lock()
|
p.cond.Wait()
|
||||||
idleWorkers = p.workers
|
l := len(p.workers) - 1
|
||||||
l := len(idleWorkers) - 1
|
w = p.workers[l]
|
||||||
if l < 0 {
|
p.lock.Unlock()
|
||||||
p.lock.Unlock()
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
w = idleWorkers[l]
|
|
||||||
idleWorkers[l] = nil
|
|
||||||
p.workers = idleWorkers[:l]
|
|
||||||
p.lock.Unlock()
|
|
||||||
break
|
|
||||||
}
|
|
||||||
} else if w == nil {
|
} else if w == nil {
|
||||||
w = &Worker{
|
w = &Worker{
|
||||||
pool: p,
|
pool: p,
|
||||||
|
@ -226,4 +218,6 @@ func (p *Pool) putWorker(worker *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()
|
||||||
|
//通知有一个空闲的worker
|
||||||
|
p.cond.Signal()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue