Files
timmy-config/go/pkg/mod/github.com/savsgio/gotils@v0.0.0-20240704082632-aef3928b8a38/time/ticker.go
2026-03-31 20:02:01 +00:00

32 lines
482 B
Go

package time
import (
"sync"
"time"
)
var tickerPool = sync.Pool{}
// AcquireTicker returns a ticker from the pool if possible.
func AcquireTicker(d time.Duration) *time.Ticker {
v := tickerPool.Get()
if v == nil {
return time.NewTicker(d)
}
t, ok := v.(*time.Ticker)
if !ok {
panic("unexpected type of time.Ticker")
}
t.Reset(d)
return t
}
// ReleaseTicker returns a ticker into the pool.
func ReleaseTicker(tm *time.Ticker) {
tm.Stop()
tickerPool.Put(tm)
}