timer.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package utils
  2. import (
  3. "math"
  4. "time"
  5. )
  6. // A Timer wrapper that behaves correctly when resetting
  7. type Timer struct {
  8. t *time.Timer
  9. read bool
  10. deadline time.Time
  11. }
  12. // NewTimer creates a new timer that is not set
  13. func NewTimer() *Timer {
  14. return &Timer{t: time.NewTimer(time.Duration(math.MaxInt64))}
  15. }
  16. // Chan returns the channel of the wrapped timer
  17. func (t *Timer) Chan() <-chan time.Time {
  18. return t.t.C
  19. }
  20. // Reset the timer, no matter whether the value was read or not
  21. func (t *Timer) Reset(deadline time.Time) {
  22. if deadline.Equal(t.deadline) && !t.read {
  23. // No need to reset the timer
  24. return
  25. }
  26. // We need to drain the timer if the value from its channel was not read yet.
  27. // See https://groups.google.com/forum/#!topic/golang-dev/c9UUfASVPoU
  28. if !t.t.Stop() && !t.read {
  29. <-t.t.C
  30. }
  31. if !deadline.IsZero() {
  32. t.t.Reset(time.Until(deadline))
  33. }
  34. t.read = false
  35. t.deadline = deadline
  36. }
  37. // SetRead should be called after the value from the chan was read
  38. func (t *Timer) SetRead() {
  39. t.read = true
  40. }