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.

132 lines
3.9 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  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. Chan() <-chan timeoutInfo // on which to receive a timeout
  18. ScheduleTimeout(ti timeoutInfo) // reset the timer
  19. }
  20. // timeoutTicker wraps time.Timer,
  21. // scheduling timeouts only for greater height/round/step
  22. // than what it's already seen.
  23. // Timeouts are scheduled along the tickChan,
  24. // and fired on the tockChan.
  25. type timeoutTicker struct {
  26. service.BaseService
  27. timer *time.Timer
  28. tickChan chan timeoutInfo // for scheduling timeouts
  29. tockChan chan timeoutInfo // for notifying about them
  30. }
  31. // NewTimeoutTicker returns a new TimeoutTicker.
  32. func NewTimeoutTicker(logger log.Logger) TimeoutTicker {
  33. tt := &timeoutTicker{
  34. timer: time.NewTimer(0),
  35. tickChan: make(chan timeoutInfo, tickTockBufferSize),
  36. tockChan: make(chan timeoutInfo, tickTockBufferSize),
  37. }
  38. tt.BaseService = *service.NewBaseService(logger, "TimeoutTicker", tt)
  39. tt.stopTimer() // don't want to fire until the first scheduled timeout
  40. return tt
  41. }
  42. // OnStart implements service.Service. It starts the timeout routine.
  43. func (t *timeoutTicker) OnStart(gctx context.Context) error {
  44. go t.timeoutRoutine()
  45. return nil
  46. }
  47. // OnStop implements service.Service. It stops the timeout routine.
  48. func (t *timeoutTicker) OnStop() {
  49. t.BaseService.OnStop()
  50. t.stopTimer()
  51. }
  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() {
  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) { t.tockChan <- toi }(ti)
  110. case <-t.Quit():
  111. return
  112. }
  113. }
  114. }