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.

129 lines
3.4 KiB

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