mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-05-13 18:00:00 +08:00
fix memory leak of grpc resolver (#4490)
Co-authored-by: nk <kui.niu@akuvox.com>
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/collection"
|
||||
"github.com/zeromicro/go-zero/core/discov/internal"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"github.com/zeromicro/go-zero/core/syncx"
|
||||
@@ -16,6 +17,7 @@ type (
|
||||
// A Subscriber is used to subscribe the given key on an etcd cluster.
|
||||
Subscriber struct {
|
||||
endpoints []string
|
||||
key string
|
||||
exclusive bool
|
||||
exactMatch bool
|
||||
items *container
|
||||
@@ -52,6 +54,11 @@ func (s *Subscriber) Values() []string {
|
||||
return s.items.getValues()
|
||||
}
|
||||
|
||||
// Close s.
|
||||
func (s *Subscriber) Close() {
|
||||
internal.GetRegistry().Unmonitor(s.endpoints, s.key, s.items)
|
||||
}
|
||||
|
||||
// Exclusive means that key value can only be 1:1,
|
||||
// which means later added value will remove the keys associated with the same value previously.
|
||||
func Exclusive() SubOption {
|
||||
@@ -83,7 +90,7 @@ func WithSubEtcdTLS(certFile, certKeyFile, caFile string, insecureSkipVerify boo
|
||||
|
||||
type container struct {
|
||||
exclusive bool
|
||||
values map[string][]string
|
||||
values map[string]*collection.Set
|
||||
mapping map[string]string
|
||||
snapshot atomic.Value
|
||||
dirty *syncx.AtomicBool
|
||||
@@ -94,7 +101,7 @@ type container struct {
|
||||
func newContainer(exclusive bool) *container {
|
||||
return &container{
|
||||
exclusive: exclusive,
|
||||
values: make(map[string][]string),
|
||||
values: make(map[string]*collection.Set),
|
||||
mapping: make(map[string]string),
|
||||
dirty: syncx.ForAtomicBool(true),
|
||||
}
|
||||
@@ -116,15 +123,21 @@ func (c *container) addKv(key, value string) ([]string, bool) {
|
||||
defer c.lock.Unlock()
|
||||
|
||||
c.dirty.Set(true)
|
||||
keys := c.values[value]
|
||||
if c.values[value] == nil {
|
||||
c.values[value] = collection.NewSet()
|
||||
}
|
||||
keys := c.values[value].KeysStr()
|
||||
previous := append([]string(nil), keys...)
|
||||
early := len(keys) > 0
|
||||
if c.exclusive && early {
|
||||
for _, each := range keys {
|
||||
c.doRemoveKey(each)
|
||||
}
|
||||
if c.values[value] == nil {
|
||||
c.values[value] = collection.NewSet()
|
||||
}
|
||||
}
|
||||
c.values[value] = append(c.values[value], key)
|
||||
c.values[value].AddStr(key)
|
||||
c.mapping[key] = value
|
||||
|
||||
if early {
|
||||
@@ -147,18 +160,12 @@ func (c *container) doRemoveKey(key string) {
|
||||
}
|
||||
|
||||
delete(c.mapping, key)
|
||||
keys := c.values[server]
|
||||
remain := keys[:0]
|
||||
|
||||
for _, k := range keys {
|
||||
if k != key {
|
||||
remain = append(remain, k)
|
||||
}
|
||||
if c.values[server] == nil {
|
||||
return
|
||||
}
|
||||
c.values[server].Remove(key)
|
||||
|
||||
if len(remain) > 0 {
|
||||
c.values[server] = remain
|
||||
} else {
|
||||
if c.values[server].Count() == 0 {
|
||||
delete(c.values, server)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user