1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
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)
}
|