forked from mirror/ants
Merge branch 'develop'
This commit is contained in:
commit
f5b37d0798
50
pool.go
50
pool.go
|
@ -63,32 +63,30 @@ type Pool struct {
|
||||||
|
|
||||||
func (p *Pool) periodicallyPurge() {
|
func (p *Pool) periodicallyPurge() {
|
||||||
heartbeat := time.NewTicker(p.expiryDuration)
|
heartbeat := time.NewTicker(p.expiryDuration)
|
||||||
go func() {
|
for range heartbeat.C {
|
||||||
for range heartbeat.C {
|
currentTime := time.Now()
|
||||||
currentTime := time.Now()
|
p.lock.Lock()
|
||||||
p.lock.Lock()
|
idleWorkers := p.workers
|
||||||
idleWorkers := p.workers
|
if len(idleWorkers) == 0 && p.Running() == 0 && len(p.release) > 0 {
|
||||||
if len(idleWorkers) == 0 && p.Running() == 0 && len(p.release) > 0 {
|
|
||||||
p.lock.Unlock()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
n := 0
|
|
||||||
for i, w := range idleWorkers {
|
|
||||||
if currentTime.Sub(w.recycleTime) <= p.expiryDuration {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
n = i
|
|
||||||
<-p.freeSignal
|
|
||||||
w.task <- nil
|
|
||||||
idleWorkers[i] = nil
|
|
||||||
}
|
|
||||||
if n > 0 {
|
|
||||||
n++
|
|
||||||
p.workers = idleWorkers[n:]
|
|
||||||
}
|
|
||||||
p.lock.Unlock()
|
p.lock.Unlock()
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}()
|
n := 0
|
||||||
|
for i, w := range idleWorkers {
|
||||||
|
if currentTime.Sub(w.recycleTime) <= p.expiryDuration {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
n = i
|
||||||
|
<-p.freeSignal
|
||||||
|
w.task <- nil
|
||||||
|
idleWorkers[i] = nil
|
||||||
|
}
|
||||||
|
if n > 0 {
|
||||||
|
n++
|
||||||
|
p.workers = idleWorkers[n:]
|
||||||
|
}
|
||||||
|
p.lock.Unlock()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewPool generates a instance of ants pool
|
// NewPool generates a instance of ants pool
|
||||||
|
@ -110,7 +108,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.periodicallyPurge()
|
go p.periodicallyPurge()
|
||||||
return p, nil
|
return p, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -208,7 +206,7 @@ func (p *Pool) getWorker() *Worker {
|
||||||
} else if w == nil {
|
} else if w == nil {
|
||||||
w = &Worker{
|
w = &Worker{
|
||||||
pool: p,
|
pool: p,
|
||||||
task: make(chan f),
|
task: make(chan f, 1),
|
||||||
}
|
}
|
||||||
w.run()
|
w.run()
|
||||||
}
|
}
|
||||||
|
|
50
pool_func.go
50
pool_func.go
|
@ -64,32 +64,30 @@ type PoolWithFunc struct {
|
||||||
|
|
||||||
func (p *PoolWithFunc) periodicallyPurge() {
|
func (p *PoolWithFunc) periodicallyPurge() {
|
||||||
heartbeat := time.NewTicker(p.expiryDuration)
|
heartbeat := time.NewTicker(p.expiryDuration)
|
||||||
go func() {
|
for range heartbeat.C {
|
||||||
for range heartbeat.C {
|
currentTime := time.Now()
|
||||||
currentTime := time.Now()
|
p.lock.Lock()
|
||||||
p.lock.Lock()
|
idleWorkers := p.workers
|
||||||
idleWorkers := p.workers
|
if len(idleWorkers) == 0 && p.Running() == 0 && len(p.release) > 0 {
|
||||||
if len(idleWorkers) == 0 && p.Running() == 0 && len(p.release) > 0 {
|
|
||||||
p.lock.Unlock()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
n := 0
|
|
||||||
for i, w := range idleWorkers {
|
|
||||||
if currentTime.Sub(w.recycleTime) <= p.expiryDuration {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
n = i
|
|
||||||
<-p.freeSignal
|
|
||||||
w.args <- nil
|
|
||||||
idleWorkers[i] = nil
|
|
||||||
}
|
|
||||||
if n > 0 {
|
|
||||||
n++
|
|
||||||
p.workers = idleWorkers[n:]
|
|
||||||
}
|
|
||||||
p.lock.Unlock()
|
p.lock.Unlock()
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}()
|
n := 0
|
||||||
|
for i, w := range idleWorkers {
|
||||||
|
if currentTime.Sub(w.recycleTime) <= p.expiryDuration {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
n = i
|
||||||
|
<-p.freeSignal
|
||||||
|
w.args <- nil
|
||||||
|
idleWorkers[i] = nil
|
||||||
|
}
|
||||||
|
if n > 0 {
|
||||||
|
n++
|
||||||
|
p.workers = idleWorkers[n:]
|
||||||
|
}
|
||||||
|
p.lock.Unlock()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewPoolWithFunc generates a instance of ants pool with a specific function
|
// NewPoolWithFunc generates a instance of ants pool with a specific function
|
||||||
|
@ -112,7 +110,7 @@ func NewTimingPoolWithFunc(size, expiry int, f pf) (*PoolWithFunc, error) {
|
||||||
expiryDuration: time.Duration(expiry) * time.Second,
|
expiryDuration: time.Duration(expiry) * time.Second,
|
||||||
poolFunc: f,
|
poolFunc: f,
|
||||||
}
|
}
|
||||||
p.periodicallyPurge()
|
go p.periodicallyPurge()
|
||||||
return p, nil
|
return p, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -213,7 +211,7 @@ func (p *PoolWithFunc) getWorker() *WorkerWithFunc {
|
||||||
} else if w == nil {
|
} else if w == nil {
|
||||||
w = &WorkerWithFunc{
|
w = &WorkerWithFunc{
|
||||||
pool: p,
|
pool: p,
|
||||||
args: make(chan interface{}),
|
args: make(chan interface{}, 1),
|
||||||
}
|
}
|
||||||
w.run()
|
w.run()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue