From 3ffd3daa372c14cdc187acdd5c13d446416e487a Mon Sep 17 00:00:00 2001 From: Andy Pan Date: Mon, 17 Jun 2024 20:13:15 +0800 Subject: [PATCH] opt: calculate the interval for ReleaseTimeout() based on a default count (#327) This PR reverts #325 to some extent. --- ants.go | 4 ++-- pool.go | 3 ++- pool_func.go | 3 ++- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/ants.go b/ants.go index 2da0304..246d91c 100644 --- a/ants.go +++ b/ants.go @@ -97,8 +97,8 @@ var ( ) const ( - nowTimeUpdateInterval = 500 * time.Millisecond - releaseTimeoutInterval = 100 * time.Millisecond + nowTimeUpdateInterval = 500 * time.Millisecond + releaseTimeoutCount = 10 ) // Logger is used for logging formatted messages. diff --git a/pool.go b/pool.go index 5ef1874..8406da4 100644 --- a/pool.go +++ b/pool.go @@ -299,6 +299,7 @@ func (p *Pool) ReleaseTimeout(timeout time.Duration) error { } p.Release() + interval := timeout / releaseTimeoutCount endTime := time.Now().Add(timeout) for time.Now().Before(endTime) { if p.Running() == 0 && @@ -306,7 +307,7 @@ func (p *Pool) ReleaseTimeout(timeout time.Duration) error { atomic.LoadInt32(&p.ticktockDone) == 1 { return nil } - time.Sleep(releaseTimeoutInterval) + time.Sleep(interval) } return ErrTimeout } diff --git a/pool_func.go b/pool_func.go index ef3a664..290ae36 100644 --- a/pool_func.go +++ b/pool_func.go @@ -304,6 +304,7 @@ func (p *PoolWithFunc) ReleaseTimeout(timeout time.Duration) error { } p.Release() + interval := timeout / releaseTimeoutCount endTime := time.Now().Add(timeout) for time.Now().Before(endTime) { if p.Running() == 0 && @@ -311,7 +312,7 @@ func (p *PoolWithFunc) ReleaseTimeout(timeout time.Duration) error { atomic.LoadInt32(&p.ticktockDone) == 1 { return nil } - time.Sleep(releaseTimeoutInterval) + time.Sleep(interval) } return ErrTimeout }