forked from mirror/ants
fixed some issues
This commit is contained in:
parent
c186871e60
commit
eaf79d239f
|
@ -112,7 +112,7 @@ func TestCodeCov(t *testing.T) {
|
||||||
t.Logf("pool, free workers number:%d", p0.Free())
|
t.Logf("pool, free workers number:%d", p0.Free())
|
||||||
p0.ReSize(AntsSize)
|
p0.ReSize(AntsSize)
|
||||||
p0.ReSize(AntsSize / 2)
|
p0.ReSize(AntsSize / 2)
|
||||||
t.Logf("pool, after resize, capacity:%d", p0.Cap())
|
t.Logf("pool, after resize, capacity:%d, running:%d", p0.Cap(), p0.Running())
|
||||||
|
|
||||||
p, _ := ants.NewPoolWithFunc(AntsSize, demoPoolFunc)
|
p, _ := ants.NewPoolWithFunc(AntsSize, demoPoolFunc)
|
||||||
defer p.Serve(Param)
|
defer p.Serve(Param)
|
||||||
|
@ -125,7 +125,7 @@ func TestCodeCov(t *testing.T) {
|
||||||
t.Logf("pool with func, free workers number:%d", p.Free())
|
t.Logf("pool with func, free workers number:%d", p.Free())
|
||||||
p.ReSize(AntsSize)
|
p.ReSize(AntsSize)
|
||||||
p.ReSize(AntsSize / 2)
|
p.ReSize(AntsSize / 2)
|
||||||
t.Logf("pool with func, after resize, capacity:%d", p.Cap())
|
t.Logf("pool with func, after resize, capacity:%d, running:%d", p.Cap(), p.Running())
|
||||||
}
|
}
|
||||||
|
|
||||||
// func TestNoPool(t *testing.T) {
|
// func TestNoPool(t *testing.T) {
|
||||||
|
|
65
pool.go
65
pool.go
|
@ -68,15 +68,18 @@ 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 {
|
||||||
|
return
|
||||||
|
}
|
||||||
n := 0
|
n := 0
|
||||||
for i, w := range idleWorkers {
|
for i, w := range idleWorkers {
|
||||||
if currentTime.Sub(w.recycleTime) <= p.expiryDuration {
|
if currentTime.Sub(w.recycleTime) <= p.expiryDuration {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
n = i
|
n = i
|
||||||
|
<-p.freeSignal
|
||||||
w.task <- nil
|
w.task <- nil
|
||||||
idleWorkers[i] = nil
|
idleWorkers[i] = nil
|
||||||
atomic.AddInt32(&p.running, 1)
|
|
||||||
}
|
}
|
||||||
if n > 0 {
|
if n > 0 {
|
||||||
n++
|
n++
|
||||||
|
@ -137,34 +140,42 @@ func (p *Pool) Cap() int {
|
||||||
return int(atomic.LoadInt32(&p.capacity))
|
return int(atomic.LoadInt32(&p.capacity))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Release Closed this pool
|
|
||||||
func (p *Pool) Release() error {
|
|
||||||
p.once.Do(func() {
|
|
||||||
p.release <- sig{}
|
|
||||||
running := p.Running()
|
|
||||||
for i := 0; i < running; i++ {
|
|
||||||
p.getWorker().task <- nil
|
|
||||||
}
|
|
||||||
p.lock.Lock()
|
|
||||||
p.workers = nil
|
|
||||||
p.lock.Unlock()
|
|
||||||
})
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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
|
diff := p.Cap() - size
|
||||||
|
p.lock.Lock()
|
||||||
|
idleWorkers := p.workers
|
||||||
for i := 0; i < diff; i++ {
|
for i := 0; i < diff; i++ {
|
||||||
p.getWorker().task <- nil
|
<-p.freeSignal
|
||||||
|
idleWorkers[i].task <- nil
|
||||||
|
idleWorkers[i] = nil
|
||||||
}
|
}
|
||||||
|
p.workers = idleWorkers[diff:]
|
||||||
|
p.lock.Unlock()
|
||||||
} else if size == p.Cap() {
|
} else if size == p.Cap() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
atomic.StoreInt32(&p.capacity, int32(size))
|
atomic.StoreInt32(&p.capacity, int32(size))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Release Closed this pool
|
||||||
|
func (p *Pool) Release() error {
|
||||||
|
p.once.Do(func() {
|
||||||
|
p.release <- sig{}
|
||||||
|
p.lock.Lock()
|
||||||
|
idleWorkers := p.workers
|
||||||
|
for i, w := range idleWorkers {
|
||||||
|
<-p.freeSignal
|
||||||
|
w.task <- nil
|
||||||
|
idleWorkers[i] = nil
|
||||||
|
}
|
||||||
|
p.workers = nil
|
||||||
|
p.lock.Unlock()
|
||||||
|
})
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
//-------------------------------------------------------------------------
|
//-------------------------------------------------------------------------
|
||||||
|
|
||||||
// getWorker returns a available worker to run the tasks.
|
// getWorker returns a available worker to run the tasks.
|
||||||
|
@ -173,8 +184,8 @@ func (p *Pool) getWorker() *Worker {
|
||||||
waiting := false
|
waiting := false
|
||||||
|
|
||||||
p.lock.Lock()
|
p.lock.Lock()
|
||||||
workers := p.workers
|
idleWorkers := p.workers
|
||||||
n := len(workers) - 1
|
n := len(idleWorkers) - 1
|
||||||
if n < 0 {
|
if n < 0 {
|
||||||
if p.Running() >= p.Cap() {
|
if p.Running() >= p.Cap() {
|
||||||
waiting = true
|
waiting = true
|
||||||
|
@ -183,20 +194,20 @@ func (p *Pool) getWorker() *Worker {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
<-p.freeSignal
|
<-p.freeSignal
|
||||||
w = workers[n]
|
w = idleWorkers[n]
|
||||||
workers[n] = nil
|
idleWorkers[n] = nil
|
||||||
p.workers = workers[:n]
|
p.workers = idleWorkers[:n]
|
||||||
}
|
}
|
||||||
p.lock.Unlock()
|
p.lock.Unlock()
|
||||||
|
|
||||||
if waiting {
|
if waiting {
|
||||||
<-p.freeSignal
|
<-p.freeSignal
|
||||||
p.lock.Lock()
|
p.lock.Lock()
|
||||||
workers = p.workers
|
idleWorkers = p.workers
|
||||||
l := len(workers) - 1
|
l := len(idleWorkers) - 1
|
||||||
w = workers[l]
|
w = idleWorkers[l]
|
||||||
workers[l] = nil
|
idleWorkers[l] = nil
|
||||||
p.workers = workers[:l]
|
p.workers = idleWorkers[:l]
|
||||||
p.lock.Unlock()
|
p.lock.Unlock()
|
||||||
} else if w == nil {
|
} else if w == nil {
|
||||||
w = &Worker{
|
w = &Worker{
|
||||||
|
|
65
pool_func.go
65
pool_func.go
|
@ -69,15 +69,18 @@ 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 {
|
||||||
|
return
|
||||||
|
}
|
||||||
n := 0
|
n := 0
|
||||||
for i, w := range idleWorkers {
|
for i, w := range idleWorkers {
|
||||||
if currentTime.Sub(w.recycleTime) <= p.expiryDuration {
|
if currentTime.Sub(w.recycleTime) <= p.expiryDuration {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
n = i
|
n = i
|
||||||
|
<-p.freeSignal
|
||||||
w.args <- nil
|
w.args <- nil
|
||||||
idleWorkers[i] = nil
|
idleWorkers[i] = nil
|
||||||
atomic.AddInt32(&p.running, 1)
|
|
||||||
}
|
}
|
||||||
if n > 0 {
|
if n > 0 {
|
||||||
n++
|
n++
|
||||||
|
@ -142,34 +145,42 @@ func (p *PoolWithFunc) Cap() int {
|
||||||
return int(atomic.LoadInt32(&p.capacity))
|
return int(atomic.LoadInt32(&p.capacity))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Release Closed this pool
|
|
||||||
func (p *PoolWithFunc) Release() error {
|
|
||||||
p.once.Do(func() {
|
|
||||||
p.release <- sig{}
|
|
||||||
running := p.Running()
|
|
||||||
for i := 0; i < running; i++ {
|
|
||||||
p.getWorker().args <- nil
|
|
||||||
}
|
|
||||||
p.lock.Lock()
|
|
||||||
p.workers = nil
|
|
||||||
p.lock.Unlock()
|
|
||||||
})
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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
|
diff := p.Cap() - size
|
||||||
|
p.lock.Lock()
|
||||||
|
idleWorkers := p.workers
|
||||||
for i := 0; i < diff; i++ {
|
for i := 0; i < diff; i++ {
|
||||||
p.getWorker().args <- nil
|
<-p.freeSignal
|
||||||
|
idleWorkers[i].args <- nil
|
||||||
|
idleWorkers[i] = nil
|
||||||
}
|
}
|
||||||
|
p.workers = idleWorkers[diff:]
|
||||||
|
p.lock.Unlock()
|
||||||
} else if size == p.Cap() {
|
} else if size == p.Cap() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
atomic.StoreInt32(&p.capacity, int32(size))
|
atomic.StoreInt32(&p.capacity, int32(size))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Release Closed this pool
|
||||||
|
func (p *PoolWithFunc) Release() error {
|
||||||
|
p.once.Do(func() {
|
||||||
|
p.release <- sig{}
|
||||||
|
p.lock.Lock()
|
||||||
|
idleWorkers := p.workers
|
||||||
|
for i, w := range idleWorkers {
|
||||||
|
<-p.freeSignal
|
||||||
|
w.args <- nil
|
||||||
|
idleWorkers[i] = nil
|
||||||
|
}
|
||||||
|
p.workers = nil
|
||||||
|
p.lock.Unlock()
|
||||||
|
})
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
//-------------------------------------------------------------------------
|
//-------------------------------------------------------------------------
|
||||||
|
|
||||||
// getWorker returns a available worker to run the tasks.
|
// getWorker returns a available worker to run the tasks.
|
||||||
|
@ -178,8 +189,8 @@ func (p *PoolWithFunc) getWorker() *WorkerWithFunc {
|
||||||
waiting := false
|
waiting := false
|
||||||
|
|
||||||
p.lock.Lock()
|
p.lock.Lock()
|
||||||
workers := p.workers
|
idleWorkers := p.workers
|
||||||
n := len(workers) - 1
|
n := len(idleWorkers) - 1
|
||||||
if n < 0 {
|
if n < 0 {
|
||||||
if p.Running() >= p.Cap() {
|
if p.Running() >= p.Cap() {
|
||||||
waiting = true
|
waiting = true
|
||||||
|
@ -188,20 +199,20 @@ func (p *PoolWithFunc) getWorker() *WorkerWithFunc {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
<-p.freeSignal
|
<-p.freeSignal
|
||||||
w = workers[n]
|
w = idleWorkers[n]
|
||||||
workers[n] = nil
|
idleWorkers[n] = nil
|
||||||
p.workers = workers[:n]
|
p.workers = idleWorkers[:n]
|
||||||
}
|
}
|
||||||
p.lock.Unlock()
|
p.lock.Unlock()
|
||||||
|
|
||||||
if waiting {
|
if waiting {
|
||||||
<-p.freeSignal
|
<-p.freeSignal
|
||||||
p.lock.Lock()
|
p.lock.Lock()
|
||||||
workers = p.workers
|
idleWorkers = p.workers
|
||||||
l := len(workers) - 1
|
l := len(idleWorkers) - 1
|
||||||
w = workers[l]
|
w = idleWorkers[l]
|
||||||
workers[l] = nil
|
idleWorkers[l] = nil
|
||||||
p.workers = workers[:l]
|
p.workers = idleWorkers[:l]
|
||||||
p.lock.Unlock()
|
p.lock.Unlock()
|
||||||
} else if w == nil {
|
} else if w == nil {
|
||||||
w = &WorkerWithFunc{
|
w = &WorkerWithFunc{
|
||||||
|
|
|
@ -44,7 +44,6 @@ type Worker struct {
|
||||||
// run starts a goroutine to repeat the process
|
// run starts a goroutine to repeat the process
|
||||||
// that performs the function calls.
|
// that performs the function calls.
|
||||||
func (w *Worker) run() {
|
func (w *Worker) run() {
|
||||||
//atomic.AddInt32(&w.pool.running, 1)
|
|
||||||
go func() {
|
go func() {
|
||||||
for f := range w.task {
|
for f := range w.task {
|
||||||
if f == nil {
|
if f == nil {
|
||||||
|
|
|
@ -44,7 +44,6 @@ type WorkerWithFunc struct {
|
||||||
// run starts a goroutine to repeat the process
|
// run starts a goroutine to repeat the process
|
||||||
// that performs the function calls.
|
// that performs the function calls.
|
||||||
func (w *WorkerWithFunc) run() {
|
func (w *WorkerWithFunc) run() {
|
||||||
//atomic.AddInt32(&w.pool.running, 1)
|
|
||||||
go func() {
|
go func() {
|
||||||
for args := range w.args {
|
for args := range w.args {
|
||||||
if args == nil {
|
if args == nil {
|
||||||
|
|
Loading…
Reference in New Issue