Skip to content

Commit

Permalink
opt: calculate the interval for ReleaseTimeout() based on a default c…
Browse files Browse the repository at this point in the history
…ount

This PR reverts #325 to some extent.
  • Loading branch information
panjf2000 committed Jun 17, 2024
1 parent b2374d5 commit 7e41896
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
4 changes: 2 additions & 2 deletions ants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,14 +299,15 @@ 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 &&
(p.options.DisablePurge || atomic.LoadInt32(&p.purgeDone) == 1) &&
atomic.LoadInt32(&p.ticktockDone) == 1 {
return nil
}
time.Sleep(releaseTimeoutInterval)
time.Sleep(interval)
}
return ErrTimeout
}
Expand Down
3 changes: 2 additions & 1 deletion pool_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,14 +304,15 @@ 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 &&
(p.options.DisablePurge || atomic.LoadInt32(&p.purgeDone) == 1) &&
atomic.LoadInt32(&p.ticktockDone) == 1 {
return nil
}
time.Sleep(releaseTimeoutInterval)
time.Sleep(interval)
}
return ErrTimeout
}
Expand Down

0 comments on commit 7e41896

Please sign in to comment.