This commit is contained in:
Andy Pan 2018-07-16 02:33:43 +08:00
parent eaf79d239f
commit d31b2413c6
2 changed files with 22 additions and 28 deletions

25
pool.go
View File

@ -68,7 +68,8 @@ func (p *Pool) monitorAndClear() {
currentTime := time.Now() currentTime := time.Now()
p.lock.Lock() p.lock.Lock()
idleWorkers := p.workers 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 return
} }
n := 0 n := 0
@ -142,21 +143,17 @@ func (p *Pool) Cap() int {
// ReSize change the capacity of this pool // ReSize change the capacity of this pool
func (p *Pool) ReSize(size int) { func (p *Pool) ReSize(size int) {
if size < p.Cap() { 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() {
return return
} } else if size < p.Cap() {
diff := p.Cap() - size
atomic.StoreInt32(&p.capacity, int32(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 // Release Closed this pool

View File

@ -69,7 +69,8 @@ func (p *PoolWithFunc) monitorAndClear() {
currentTime := time.Now() currentTime := time.Now()
p.lock.Lock() p.lock.Lock()
idleWorkers := p.workers 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 return
} }
n := 0 n := 0
@ -147,21 +148,17 @@ func (p *PoolWithFunc) Cap() int {
// ReSize change the capacity of this pool // ReSize change the capacity of this pool
func (p *PoolWithFunc) ReSize(size int) { func (p *PoolWithFunc) ReSize(size int) {
if size < p.Cap() { 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() {
return return
} } else if size < p.Cap() {
diff := p.Cap() - size
atomic.StoreInt32(&p.capacity, int32(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 // Release Closed this pool