forked from mirror/ants
update
This commit is contained in:
parent
eaf79d239f
commit
d31b2413c6
25
pool.go
25
pool.go
|
@ -68,7 +68,8 @@ func (p *Pool) monitorAndClear() {
|
|||
currentTime := time.Now()
|
||||
p.lock.Lock()
|
||||
idleWorkers := p.workers
|
||||
if len(idleWorkers) == 0 && len(p.release) > 0 {
|
||||
if len(idleWorkers) == 0 && p.Running() == 0 && len(p.release) > 0 {
|
||||
p.lock.Unlock()
|
||||
return
|
||||
}
|
||||
n := 0
|
||||
|
@ -142,21 +143,17 @@ func (p *Pool) Cap() int {
|
|||
|
||||
// ReSize change the capacity of this pool
|
||||
func (p *Pool) ReSize(size int) {
|
||||
if size < p.Cap() {
|
||||
diff := p.Cap() - size
|
||||
p.lock.Lock()
|
||||
idleWorkers := p.workers
|
||||
for i := 0; i < diff; i++ {
|
||||
<-p.freeSignal
|
||||
idleWorkers[i].task <- nil
|
||||
idleWorkers[i] = nil
|
||||
}
|
||||
p.workers = idleWorkers[diff:]
|
||||
p.lock.Unlock()
|
||||
} else if size == p.Cap() {
|
||||
if size == p.Cap() {
|
||||
return
|
||||
}
|
||||
} else if size < p.Cap() {
|
||||
diff := p.Cap() - size
|
||||
atomic.StoreInt32(&p.capacity, int32(size))
|
||||
for i := 0; i < diff; i++ {
|
||||
p.getWorker().task <- nil
|
||||
}
|
||||
} else {
|
||||
atomic.StoreInt32(&p.capacity, int32(size))
|
||||
}
|
||||
}
|
||||
|
||||
// Release Closed this pool
|
||||
|
|
25
pool_func.go
25
pool_func.go
|
@ -69,7 +69,8 @@ func (p *PoolWithFunc) monitorAndClear() {
|
|||
currentTime := time.Now()
|
||||
p.lock.Lock()
|
||||
idleWorkers := p.workers
|
||||
if len(idleWorkers) == 0 && len(p.release) > 0 {
|
||||
if len(idleWorkers) == 0 && p.Running() == 0 && len(p.release) > 0 {
|
||||
p.lock.Unlock()
|
||||
return
|
||||
}
|
||||
n := 0
|
||||
|
@ -147,21 +148,17 @@ func (p *PoolWithFunc) Cap() int {
|
|||
|
||||
// ReSize change the capacity of this pool
|
||||
func (p *PoolWithFunc) ReSize(size int) {
|
||||
if size < p.Cap() {
|
||||
diff := p.Cap() - size
|
||||
p.lock.Lock()
|
||||
idleWorkers := p.workers
|
||||
for i := 0; i < diff; i++ {
|
||||
<-p.freeSignal
|
||||
idleWorkers[i].args <- nil
|
||||
idleWorkers[i] = nil
|
||||
}
|
||||
p.workers = idleWorkers[diff:]
|
||||
p.lock.Unlock()
|
||||
} else if size == p.Cap() {
|
||||
if size == p.Cap() {
|
||||
return
|
||||
}
|
||||
} else if size < p.Cap() {
|
||||
diff := p.Cap() - size
|
||||
atomic.StoreInt32(&p.capacity, int32(size))
|
||||
for i := 0; i < diff; i++ {
|
||||
p.getWorker().args <- nil
|
||||
}
|
||||
} else {
|
||||
atomic.StoreInt32(&p.capacity, int32(size))
|
||||
}
|
||||
}
|
||||
|
||||
// Release Closed this pool
|
||||
|
|
Loading…
Reference in New Issue