timer.go 538 B

12345678910111213141516171819202122232425262728293031
  1. package radix
  2. import (
  3. "sync"
  4. "time"
  5. )
  6. // global pool of *time.Timer's.
  7. var timerPool sync.Pool
  8. // get returns a timer that completes after the given duration.
  9. func getTimer(d time.Duration) *time.Timer {
  10. if t, _ := timerPool.Get().(*time.Timer); t != nil {
  11. t.Reset(d)
  12. return t
  13. }
  14. return time.NewTimer(d)
  15. }
  16. // putTimer pools the given timer. putTimer stops the timer and handles any left over data in the channel.
  17. func putTimer(t *time.Timer) {
  18. if !t.Stop() {
  19. select {
  20. case <-t.C:
  21. default:
  22. }
  23. }
  24. timerPool.Put(t)
  25. }