Skip to content

Commit

Permalink
feat: add a new API ReleaseTimeout for the default pool (#285)
Browse files Browse the repository at this point in the history
Co-authored-by: EL <evnldn9321@gmail.com>
  • Loading branch information
ELchem and ELchem authored Jul 1, 2023
1 parent d3b35b8 commit a66958c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
5 changes: 5 additions & 0 deletions ants.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ func Release() {
defaultAntsPool.Release()
}

// ReleaseTimeout is like Release but with a timeout, it waits all workers to exit before timing out.
func ReleaseTimeout(timeout time.Duration) error {
return defaultAntsPool.ReleaseTimeout(timeout)
}

// Reboot reboots the default pool.
func Reboot() {
defaultAntsPool.Reboot()
Expand Down
12 changes: 12 additions & 0 deletions ants_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -973,3 +973,15 @@ func TestReleaseTimeout(t *testing.T) {
err = pf.ReleaseTimeout(2 * time.Second)
assert.NoError(t, err)
}

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

0 comments on commit a66958c

Please sign in to comment.