feat: add a new API `ReleaseTimeout` for the default pool (#285)

Co-authored-by: EL <evnldn9321@gmail.com>
This commit is contained in:
E_L 2023-07-01 00:55:34 -04:00 committed by GitHub
parent d3b35b8db2
commit a66958c9d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 0 deletions

View File

@ -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()

View File

@ -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)
}