Skip to content

Commit

Permalink
Add a new method -- ReleaseTimeout() for waiting all workers to exit
Browse files Browse the repository at this point in the history
Fixes #212
  • Loading branch information
panjf2000 committed Mar 8, 2022
1 parent 134f354 commit 96d0742
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 11 deletions.
3 changes: 3 additions & 0 deletions ants.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ var (
// ErrInvalidPreAllocSize will be returned when trying to set up a negative capacity under PreAlloc mode.
ErrInvalidPreAllocSize = errors.New("can not set up a negative capacity under PreAlloc mode")

// ErrTimeout will be returned after the operations timed out.
ErrTimeout = errors.New("operation timed out")

//---------------------------------------------------------------------------

// workerChanCap determines whether the channel of a worker should be a buffered channel
Expand Down
24 changes: 24 additions & 0 deletions ants_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -822,3 +822,27 @@ func TestPoolTuneScaleUp(t *testing.T) {
close(c)
pf.Release()
}

func TestReleaseTimeout(t *testing.T) {
p, _ := NewPool(10)
for i := 0; i < 5; i++ {
_ = p.Submit(func() {
time.Sleep(time.Second)
})
}
assert.NotZero(t, p.Running())
err := p.ReleaseTimeout(2 * time.Second)
assert.NoError(t, err)

var pf *PoolWithFunc
pf, _ = NewPoolWithFunc(10, func(i interface{}) {
dur := i.(time.Duration)
time.Sleep(dur)
})
for i := 0; i < 5; i++ {
_ = pf.Invoke(time.Second)
}
assert.NotZero(t, pf.Running())
err = pf.ReleaseTimeout(2 * time.Second)
assert.NoError(t, err)
}
44 changes: 39 additions & 5 deletions pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
package ants

import (
"errors"
"sync"
"sync/atomic"
"time"
Expand Down Expand Up @@ -58,6 +59,8 @@ type Pool struct {
// blockingNum is the number of the goroutines already been blocked on pool.Submit, protected by pool.lock
blockingNum int

stopHeartbeat chan struct{}

options *Options
}

Expand All @@ -66,7 +69,14 @@ func (p *Pool) purgePeriodically() {
heartbeat := time.NewTicker(p.options.ExpiryDuration)
defer heartbeat.Stop()

for range heartbeat.C {
for {
select {
case <-heartbeat.C:
case <-p.stopHeartbeat:
p.stopHeartbeat <- struct{}{}
return
}

if p.IsClosed() {
break
}
Expand Down Expand Up @@ -112,9 +122,10 @@ func NewPool(size int, options ...Option) (*Pool, error) {
}

p := &Pool{
capacity: int32(size),
lock: internal.NewSpinLock(),
options: opts,
capacity: int32(size),
lock: internal.NewSpinLock(),
stopHeartbeat: make(chan struct{}, 1),
options: opts,
}
p.workerCache.New = func() interface{} {
return &goWorker{
Expand Down Expand Up @@ -201,7 +212,9 @@ func (p *Pool) IsClosed() bool {

// Release closes this pool and releases the worker queue.
func (p *Pool) Release() {
atomic.StoreInt32(&p.state, CLOSED)
if !atomic.CompareAndSwapInt32(&p.state, OPENED, CLOSED) {
return
}
p.lock.Lock()
p.workers.reset()
p.lock.Unlock()
Expand All @@ -210,6 +223,27 @@ func (p *Pool) Release() {
p.cond.Broadcast()
}

// ReleaseTimeout is like Release but with a timeout, it waits all workers to exit before timing out.
func (p *Pool) ReleaseTimeout(timeout time.Duration) error {
if p.IsClosed() {
return errors.New("pool is already closed")
}
select {
case p.stopHeartbeat <- struct{}{}:
<-p.stopHeartbeat
default:
}
p.Release()
endTime := time.Now().Add(timeout)
for time.Now().Before(endTime) {
if p.Running() == 0 {
return nil
}
time.Sleep(10 * time.Millisecond)
}
return ErrTimeout
}

// Reboot reboots a closed pool.
func (p *Pool) Reboot() {
if atomic.CompareAndSwapInt32(&p.state, CLOSED, OPENED) {
Expand Down
46 changes: 40 additions & 6 deletions pool_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
package ants

import (
"errors"
"sync"
"sync/atomic"
"time"
Expand Down Expand Up @@ -60,6 +61,8 @@ type PoolWithFunc struct {
// blockingNum is the number of the goroutines already been blocked on pool.Submit, protected by pool.lock
blockingNum int

stopHeartbeat chan struct{}

options *Options
}

Expand All @@ -69,7 +72,14 @@ func (p *PoolWithFunc) purgePeriodically() {
defer heartbeat.Stop()

var expiredWorkers []*goWorkerWithFunc
for range heartbeat.C {
for {
select {
case <-heartbeat.C:
case <-p.stopHeartbeat:
p.stopHeartbeat <- struct{}{}
return
}

if p.IsClosed() {
break
}
Expand Down Expand Up @@ -131,10 +141,11 @@ func NewPoolWithFunc(size int, pf func(interface{}), options ...Option) (*PoolWi
}

p := &PoolWithFunc{
capacity: int32(size),
poolFunc: pf,
lock: internal.NewSpinLock(),
options: opts,
capacity: int32(size),
poolFunc: pf,
lock: internal.NewSpinLock(),
stopHeartbeat: make(chan struct{}, 1),
options: opts,
}
p.workerCache.New = func() interface{} {
return &goWorkerWithFunc{
Expand Down Expand Up @@ -218,7 +229,9 @@ func (p *PoolWithFunc) IsClosed() bool {

// Release closes this pool and releases the worker queue.
func (p *PoolWithFunc) Release() {
atomic.StoreInt32(&p.state, CLOSED)
if !atomic.CompareAndSwapInt32(&p.state, OPENED, CLOSED) {
return
}
p.lock.Lock()
idleWorkers := p.workers
for _, w := range idleWorkers {
Expand All @@ -231,6 +244,27 @@ func (p *PoolWithFunc) Release() {
p.cond.Broadcast()
}

// ReleaseTimeout is like Release but with a timeout, it waits all workers to exit before timing out.
func (p *PoolWithFunc) ReleaseTimeout(timeout time.Duration) error {
if p.IsClosed() {
return errors.New("pool is already closed")
}
select {
case p.stopHeartbeat <- struct{}{}:
<-p.stopHeartbeat
default:
}
p.Release()
endTime := time.Now().Add(timeout)
for time.Now().Before(endTime) {
if p.Running() == 0 {
return nil
}
time.Sleep(10 * time.Millisecond)
}
return ErrTimeout
}

// Reboot reboots a closed pool.
func (p *PoolWithFunc) Reboot() {
if atomic.CompareAndSwapInt32(&p.state, CLOSED, OPENED) {
Expand Down

0 comments on commit 96d0742

Please sign in to comment.