Skip to content
Snippets Groups Projects
Unverified Commit b51438b9 authored by Silas Davis's avatar Silas Davis
Browse files

Use RWMutex and lock it for poll reads to avoid concurrent read-write

parent 9d95e7f3
No related branches found
No related tags found
No related merge requests found
...@@ -59,7 +59,7 @@ func (this *EventCache) poll() []interface{} { ...@@ -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. // Catches events that callers subscribe to and adds them to an array ready to be polled.
type EventSubscriptions struct { type EventSubscriptions struct {
mtx *sync.Mutex mtx *sync.RWMutex
eventEmitter EventEmitter eventEmitter EventEmitter
subs map[string]*EventCache subs map[string]*EventCache
reap bool reap bool
...@@ -67,7 +67,7 @@ type EventSubscriptions struct { ...@@ -67,7 +67,7 @@ type EventSubscriptions struct {
func NewEventSubscriptions(eventEmitter EventEmitter) *EventSubscriptions { func NewEventSubscriptions(eventEmitter EventEmitter) *EventSubscriptions {
es := &EventSubscriptions{ es := &EventSubscriptions{
mtx: &sync.Mutex{}, mtx: &sync.RWMutex{},
eventEmitter: eventEmitter, eventEmitter: eventEmitter,
subs: make(map[string]*EventCache), subs: make(map[string]*EventCache),
reap: true, reap: true,
...@@ -121,6 +121,8 @@ func (this *EventSubscriptions) Add(eventId string) (string, error) { ...@@ -121,6 +121,8 @@ func (this *EventSubscriptions) Add(eventId string) (string, error) {
} }
func (this *EventSubscriptions) Poll(subId string) ([]interface{}, error) { func (this *EventSubscriptions) Poll(subId string) ([]interface{}, error) {
this.mtx.RLock()
defer this.mtx.RUnlock()
sub, ok := this.subs[subId] sub, ok := this.subs[subId]
if !ok { if !ok {
return nil, fmt.Errorf("Subscription not active. ID: " + subId) return nil, fmt.Errorf("Subscription not active. ID: " + subId)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment