tendermint/blockchain/v2/routine.go

145 lines
3.0 KiB
Go
Raw Normal View History

2019-07-25 16:02:47 +02:00
package v2
import (
"fmt"
"sync/atomic"
2019-07-25 16:02:47 +02:00
)
2019-07-30 12:17:51 +02:00
// TODO
// logging
// metrics
type handleFunc = func(event Event) (Events, error)
2019-07-27 10:58:58 +02:00
// XXX: This should comply with the BaseService interface
2019-07-29 08:45:59 +02:00
// Routine
type Routine struct {
name string
input chan Event
errors chan error
output chan Event
stopped chan struct{}
finished chan error
running *uint32
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),
errors: make(chan error, 1),
output: output,
stopped: make(chan struct{}, 1),
finished: make(chan error, 1),
running: new(uint32),
handle: handleFunc,
2019-07-27 10:58:58 +02:00
}
}
2019-07-29 08:45:59 +02:00
func (rt *Routine) run() {
fmt.Printf("%s: run\n", rt.name)
starting := atomic.CompareAndSwapUint32(rt.running, uint32(0), uint32(1))
if !starting {
panic("Routine has already started")
}
errorsDrained := false
2019-07-25 16:02:47 +02:00
for {
if !rt.isRunning() {
break
}
2019-07-29 12:40:59 +02:00
select {
case iEvent, ok := <-rt.input:
if !ok {
if !errorsDrained {
continue // wait for errors to be drainned
}
2019-07-29 12:40:59 +02:00
fmt.Printf("%s: stopping\n", rt.name)
rt.stopped <- struct{}{}
2019-07-29 12:47:43 +02:00
return
2019-07-29 12:40:59 +02:00
}
oEvents, err := rt.handle(iEvent)
if err != nil {
rt.terminate(err)
return
}
2019-07-29 12:40:59 +02:00
fmt.Printf("%s handled %d events\n", rt.name, len(oEvents))
for _, event := range oEvents {
fmt.Println("writting back to output")
2019-07-29 12:40:59 +02:00
rt.output <- event
}
case iEvent, ok := <-rt.errors:
if !ok {
fmt.Printf("%s: errors closed\n", rt.name)
errorsDrained = true
2019-07-29 12:40:59 +02:00
continue
}
// rename flush?
oEvents, err := rt.handle(iEvent)
if err != nil {
rt.terminate(err)
return
}
2019-07-29 12:40:59 +02:00
fmt.Printf("%s handled %d events from errors\n", rt.name, len(oEvents))
for _, event := range oEvents {
rt.output <- event
}
2019-07-27 00:25:32 +02:00
}
2019-07-25 16:02:47 +02:00
}
}
func (rt *Routine) feedback() {
for event := range rt.output {
rt.send(event)
}
}
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)
// XXX: What if we arn't running? this will panic
if err, ok := event.(error); ok {
2019-07-29 12:40:59 +02:00
select {
case rt.errors <- err:
return true
default:
fmt.Printf("%s: errors channel was full\n", rt.name)
return false
}
} else {
select {
case rt.input <- event:
return true
default:
fmt.Printf("%s: channel was full\n", rt.name)
return false
}
2019-07-27 00:25:32 +02:00
}
}
func (rt *Routine) isRunning() bool {
return atomic.LoadUint32(rt.running) == 1
}
// rename flush?
2019-07-29 08:45:59 +02:00
func (rt *Routine) stop() {
fmt.Printf("%s: stop\n", rt.name)
close(rt.input)
close(rt.errors)
<-rt.stopped // stuck here
rt.terminate(fmt.Errorf("routine stopped"))
}
func (rt *Routine) terminate(reason error) {
stopped := atomic.CompareAndSwapUint32(rt.running, uint32(1), uint32(0))
if !stopped {
panic("called stop but already stopped")
}
rt.finished <- reason
2019-07-29 08:45:59 +02:00
}
// XXX: this should probably produced the finished
// channel and let the caller deicde how long to wait
func (rt *Routine) wait() error {
return <-rt.finished
}