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.

137 lines
4.0 KiB

  1. package consensus
  2. import (
  3. "context"
  4. "time"
  5. "github.com/tendermint/tendermint/libs/log"
  6. "github.com/tendermint/tendermint/libs/service"
  7. )
  8. var (
  9. tickTockBufferSize = 10
  10. )
  11. // TimeoutTicker is a timer that schedules timeouts
  12. // conditional on the height/round/step in the timeoutInfo.
  13. // The timeoutInfo.Duration may be non-positive.
  14. type TimeoutTicker interface {
  15. Start(context.Context) error
  16. Stop() error
  17. IsRunning() bool
  18. Chan() <-chan timeoutInfo // on which to receive a timeout
  19. ScheduleTimeout(ti timeoutInfo) // reset the timer
  20. }
  21. // timeoutTicker wraps time.Timer,
  22. // scheduling timeouts only for greater height/round/step
  23. // than what it's already seen.
  24. // Timeouts are scheduled along the tickChan,
  25. // and fired on the tockChan.
  26. type timeoutTicker struct {
  27. service.BaseService
  28. logger log.Logger
  29. timer *time.Timer
  30. tickChan chan timeoutInfo // for scheduling timeouts
  31. tockChan chan timeoutInfo // for notifying about them
  32. }
  33. // NewTimeoutTicker returns a new TimeoutTicker.
  34. func NewTimeoutTicker(logger log.Logger) TimeoutTicker {
  35. tt := &timeoutTicker{
  36. logger: logger,
  37. timer: time.NewTimer(0),
  38. tickChan: make(chan timeoutInfo, tickTockBufferSize),
  39. tockChan: make(chan timeoutInfo, tickTockBufferSize),
  40. }
  41. tt.BaseService = *service.NewBaseService(logger, "TimeoutTicker", tt)
  42. tt.stopTimer() // don't want to fire until the first scheduled timeout
  43. return tt
  44. }
  45. // OnStart implements service.Service. It starts the timeout routine.
  46. func (t *timeoutTicker) OnStart(ctx context.Context) error {
  47. go t.timeoutRoutine(ctx)
  48. return nil
  49. }
  50. // OnStop implements service.Service. It stops the timeout routine.
  51. func (t *timeoutTicker) OnStop() { t.stopTimer() }
  52. // Chan returns a channel on which timeouts are sent.
  53. func (t *timeoutTicker) Chan() <-chan timeoutInfo {
  54. return t.tockChan
  55. }
  56. // ScheduleTimeout schedules a new timeout by sending on the internal tickChan.
  57. // The timeoutRoutine is always available to read from tickChan, so this won't block.
  58. // The scheduling may fail if the timeoutRoutine has already scheduled a timeout for a later height/round/step.
  59. func (t *timeoutTicker) ScheduleTimeout(ti timeoutInfo) {
  60. t.tickChan <- ti
  61. }
  62. //-------------------------------------------------------------
  63. // stop the timer and drain if necessary
  64. func (t *timeoutTicker) stopTimer() {
  65. // Stop() returns false if it was already fired or was stopped
  66. if !t.timer.Stop() {
  67. select {
  68. case <-t.timer.C:
  69. default:
  70. t.logger.Debug("Timer already stopped")
  71. }
  72. }
  73. }
  74. // send on tickChan to start a new timer.
  75. // timers are interupted and replaced by new ticks from later steps
  76. // timeouts of 0 on the tickChan will be immediately relayed to the tockChan
  77. func (t *timeoutTicker) timeoutRoutine(ctx context.Context) {
  78. t.logger.Debug("Starting timeout routine")
  79. var ti timeoutInfo
  80. for {
  81. select {
  82. case newti := <-t.tickChan:
  83. t.logger.Debug("Received tick", "old_ti", ti, "new_ti", newti)
  84. // ignore tickers for old height/round/step
  85. if newti.Height < ti.Height {
  86. continue
  87. } else if newti.Height == ti.Height {
  88. if newti.Round < ti.Round {
  89. continue
  90. } else if newti.Round == ti.Round {
  91. if ti.Step > 0 && newti.Step <= ti.Step {
  92. continue
  93. }
  94. }
  95. }
  96. // stop the last timer
  97. t.stopTimer()
  98. // update timeoutInfo and reset timer
  99. // NOTE time.Timer allows duration to be non-positive
  100. ti = newti
  101. t.timer.Reset(ti.Duration)
  102. t.logger.Debug("Scheduled timeout", "dur", ti.Duration, "height", ti.Height, "round", ti.Round, "step", ti.Step)
  103. case <-t.timer.C:
  104. t.logger.Info("Timed out", "dur", ti.Duration, "height", ti.Height, "round", ti.Round, "step", ti.Step)
  105. // go routine here guarantees timeoutRoutine doesn't block.
  106. // Determinism comes from playback in the receiveRoutine.
  107. // We can eliminate it by merging the timeoutRoutine into receiveRoutine
  108. // and managing the timeouts ourselves with a millisecond ticker
  109. go func(toi timeoutInfo) {
  110. select {
  111. case t.tockChan <- toi:
  112. case <-ctx.Done():
  113. }
  114. }(ti)
  115. case <-ctx.Done():
  116. return
  117. }
  118. }
  119. }