package client import ( "sync" "time" ) var timerPool sync.Pool func retainTimer(timeout time.Duration) *time.Timer { tv := timerPool.Get() if tv == nil { return time.NewTimer(timeout) } t := tv.(*time.Timer) if t.Reset(timeout) { panic("BUG: Active timer trapped into retainTimer()") } return t } func releaseTimer(t *time.Timer) { if !t.Stop() { // Collect possibly added time from the channel // if timer has been stopped and nobody collected its' value. select { case <-t.C: default: } } timerPool.Put(t) }