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.

126 lines
3.4 KiB

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