Created
March 28, 2020 16:28
-
-
Save marxangels/f8d990f8f9303a97d8b5dda19e737b9d to your computer and use it in GitHub Desktop.
[Go] time.Timer + sync.Pool
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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