Skip to content

Commit

Permalink
chore: optimize lock in discov.etcd (#4272)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevwan authored Jul 25, 2024
1 parent 8f7aff5 commit 8ae0f28
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions core/discov/internal/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ type cluster struct {
listeners map[string][]UpdateListener
watchGroup *threading.RoutineGroup
done chan lang.PlaceholderType
lock sync.Mutex
lock sync.RWMutex
}

func newCluster(endpoints []string) *cluster {
Expand Down Expand Up @@ -108,8 +108,8 @@ func (c *cluster) getClient() (EtcdClient, error) {
}

func (c *cluster) getCurrent(key string) []KV {
c.lock.Lock()
defer c.lock.Unlock()
c.lock.RLock()
defer c.lock.RUnlock()

var kvs []KV
for k, v := range c.values[key] {
Expand All @@ -125,6 +125,7 @@ func (c *cluster) getCurrent(key string) []KV {
func (c *cluster) handleChanges(key string, kvs []KV) {
var add []KV
var remove []KV

c.lock.Lock()
listeners := append([]UpdateListener(nil), c.listeners[key]...)
vals, ok := c.values[key]
Expand Down Expand Up @@ -173,9 +174,9 @@ func (c *cluster) handleChanges(key string, kvs []KV) {
}

func (c *cluster) handleWatchEvents(key string, events []*clientv3.Event) {
c.lock.Lock()
c.lock.RLock()
listeners := append([]UpdateListener(nil), c.listeners[key]...)
c.lock.Unlock()
c.lock.RUnlock()

for _, ev := range events {
switch ev.Type {
Expand Down

0 comments on commit 8ae0f28

Please sign in to comment.