Last change
on this file since 145 was 145, checked in by Izuru Yakumo, 22 months ago |
Updated the Makefile and vendored depedencies
Signed-off-by: Izuru Yakumo <yakumo.izuru@…>
|
File size:
1.2 KB
|
Line | |
---|
1 | package fasthttp
|
---|
2 |
|
---|
3 | import (
|
---|
4 | "sync"
|
---|
5 | "time"
|
---|
6 | )
|
---|
7 |
|
---|
8 | func initTimer(t *time.Timer, timeout time.Duration) *time.Timer {
|
---|
9 | if t == nil {
|
---|
10 | return time.NewTimer(timeout)
|
---|
11 | }
|
---|
12 | if t.Reset(timeout) {
|
---|
13 | panic("BUG: active timer trapped into initTimer()")
|
---|
14 | }
|
---|
15 | return t
|
---|
16 | }
|
---|
17 |
|
---|
18 | func stopTimer(t *time.Timer) {
|
---|
19 | if !t.Stop() {
|
---|
20 | // Collect possibly added time from the channel
|
---|
21 | // if timer has been stopped and nobody collected its' value.
|
---|
22 | select {
|
---|
23 | case <-t.C:
|
---|
24 | default:
|
---|
25 | }
|
---|
26 | }
|
---|
27 | }
|
---|
28 |
|
---|
29 | // AcquireTimer returns a time.Timer from the pool and updates it to
|
---|
30 | // send the current time on its channel after at least timeout.
|
---|
31 | //
|
---|
32 | // The returned Timer may be returned to the pool with ReleaseTimer
|
---|
33 | // when no longer needed. This allows reducing GC load.
|
---|
34 | func AcquireTimer(timeout time.Duration) *time.Timer {
|
---|
35 | v := timerPool.Get()
|
---|
36 | if v == nil {
|
---|
37 | return time.NewTimer(timeout)
|
---|
38 | }
|
---|
39 | t := v.(*time.Timer)
|
---|
40 | initTimer(t, timeout)
|
---|
41 | return t
|
---|
42 | }
|
---|
43 |
|
---|
44 | // ReleaseTimer returns the time.Timer acquired via AcquireTimer to the pool
|
---|
45 | // and prevents the Timer from firing.
|
---|
46 | //
|
---|
47 | // Do not access the released time.Timer or read from it's channel otherwise
|
---|
48 | // data races may occur.
|
---|
49 | func ReleaseTimer(t *time.Timer) {
|
---|
50 | stopTimer(t)
|
---|
51 | timerPool.Put(t)
|
---|
52 | }
|
---|
53 |
|
---|
54 | var timerPool sync.Pool
|
---|
Note:
See
TracBrowser
for help on using the repository browser.