fix: protect `EventDispatcher` map with mutex (#28540)

Add mutex protection for `EventDispatcher.registry` map 
Fix #28538

Signed-off-by: Congqi Xia <congqi.xia@zilliz.com>
pull/28498/head
congqixia 2023-11-17 20:32:20 +08:00 committed by GitHub
parent d2f53aefa5
commit c948a437a9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 0 deletions

View File

@ -17,9 +17,11 @@ package config
import (
"strings"
"sync"
)
type EventDispatcher struct {
mut sync.RWMutex
registry map[string][]EventHandler
keyPrefix []string
}
@ -32,10 +34,14 @@ func NewEventDispatcher() *EventDispatcher {
}
func (ed *EventDispatcher) Get(key string) []EventHandler {
ed.mut.RLock()
defer ed.mut.RUnlock()
return ed.registry[formatKey(key)]
}
func (ed *EventDispatcher) Dispatch(event *Event) {
ed.mut.RLock()
defer ed.mut.RUnlock()
var hs []EventHandler
realKey := formatKey(event.Key)
hs, ok := ed.registry[realKey]
@ -55,6 +61,8 @@ func (ed *EventDispatcher) Dispatch(event *Event) {
// register a handler to watch specific config changed
func (ed *EventDispatcher) Register(key string, handler EventHandler) {
ed.mut.Lock()
defer ed.mut.Unlock()
key = formatKey(key)
v, ok := ed.registry[key]
if ok {
@ -66,6 +74,8 @@ func (ed *EventDispatcher) Register(key string, handler EventHandler) {
// register a handler to watch specific config changed
func (ed *EventDispatcher) RegisterForKeyPrefix(keyPrefix string, handler EventHandler) {
ed.mut.Lock()
defer ed.mut.Unlock()
keyPrefix = formatKey(keyPrefix)
v, ok := ed.registry[keyPrefix]
if ok {
@ -77,6 +87,8 @@ func (ed *EventDispatcher) RegisterForKeyPrefix(keyPrefix string, handler EventH
}
func (ed *EventDispatcher) Unregister(key string, handler EventHandler) {
ed.mut.Lock()
defer ed.mut.Unlock()
key = formatKey(key)
v, ok := ed.registry[key]
if !ok {