mirror of
https://github.com/valyala/fasthttp.git
synced 2026-06-13 15:46:49 +03:00
35 lines
549 B
Go
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)
|
|
}
|