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.

118 lines
2.5 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package v2
  2. import (
  3. "fmt"
  4. "time"
  5. "github.com/tendermint/tendermint/libs/log"
  6. )
  7. type timeCheck struct {
  8. priorityHigh
  9. time time.Time
  10. }
  11. func schedulerHandle(event Event) (Event, error) {
  12. if _, ok := event.(timeCheck); ok {
  13. fmt.Println("scheduler handle timeCheck")
  14. }
  15. return noOp, nil
  16. }
  17. func processorHandle(event Event) (Event, error) {
  18. if _, ok := event.(timeCheck); ok {
  19. fmt.Println("processor handle timeCheck")
  20. }
  21. return noOp, nil
  22. }
  23. type Reactor struct {
  24. events chan Event
  25. stopDemux chan struct{}
  26. scheduler *Routine
  27. processor *Routine
  28. ticker *time.Ticker
  29. logger log.Logger
  30. }
  31. func NewReactor(bufferSize int) *Reactor {
  32. return &Reactor{
  33. events: make(chan Event, bufferSize),
  34. stopDemux: make(chan struct{}),
  35. scheduler: newRoutine("scheduler", schedulerHandle, bufferSize),
  36. processor: newRoutine("processor", processorHandle, bufferSize),
  37. ticker: time.NewTicker(1 * time.Second),
  38. logger: log.NewNopLogger(),
  39. }
  40. }
  41. // nolint:unused
  42. func (r *Reactor) setLogger(logger log.Logger) {
  43. r.logger = logger
  44. r.scheduler.setLogger(logger)
  45. r.processor.setLogger(logger)
  46. }
  47. func (r *Reactor) Start() {
  48. go r.scheduler.start()
  49. go r.processor.start()
  50. go r.demux()
  51. <-r.scheduler.ready()
  52. <-r.processor.ready()
  53. go func() {
  54. for t := range r.ticker.C {
  55. r.events <- timeCheck{time: t}
  56. }
  57. }()
  58. }
  59. // XXX: How to make this deterministic?
  60. // XXX: Would it be possible here to provide some kind of type safety for the types
  61. // of events that each routine can produce and consume?
  62. func (r *Reactor) demux() {
  63. for {
  64. select {
  65. case event := <-r.events:
  66. // XXX: check for backpressure
  67. r.scheduler.send(event)
  68. r.processor.send(event)
  69. case <-r.stopDemux:
  70. r.logger.Info("demuxing stopped")
  71. return
  72. case event := <-r.scheduler.next():
  73. r.processor.send(event)
  74. case event := <-r.processor.next():
  75. r.scheduler.send(event)
  76. case err := <-r.scheduler.final():
  77. r.logger.Info(fmt.Sprintf("scheduler final %s", err))
  78. case err := <-r.processor.final():
  79. r.logger.Info(fmt.Sprintf("processor final %s", err))
  80. // XXX: switch to consensus
  81. }
  82. }
  83. }
  84. func (r *Reactor) Stop() {
  85. r.logger.Info("reactor stopping")
  86. r.ticker.Stop()
  87. r.scheduler.stop()
  88. r.processor.stop()
  89. close(r.stopDemux)
  90. close(r.events)
  91. r.logger.Info("reactor stopped")
  92. }
  93. func (r *Reactor) Receive(event Event) {
  94. // XXX: decode and serialize write events
  95. // TODO: backpressure
  96. r.events <- event
  97. }
  98. func (r *Reactor) AddPeer() {
  99. // TODO: add peer event and send to demuxer
  100. }