snapshot of https://github.com/centrifugal/centrifugo.git (master, 2026-09-16; blobs > 131072B omitted)
This commit is contained in:
34
internal/timers/pool.go
Normal file
34
internal/timers/pool.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package timers
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
var timerPool sync.Pool
|
||||
|
||||
// AcquireTimer from pool.
|
||||
func AcquireTimer(d time.Duration) *time.Timer {
|
||||
v := timerPool.Get()
|
||||
if v == nil {
|
||||
return time.NewTimer(d)
|
||||
}
|
||||
tm := v.(*time.Timer)
|
||||
if tm.Reset(d) {
|
||||
panic("Received an active timer from the pool!")
|
||||
}
|
||||
return tm
|
||||
}
|
||||
|
||||
// ReleaseTimer to pool.
|
||||
func ReleaseTimer(tm *time.Timer) {
|
||||
if !tm.Stop() {
|
||||
// Collect possibly added time from the channel
|
||||
// If timer has been stopped and nobody collected its value.
|
||||
select {
|
||||
case <-tm.C:
|
||||
default:
|
||||
}
|
||||
}
|
||||
timerPool.Put(tm)
|
||||
}
|
||||
Reference in New Issue
Block a user