You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
639 B

  1. package common
  2. import "time"
  3. /* RepeatTimer */
  4. type RepeatTimer struct {
  5. Ch chan struct{}
  6. quit chan struct{}
  7. dur time.Duration
  8. timer *time.Timer
  9. }
  10. func NewRepeatTimer(dur time.Duration) *RepeatTimer {
  11. var ch = make(chan struct{})
  12. var quit = make(chan struct{})
  13. var t = &RepeatTimer{Ch: ch, dur: dur, quit: quit}
  14. t.timer = time.AfterFunc(dur, t.fireHandler)
  15. return t
  16. }
  17. func (t *RepeatTimer) fireHandler() {
  18. select {
  19. case t.Ch <- struct{}{}:
  20. t.timer.Reset(t.dur)
  21. case <-t.quit:
  22. }
  23. }
  24. func (t *RepeatTimer) Reset() {
  25. t.timer.Reset(t.dur)
  26. }
  27. func (t *RepeatTimer) Stop() bool {
  28. close(t.quit)
  29. return t.timer.Stop()
  30. }