From e8d0e765d61474739223d8b266e3b4532f0f4a69 Mon Sep 17 00:00:00 2001 From: alpharush <0xalpharush@protonmail.com> Date: Thu, 27 Jun 2024 16:21:49 -0500 Subject: [PATCH] fix: use read-only lock for event callbacks --- events/events.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/events/events.go b/events/events.go index 2ee595c9..63179243 100644 --- a/events/events.go +++ b/events/events.go @@ -19,7 +19,7 @@ var globalEventHandlers map[string][]any // globalEventHandlersLock is a lock that provides thread synchronization when accessing globalEventHandlers. This // helps in avoiding concurrent access panics. -var globalEventHandlersLock sync.Mutex +var globalEventHandlersLock sync.RWMutex // SubscribeAny adds an EventHandler to the list of global EventHandler objects for this a given event data type. // When an event is published, the callback will be triggered with the event data. @@ -76,9 +76,9 @@ func (e *EventEmitter[T]) Publish(event T) error { // If we have any handlers, invoke them. if globalEventHandlers != nil { // Acquire a thread lock when fetching our event handlers to avoid concurrent access panics. - globalEventHandlersLock.Lock() + globalEventHandlersLock.RLock() callbacks := globalEventHandlers[eventType.String()] - globalEventHandlersLock.Unlock() + globalEventHandlersLock.RUnlock() // Call all relevant event handlers. for i := 0; i < len(callbacks); i++ {