locking in event manager to fix automated test detected race
This commit is contained in:
parent
dd8ed97f90
commit
0126379436
|
@ -56,6 +56,7 @@ type manager struct {
|
||||||
subscribers map[Type][]Queue
|
subscribers map[Type][]Queue
|
||||||
events chan []byte
|
events chan []byte
|
||||||
mapMutex sync.Mutex
|
mapMutex sync.Mutex
|
||||||
|
chanMutex sync.Mutex
|
||||||
internal chan bool
|
internal chan bool
|
||||||
closed bool
|
closed bool
|
||||||
trace bool
|
trace bool
|
||||||
|
@ -97,6 +98,8 @@ func (em *manager) Subscribe(eventType Type, queue Queue) {
|
||||||
|
|
||||||
// Publish takes an Event and sends it to the internal eventBus where it is distributed to all Subscribers
|
// Publish takes an Event and sends it to the internal eventBus where it is distributed to all Subscribers
|
||||||
func (em *manager) Publish(event Event) {
|
func (em *manager) Publish(event Event) {
|
||||||
|
em.chanMutex.Lock()
|
||||||
|
defer em.chanMutex.Unlock()
|
||||||
if event.EventType != "" && !em.closed {
|
if event.EventType != "" && !em.closed {
|
||||||
|
|
||||||
// Debug Events for Tracing, locked behind an environment variable
|
// Debug Events for Tracing, locked behind an environment variable
|
||||||
|
@ -160,7 +163,9 @@ func (em *manager) eventBus() {
|
||||||
// Shutdown triggers, and waits for, the internal eventBus goroutine to finish
|
// Shutdown triggers, and waits for, the internal eventBus goroutine to finish
|
||||||
func (em *manager) Shutdown() {
|
func (em *manager) Shutdown() {
|
||||||
em.events <- []byte{}
|
em.events <- []byte{}
|
||||||
|
em.chanMutex.Lock()
|
||||||
em.closed = true
|
em.closed = true
|
||||||
|
em.chanMutex.Unlock()
|
||||||
// wait for eventBus to finish
|
// wait for eventBus to finish
|
||||||
<-em.internal
|
<-em.internal
|
||||||
close(em.events)
|
close(em.events)
|
||||||
|
|
Loading…
Reference in New Issue