locking in event manager to fix automated test detected race
continuous-integration/drone/push Build is passing Details
continuous-integration/drone/pr Build is passing Details

This commit is contained in:
Dan Ballard 2022-03-23 14:57:18 -07:00
parent dd8ed97f90
commit 0126379436
1 changed files with 5 additions and 0 deletions

View File

@ -56,6 +56,7 @@ type manager struct {
subscribers map[Type][]Queue
events chan []byte
mapMutex sync.Mutex
chanMutex sync.Mutex
internal chan bool
closed 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
func (em *manager) Publish(event Event) {
em.chanMutex.Lock()
defer em.chanMutex.Unlock()
if event.EventType != "" && !em.closed {
// 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
func (em *manager) Shutdown() {
em.events <- []byte{}
em.chanMutex.Lock()
em.closed = true
em.chanMutex.Unlock()
// wait for eventBus to finish
<-em.internal
close(em.events)