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.

117 lines
2.4 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: Would it be possible here to provide some kind of type safety for the types
  60. // of events that each routine can produce and consume?
  61. func (r *Reactor) demux() {
  62. for {
  63. select {
  64. case event := <-r.events:
  65. // XXX: check for backpressure
  66. r.scheduler.send(event)
  67. r.processor.send(event)
  68. case <-r.stopDemux:
  69. r.logger.Info("demuxing stopped")
  70. return
  71. case event := <-r.scheduler.next():
  72. r.processor.send(event)
  73. case event := <-r.processor.next():
  74. r.scheduler.send(event)
  75. case err := <-r.scheduler.final():
  76. r.logger.Info(fmt.Sprintf("scheduler final %s", err))
  77. case err := <-r.processor.final():
  78. r.logger.Info(fmt.Sprintf("processor final %s", err))
  79. // XXX: switch to consensus
  80. }
  81. }
  82. }
  83. func (r *Reactor) Stop() {
  84. r.logger.Info("reactor stopping")
  85. r.ticker.Stop()
  86. r.scheduler.stop()
  87. r.processor.stop()
  88. close(r.stopDemux)
  89. close(r.events)
  90. r.logger.Info("reactor stopped")
  91. }
  92. func (r *Reactor) Receive(event Event) {
  93. // XXX: decode and serialize write events
  94. // TODO: backpressure
  95. r.events <- event
  96. }
  97. func (r *Reactor) AddPeer() {
  98. // TODO: add peer event and send to demuxer
  99. }