From a66958c9d3de36f7f6d704211005104f14530013 Mon Sep 17 00:00:00 2001 From: E_L <54755533+ELchem@users.noreply.github.com> Date: Sat, 1 Jul 2023 00:55:34 -0400 Subject: [PATCH] feat: add a new API `ReleaseTimeout` for the default pool (#285) Co-authored-by: EL --- ants.go | 5 +++++ ants_test.go | 12 ++++++++++++ 2 files changed, 17 insertions(+) diff --git a/ants.go b/ants.go index 18b3dc8f..4ec81fad 100644 --- a/ants.go +++ b/ants.go @@ -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() diff --git a/ants_test.go b/ants_test.go index 93bbbeac..c216066d 100644 --- a/ants_test.go +++ b/ants_test.go @@ -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) +}