diff --git a/event/event_cache.go b/event/event_cache.go index 99dc4f3fa98e65f9de8c9cc6fc8f98ea9632912f..9342564bc51d26c80a10dbf2fa099b81e5eb167c 100644 --- a/event/event_cache.go +++ b/event/event_cache.go @@ -59,7 +59,7 @@ func (this *EventCache) poll() []interface{} { // Catches events that callers subscribe to and adds them to an array ready to be polled. type EventSubscriptions struct { - mtx *sync.Mutex + mtx *sync.RWMutex eventEmitter EventEmitter subs map[string]*EventCache reap bool @@ -67,7 +67,7 @@ type EventSubscriptions struct { func NewEventSubscriptions(eventEmitter EventEmitter) *EventSubscriptions { es := &EventSubscriptions{ - mtx: &sync.Mutex{}, + mtx: &sync.RWMutex{}, eventEmitter: eventEmitter, subs: make(map[string]*EventCache), reap: true, @@ -121,6 +121,8 @@ func (this *EventSubscriptions) Add(eventId string) (string, error) { } func (this *EventSubscriptions) Poll(subId string) ([]interface{}, error) { + this.mtx.RLock() + defer this.mtx.RUnlock() sub, ok := this.subs[subId] if !ok { return nil, fmt.Errorf("Subscription not active. ID: " + subId)