fix reactor channel blocking
This commit is contained in:
parent
5c03adbded
commit
dc11b5c55e
@ -7,6 +7,10 @@ import (
|
||||
|
||||
var logger = ethlog.NewLogger("REACTOR")
|
||||
|
||||
const (
|
||||
eventBufferSize int = 10
|
||||
)
|
||||
|
||||
type EventHandler struct {
|
||||
lock sync.RWMutex
|
||||
name string
|
||||
@ -21,7 +25,7 @@ func (e *EventHandler) Post(event Event) {
|
||||
|
||||
// if we want to preserve order pushing to subscibed channels
|
||||
// dispatching should be syncrounous
|
||||
// this means if subscribed event channel is blocked (closed or has fixed capacity)
|
||||
// this means if subscribed event channel is blocked
|
||||
// the reactor dispatch will be blocked, so we need to mitigate by skipping
|
||||
// rogue blocking subscribers
|
||||
for i, ch := range e.chans {
|
||||
@ -66,8 +70,7 @@ type ReactorEngine struct {
|
||||
lock sync.RWMutex
|
||||
eventChannel chan Event
|
||||
eventHandlers map[string]*EventHandler
|
||||
quit chan bool
|
||||
shutdownChannel chan bool
|
||||
quit chan chan error
|
||||
running bool
|
||||
drained chan bool
|
||||
}
|
||||
@ -75,10 +78,9 @@ type ReactorEngine struct {
|
||||
func New() *ReactorEngine {
|
||||
return &ReactorEngine{
|
||||
eventHandlers: make(map[string]*EventHandler),
|
||||
eventChannel: make(chan Event),
|
||||
quit: make(chan bool, 1),
|
||||
eventChannel: make(chan Event, eventBufferSize),
|
||||
quit: make(chan chan error, 1),
|
||||
drained: make(chan bool, 1),
|
||||
shutdownChannel: make(chan bool, 1),
|
||||
}
|
||||
}
|
||||
|
||||
@ -87,24 +89,22 @@ func (reactor *ReactorEngine) Start() {
|
||||
defer reactor.lock.Unlock()
|
||||
if !reactor.running {
|
||||
go func() {
|
||||
out:
|
||||
for {
|
||||
select {
|
||||
case <-reactor.quit:
|
||||
break out
|
||||
case event := <-reactor.eventChannel:
|
||||
// needs to be called syncronously to keep order of events
|
||||
reactor.dispatch(event)
|
||||
// case reactor.drained <- true:
|
||||
default:
|
||||
reactor.drained <- true // blocking till message is coming in
|
||||
}
|
||||
}
|
||||
case status := <-reactor.quit:
|
||||
reactor.lock.Lock()
|
||||
defer reactor.lock.Unlock()
|
||||
reactor.running = false
|
||||
logger.Infoln("stopped")
|
||||
close(reactor.shutdownChannel)
|
||||
status <- nil
|
||||
return
|
||||
case event := <-reactor.eventChannel:
|
||||
// needs to be called syncronously to keep order of events
|
||||
reactor.dispatch(event)
|
||||
default:
|
||||
reactor.drained <- true // blocking till message is coming in
|
||||
}
|
||||
}
|
||||
}()
|
||||
reactor.running = true
|
||||
logger.Infoln("started")
|
||||
@ -112,15 +112,15 @@ func (reactor *ReactorEngine) Start() {
|
||||
}
|
||||
|
||||
func (reactor *ReactorEngine) Stop() {
|
||||
reactor.lock.RLock()
|
||||
if reactor.running {
|
||||
reactor.quit <- true
|
||||
status := make(chan error)
|
||||
reactor.quit <- status
|
||||
select {
|
||||
case <-reactor.drained:
|
||||
default:
|
||||
}
|
||||
<-status
|
||||
}
|
||||
reactor.lock.RUnlock()
|
||||
<-reactor.shutdownChannel
|
||||
}
|
||||
|
||||
func (reactor *ReactorEngine) Flush() {
|
||||
@ -165,6 +165,7 @@ func (reactor *ReactorEngine) Post(event string, resource interface{}) {
|
||||
reactor.eventChannel <- Event{Resource: resource, Name: event}
|
||||
select {
|
||||
case <-reactor.drained:
|
||||
default:
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user