Skip to content

Commit

Permalink
Fix concurrent access in DB listeners (#490)
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Dort-Golts <[email protected]>
  • Loading branch information
dgtony authored Feb 26, 2021
1 parent 7a964ca commit 879fb54
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions db/listeners.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ type Listener interface {
}

type stateChangedNotifee struct {
lock sync.Mutex
lock sync.RWMutex
listeners []*listener
}

Expand All @@ -87,6 +87,8 @@ type listener struct {
var _ Listener = (*listener)(nil)

func (scn *stateChangedNotifee) notify(actions []Action) {
scn.lock.RLock()
defer scn.lock.RUnlock()
for _, a := range actions {
for _, l := range scn.listeners {
if l.evaluate(a) {
Expand All @@ -107,6 +109,8 @@ func (scn *stateChangedNotifee) addListener(sl *listener) {
}

func (scn *stateChangedNotifee) remove(sl *listener) bool {
scn.lock.Lock()
defer scn.lock.Unlock()
for i := range scn.listeners {
if scn.listeners[i] == sl {
scn.listeners[i] = scn.listeners[len(scn.listeners)-1]
Expand All @@ -122,8 +126,10 @@ func (scn *stateChangedNotifee) close() {
scn.lock.Lock()
defer scn.lock.Unlock()
for i := range scn.listeners {
scn.listeners[i].Close()
close(scn.listeners[i].c)
scn.listeners[i] = nil
}
scn.listeners = nil
}

// Channel returns an unbuffered channel to receive
Expand Down

0 comments on commit 879fb54

Please sign in to comment.