Files
fasthttp/timer.go
T
Aliaksandr Valialkin a049630bca initial commit
2015-10-19 01:21:09 +03:00

35 lines
549 B
Go

package fasthttp
import (
"sync"
"time"
)
var timerPool sync.Pool
func acquireTimer(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 AcquireTimer()")
}
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)
}