aboutsummaryrefslogtreecommitdiff
path: root/timer.go
diff options
context:
space:
mode:
authorGravatar xuecai <bluesnow1st@sina.com> 2019-02-04 03:16:39 +0800
committerGravatar Erik Dubbelboer <erik@dubbelboer.com> 2019-02-03 19:16:39 +0000
commit627d63dd25bce25772a610342afd632238463c33 (patch)
tree22cbf9f70eb9310f7d07e7d829789bbe317396be /timer.go
parentMake InmemoryListener.Dial return when the connection is accepted (diff)
downloadfasthttp-627d63dd25bce25772a610342afd632238463c33.tar.gz
fasthttp-627d63dd25bce25772a610342afd632238463c33.tar.bz2
fasthttp-627d63dd25bce25772a610342afd632238463c33.zip
change timer to public api #525 (#527)
* change acquireTimer and releaseTimer to public api
Diffstat (limited to 'timer.go')
-rw-r--r--timer.go14
1 files changed, 12 insertions, 2 deletions
diff --git a/timer.go b/timer.go
index bb12acb..4e91938 100644
--- a/timer.go
+++ b/timer.go
@@ -26,7 +26,12 @@ func stopTimer(t *time.Timer) {
}
}
-func acquireTimer(timeout time.Duration) *time.Timer {
+// AcquireTimer returns a time.Timer from the pool and updates it to
+// send the current time on its channel after at least timeout.
+//
+// The returned Timer may be returned to the pool with ReleaseTimer
+// when no longer needed. This allows reducing GC load.
+func AcquireTimer(timeout time.Duration) *time.Timer {
v := timerPool.Get()
if v == nil {
return time.NewTimer(timeout)
@@ -36,7 +41,12 @@ func acquireTimer(timeout time.Duration) *time.Timer {
return t
}
-func releaseTimer(t *time.Timer) {
+// ReleaseTimer returns the time.Timer acquired via AcquireTimer to the pool
+// and prevents the Timer from firing.
+//
+// Do not access the released time.Timer or read from it's channel otherwise
+// data races may occur.
+func ReleaseTimer(t *time.Timer) {
stopTimer(t)
timerPool.Put(t)
}