timer.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package signal
  2. import (
  3. "context"
  4. "time"
  5. )
  6. type ActivityUpdater interface {
  7. Update()
  8. }
  9. type ActivityTimer struct {
  10. updated chan bool
  11. timeout chan time.Duration
  12. ctx context.Context
  13. cancel context.CancelFunc
  14. }
  15. func (t *ActivityTimer) Update() {
  16. select {
  17. case t.updated <- true:
  18. default:
  19. }
  20. }
  21. func (t *ActivityTimer) SetTimeout(timeout time.Duration) {
  22. t.timeout <- timeout
  23. }
  24. func (t *ActivityTimer) run() {
  25. ticker := time.NewTicker(<-t.timeout)
  26. defer func() {
  27. ticker.Stop()
  28. }()
  29. for {
  30. select {
  31. case <-ticker.C:
  32. case <-t.ctx.Done():
  33. return
  34. case timeout := <-t.timeout:
  35. if timeout == 0 {
  36. t.cancel()
  37. return
  38. }
  39. ticker.Stop()
  40. ticker = time.NewTicker(timeout)
  41. }
  42. select {
  43. case <-t.updated:
  44. // Updated keep waiting.
  45. default:
  46. t.cancel()
  47. return
  48. }
  49. }
  50. }
  51. func CancelAfterInactivity(ctx context.Context, cancel context.CancelFunc, timeout time.Duration) *ActivityTimer {
  52. timer := &ActivityTimer{
  53. ctx: ctx,
  54. cancel: cancel,
  55. timeout: make(chan time.Duration, 1),
  56. updated: make(chan bool, 1),
  57. }
  58. timer.timeout <- timeout
  59. go timer.run()
  60. return timer
  61. }