mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-06-10 16:10:18 +08:00
fix(discov): prevent unbounded memory growth on duplicate etcd PUT events (#5580)
This commit is contained in:
@@ -263,14 +263,24 @@ func (c *cluster) handleWatchEvents(ctx context.Context, key watchKey, events []
|
||||
for _, ev := range events {
|
||||
switch ev.Type {
|
||||
case clientv3.EventTypePut:
|
||||
evKey := string(ev.Kv.Key)
|
||||
evVal := string(ev.Kv.Value)
|
||||
c.lock.Lock()
|
||||
watcher.values[string(ev.Kv.Key)] = string(ev.Kv.Value)
|
||||
oldVal, exists := watcher.values[evKey]
|
||||
watcher.values[evKey] = evVal
|
||||
c.lock.Unlock()
|
||||
if exists && oldVal == evVal {
|
||||
// duplicate PUT with same value, skip to prevent unbounded growth
|
||||
continue
|
||||
}
|
||||
if exists {
|
||||
// key moved to a new value, notify delete of old entry first
|
||||
for _, l := range listeners {
|
||||
l.OnDelete(KV{Key: evKey, Val: oldVal})
|
||||
}
|
||||
}
|
||||
for _, l := range listeners {
|
||||
l.OnAdd(KV{
|
||||
Key: string(ev.Kv.Key),
|
||||
Val: string(ev.Kv.Value),
|
||||
})
|
||||
l.OnAdd(KV{Key: evKey, Val: evVal})
|
||||
}
|
||||
case clientv3.EventTypeDelete:
|
||||
c.lock.Lock()
|
||||
@@ -433,7 +443,7 @@ func (c *cluster) setupWatch(cli EtcdClient, key watchKey, rev int64) (context.C
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithCancel(cli.Ctx())
|
||||
|
||||
|
||||
c.lock.Lock()
|
||||
if watcher, ok := c.watchers[key]; ok {
|
||||
watcher.cancel = cancel
|
||||
|
||||
Reference in New Issue
Block a user