tendermint/blockchain/v2/routines.go

187 lines
3.8 KiB
Go
Raw Normal View History

2019-07-25 16:02:47 +02:00
package v2
import (
"fmt"
"time"
)
/*
2019-07-29 08:45:59 +02:00
TODO:
* Look at refactoring routines
* single struct, paramterize the handle function
* introduce an sendError and seperate error channel
* How could this be tested?
* Ensure Start/Stopping
* Ensure all messages sent are processed
* Ensure that errors can be processed depsite outstanding messages
2019-07-25 16:02:47 +02:00
*/
type testEvent struct {
msg string
time time.Time
}
type testEventTwo struct {
msg string
}
type stopEvent struct{}
2019-07-27 00:25:32 +02:00
type timeCheck struct {
time time.Time
}
2019-07-25 16:02:47 +02:00
2019-07-29 08:45:59 +02:00
type handleFunc = func(event Event) Events
2019-07-27 10:58:58 +02:00
2019-07-29 08:45:59 +02:00
// Routine
type Routine struct {
name string
2019-07-27 11:38:37 +02:00
input chan Event
output chan Event
stopped chan struct{}
2019-07-29 08:45:59 +02:00
handle handleFunc
2019-07-25 16:02:47 +02:00
}
2019-07-29 08:45:59 +02:00
func newRoutine(name string, output chan Event, handleFunc handleFunc) *Routine {
return &Routine{
name: name,
input: make(chan Event, 1),
output: output,
stopped: make(chan struct{}, 1),
2019-07-29 08:45:59 +02:00
handle: handleFunc,
2019-07-27 10:58:58 +02:00
}
}
2019-07-29 08:45:59 +02:00
// XXX: what about error handling?
// we need an additional error channel here which will ensure
// errors get processed as soon as possible
// XXX: what about state here, we need per routine state
func (rt *Routine) run() {
fmt.Printf("%s: run\n", rt.name)
2019-07-25 16:02:47 +02:00
for {
2019-07-29 08:45:59 +02:00
iEvent, ok := <-rt.input
2019-07-27 11:38:37 +02:00
if !ok {
2019-07-29 08:45:59 +02:00
fmt.Printf("%s: stopping\n", rt.name)
rt.stopped <- struct{}{}
2019-07-25 16:02:47 +02:00
break
}
2019-07-29 08:45:59 +02:00
oEvents := rt.handle(iEvent)
2019-07-27 00:25:32 +02:00
for _, event := range oEvents {
2019-07-29 08:45:59 +02:00
rt.output <- event
2019-07-27 00:25:32 +02:00
}
2019-07-25 16:02:47 +02:00
}
}
2019-07-29 08:45:59 +02:00
func (rt *Routine) send(event Event) bool {
fmt.Printf("%s: send\n", rt.name)
2019-07-27 00:25:32 +02:00
select {
2019-07-29 08:45:59 +02:00
case rt.input <- event:
2019-07-27 00:25:32 +02:00
return true
default:
2019-07-29 08:45:59 +02:00
fmt.Printf("%s: channel was full\n", rt.name)
2019-07-27 00:25:32 +02:00
return false
}
}
2019-07-29 08:45:59 +02:00
func (rt *Routine) stop() {
fmt.Printf("%s: stop\n", rt.name)
close(rt.input)
<-rt.stopped
}
func schedulerHandle(event Event) Events {
2019-07-27 10:58:58 +02:00
switch event.(type) {
2019-07-27 00:25:32 +02:00
case timeCheck:
fmt.Println("scheduler handle timeCheck")
case testEvent:
fmt.Println("scheduler handle testEvent")
}
return Events{}
}
2019-07-29 08:45:59 +02:00
func processorHandle(event Event) Events {
2019-07-27 10:58:58 +02:00
switch event.(type) {
2019-07-27 00:25:32 +02:00
case timeCheck:
2019-07-27 10:58:58 +02:00
fmt.Println("processor handle timeCheck")
2019-07-27 00:25:32 +02:00
case testEvent:
fmt.Println("processor handle testEvent")
}
return Events{}
}
2019-07-29 08:45:59 +02:00
func genDemuxerHandle(scheduler *Routine, processor *Routine) handleFunc {
return func(event Event) Events {
received := scheduler.send(event)
2019-07-27 10:58:58 +02:00
if !received {
panic("couldn't send to scheduler")
}
2019-07-29 08:45:59 +02:00
received = processor.send(event)
2019-07-27 10:58:58 +02:00
if !received {
panic("couldn't send to the processor")
}
2019-07-25 16:02:47 +02:00
2019-07-29 08:45:59 +02:00
// XXX: think about emitting backpressure if !received
return Events{}
2019-07-27 10:58:58 +02:00
}
}
2019-07-27 00:25:32 +02:00
// reactor
2019-07-25 16:02:47 +02:00
type DummyReactor struct {
2019-07-27 11:38:37 +02:00
events chan Event
2019-07-29 08:45:59 +02:00
demuxer *Routine
scheduler *Routine
processor *Routine
2019-07-27 11:38:37 +02:00
ticker *time.Ticker
tickerStopped chan struct{}
2019-07-25 16:02:47 +02:00
}
func (dr *DummyReactor) Start() {
2019-07-27 00:25:32 +02:00
bufferSize := 10
events := make(chan Event, bufferSize)
2019-07-29 08:45:59 +02:00
dr.scheduler = newRoutine("scheduler", events, schedulerHandle)
dr.processor = newRoutine("processor", events, processorHandle)
demuxerHandle := genDemuxerHandle(dr.scheduler, dr.processor)
dr.demuxer = newRoutine("demuxer", events, demuxerHandle)
2019-07-27 11:38:37 +02:00
dr.tickerStopped = make(chan struct{})
2019-07-27 00:25:32 +02:00
2019-07-27 11:38:37 +02:00
go dr.scheduler.run()
go dr.processor.run()
2019-07-27 00:25:32 +02:00
go dr.demuxer.run()
2019-07-27 11:38:37 +02:00
2019-07-27 10:58:58 +02:00
go func() {
2019-07-27 11:38:37 +02:00
ticker := time.NewTicker(1 * time.Second)
for {
select {
case <-ticker.C:
dr.demuxer.send(timeCheck{})
case <-dr.tickerStopped:
fmt.Println("ticker stopped")
return
}
2019-07-27 10:58:58 +02:00
}
}()
2019-07-25 16:02:47 +02:00
}
2019-07-27 00:25:32 +02:00
func (dr *DummyReactor) Stop() {
2019-07-27 11:38:37 +02:00
fmt.Println("reactor stopping")
2019-07-27 10:58:58 +02:00
// this should be synchronous
2019-07-27 11:38:37 +02:00
dr.tickerStopped <- struct{}{}
dr.demuxer.stop()
2019-07-27 11:38:37 +02:00
dr.scheduler.stop()
dr.processor.stop()
fmt.Println("reactor stopped")
2019-07-27 00:25:32 +02:00
}
2019-07-25 16:02:47 +02:00
2019-07-27 00:25:32 +02:00
func (dr *DummyReactor) Receive(event Event) {
2019-07-27 10:58:58 +02:00
fmt.Println("receive event")
2019-07-27 00:25:32 +02:00
sent := dr.demuxer.send(event)
if !sent {
panic("demuxer is full")
}
}
2019-07-25 16:02:47 +02:00
2019-07-27 00:25:32 +02:00
func (dr *DummyReactor) AddPeer() {
// TODO: add peer event and send to demuxer
}