diff options
author | Valery Piashchynski <[email protected]> | 2020-12-26 13:13:59 +0300 |
---|---|---|
committer | Valery Piashchynski <[email protected]> | 2020-12-26 13:13:59 +0300 |
commit | 1aaf6e6ffb015cd5a21d9d938ad84c18723973c5 (patch) | |
tree | 7221427ba25f8f99a10720778a420703dc2bff92 /pkg/events | |
parent | de4d0b6e66ceda08b0daec18a218c1baa71ebf04 (diff) |
Reorganize eventsv2.0.0-beta4
Diffstat (limited to 'pkg/events')
-rwxr-xr-x | pkg/events/events.go | 23 |
1 files changed, 13 insertions, 10 deletions
diff --git a/pkg/events/events.go b/pkg/events/events.go index 92dc103a..226a0c91 100755 --- a/pkg/events/events.go +++ b/pkg/events/events.go @@ -6,32 +6,35 @@ import ( "github.com/spiral/roadrunner/v2/interfaces/events" ) -// EventHandler helps to broadcast events to multiple listeners. -type EventHandler struct { - listeners []events.EventListener - sync.RWMutex +// HandlerImpl helps to broadcast events to multiple listeners. +type HandlerImpl struct { + listeners []events.Listener + sync.RWMutex // all receivers should be pointers } func NewEventsHandler() events.Handler { - return &EventHandler{listeners: make([]events.EventListener, 0, 2)} + return &HandlerImpl{listeners: make([]events.Listener, 0, 2)} } // NumListeners returns number of event listeners. -func (eb *EventHandler) NumListeners() int { +func (eb *HandlerImpl) NumListeners() int { + eb.Lock() + defer eb.Unlock() return len(eb.listeners) } // AddListener registers new event listener. -func (eb *EventHandler) AddListener(listener events.EventListener) { +func (eb *HandlerImpl) AddListener(listener events.Listener) { eb.Lock() defer eb.Unlock() eb.listeners = append(eb.listeners, listener) } // Push broadcast events across all event listeners. -func (eb *EventHandler) Push(e interface{}) { - eb.Lock() - defer eb.Unlock() +func (eb *HandlerImpl) Push(e interface{}) { + // ReadLock here because we are not changing listeners + eb.RLock() + defer eb.RUnlock() for k := range eb.listeners { eb.listeners[k](e) } |