2019-05-28 17:17:19 +03:00
|
|
|
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)
|
2026-02-25 19:45:12 +01:00
|
|
|
// any receive from t.C after Reset has returned is guaranteed not
|
|
|
|
|
// to receive a time value corresponding to the previous timer settings
|
|
|
|
|
t.Reset(d)
|
2019-05-28 17:17:19 +03:00
|
|
|
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) {
|
2025-09-16 10:55:33 +03:00
|
|
|
t.Stop()
|
2019-05-28 17:17:19 +03:00
|
|
|
timerPool.Put(t)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var timerPool sync.Pool
|