diff options
author | Valery Piashchynski <[email protected]> | 2020-12-17 10:11:23 +0300 |
---|---|---|
committer | Valery Piashchynski <[email protected]> | 2020-12-17 10:11:23 +0300 |
commit | 8422e300f64f20d86ffccf773d41109285bbc12d (patch) | |
tree | 2c66ade061a7c2af1239515b431e50c7627b0ba5 /util | |
parent | 26c6e2c481921760f0f2cc98fdbb022ed1c54a48 (diff) |
Attempt to fix race #1
Diffstat (limited to 'util')
-rwxr-xr-x | util/events.go | 9 |
1 files changed, 7 insertions, 2 deletions
diff --git a/util/events.go b/util/events.go index 259c7ddb..dbb5990b 100755 --- a/util/events.go +++ b/util/events.go @@ -1,12 +1,15 @@ package util import ( + "sync" + "github.com/spiral/roadrunner/v2/interfaces/events" ) // EventHandler helps to broadcast events to multiple listeners. type EventHandler struct { listeners []events.EventListener + sync.RWMutex } func NewEventsHandler() events.Handler { @@ -20,12 +23,14 @@ func (eb *EventHandler) NumListeners() int { // AddListener registers new event listener. func (eb *EventHandler) AddListener(listener events.EventListener) { + eb.Lock() + defer eb.Unlock() eb.listeners = append(eb.listeners, listener) } // Push broadcast events across all event listeners. func (eb *EventHandler) Push(e interface{}) { - for _, listener := range eb.listeners { - listener(e) + for k := range eb.listeners { + eb.listeners[k](e) } } |