Skip to content

Commit

Permalink
fix: agent ui sensor mutex (#3672)
Browse files Browse the repository at this point in the history
* fix: agent ui sensor mutex

* don't run sensor On callback in a goroutine
  • Loading branch information
mathnogueira authored Feb 21, 2024
1 parent 53c7a25 commit cf4f517
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions agent/ui/dashboard/sensors/sensor.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type sensor struct {
listeners map[string][]func(Event)
lastEvent map[string]Event

sync.Mutex
mutex sync.Mutex
}

func NewSensor() Sensor {
Expand All @@ -42,16 +42,15 @@ func NewSensor() Sensor {
}

func (r *sensor) Reset() {
r.Lock()
defer r.Unlock()
r.mutex.Lock()
defer r.mutex.Unlock()

r.listeners = make(map[string][]func(Event))
r.lastEvent = make(map[string]Event)
}

func (r *sensor) On(eventName string, cb func(Event)) {
r.Lock()
defer r.Unlock()
r.mutex.Lock()

var slice []func(Event)
if existingSlice, ok := r.listeners[eventName]; ok {
Expand All @@ -60,15 +59,16 @@ func (r *sensor) On(eventName string, cb func(Event)) {
slice = make([]func(Event), 0)
}
r.listeners[eventName] = append(slice, cb)
r.mutex.Unlock()

if event, ok := r.lastEvent[eventName]; ok {
cb(event)
}
}

func (r *sensor) Emit(eventName string, event interface{}) {
r.Lock()
defer r.Unlock()
r.mutex.Lock()
r.mutex.Unlock()

listeners := r.listeners[eventName]
e := Event{
Expand Down

0 comments on commit cf4f517

Please sign in to comment.