diff options
author | Aliaksandr Valialkin <valyala@gmail.com> | 2015-10-19 01:21:09 +0300 |
---|---|---|
committer | Aliaksandr Valialkin <valyala@gmail.com> | 2015-10-19 01:21:09 +0300 |
commit | a049630bca2089ac5ade886b2ea5554f65f065d3 (patch) | |
tree | d3156cf552338e101108988bce94cd6438742d38 /timer.go | |
download | fasthttp-a049630bca2089ac5ade886b2ea5554f65f065d3.tar.gz fasthttp-a049630bca2089ac5ade886b2ea5554f65f065d3.tar.bz2 fasthttp-a049630bca2089ac5ade886b2ea5554f65f065d3.zip |
initial commit
Diffstat (limited to 'timer.go')
-rw-r--r-- | timer.go | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/timer.go b/timer.go new file mode 100644 index 0000000..8227c7d --- /dev/null +++ b/timer.go @@ -0,0 +1,34 @@ +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) +} |