Files
upstream-centrifugo/internal/timers/pool.go
upstream-mirror b352a9a30d
Some checks failed
test / Lint (push) Failing after 3s
test / Test with Go 1.26.8 (push) Failing after 2s
snapshot of https://github.com/centrifugal/centrifugo.git (master, 2026-09-16; blobs > 131072B omitted)
2026-09-16 01:17:21 +05:00

35 lines
576 B
Go

package timers
import (
"sync"
"time"
)
var timerPool sync.Pool
// AcquireTimer from pool.
func AcquireTimer(d time.Duration) *time.Timer {
v := timerPool.Get()
if v == nil {
return time.NewTimer(d)
}
tm := v.(*time.Timer)
if tm.Reset(d) {
panic("Received an active timer from the pool!")
}
return tm
}
// ReleaseTimer to pool.
func ReleaseTimer(tm *time.Timer) {
if !tm.Stop() {
// Collect possibly added time from the channel
// If timer has been stopped and nobody collected its value.
select {
case <-tm.C:
default:
}
}
timerPool.Put(tm)
}