Files
VictoriaMetrics/lib/timerpool/timerpool.go
2025-10-02 08:43:38 +03:00

29 lines
482 B
Go

package timerpool
import (
"sync"
"time"
)
// Get returns a timer for the given duration d from the pool.
//
// Return back the timer to the pool with Put.
func Get(d time.Duration) *time.Timer {
if v := timerPool.Get(); v != nil {
t := v.(*time.Timer)
t.Reset(d)
return t
}
return time.NewTimer(d)
}
// Put returns t to the pool.
//
// t cannot be accessed after returning to the pool.
func Put(t *time.Timer) {
t.Stop()
timerPool.Put(t)
}
var timerPool sync.Pool