From b51438b9d83fafc3d61fc214450205194ab87af8 Mon Sep 17 00:00:00 2001 From: Silas Davis <silas@erisindustries.com> Date: Wed, 30 Aug 2017 14:48:28 +0100 Subject: [PATCH] Use RWMutex and lock it for poll reads to avoid concurrent read-write --- event/event_cache.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/event/event_cache.go b/event/event_cache.go index 99dc4f3f..9342564b 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) -- GitLab