Skip to content

Instantly share code, notes, and snippets.

@marxangels
Created March 28, 2020 16:28
Show Gist options
  • Save marxangels/f8d990f8f9303a97d8b5dda19e737b9d to your computer and use it in GitHub Desktop.
Save marxangels/f8d990f8f9303a97d8b5dda19e737b9d to your computer and use it in GitHub Desktop.
[Go] time.Timer + sync.Pool
package foo
import (
"sync"
"time"
)
var timerPool sync.Pool
func PoolTimerGet(d time.Duration) *time.Timer {
if value := timerPool.Get(); nil == value {
return time.NewTimer(d)
} else {
timer := value.(*time.Timer)
timer.Reset(d)
return timer
}
}
func PoolTimerPut(timer *time.Timer, stop bool) {
if stop && !timer.Stop() {
<-timer.C
}
timerPool.Put(timer)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment