mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-05-13 09:50:00 +08:00
fix memory leak of grpc resolver (#4490)
Co-authored-by: nk <kui.niu@akuvox.com>
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"slices"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -59,6 +60,17 @@ func (r *Registry) Monitor(endpoints []string, key string, l UpdateListener, exa
|
||||
return c.monitor(key, l, exactMatch)
|
||||
}
|
||||
|
||||
// Unmonitor cancel monitoring of given endpoints and keys, and remove the listener.
|
||||
func (r *Registry) Unmonitor(endpoints []string, key string, l UpdateListener) {
|
||||
c, exists := r.getCluster(endpoints)
|
||||
// if not exists, return.
|
||||
if !exists {
|
||||
return
|
||||
}
|
||||
|
||||
c.unmonitor(key, l)
|
||||
}
|
||||
|
||||
func (r *Registry) getCluster(endpoints []string) (c *cluster, exists bool) {
|
||||
clusterKey := getClusterKey(endpoints)
|
||||
r.lock.RLock()
|
||||
@@ -273,6 +285,14 @@ func (c *cluster) monitor(key string, l UpdateListener, exactMatch bool) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *cluster) unmonitor(key string, l UpdateListener) {
|
||||
c.lock.Lock()
|
||||
defer c.lock.Unlock()
|
||||
c.listeners[key] = slices.DeleteFunc(c.listeners[key], func(listener UpdateListener) bool {
|
||||
return l == listener
|
||||
})
|
||||
}
|
||||
|
||||
func (c *cluster) newClient() (EtcdClient, error) {
|
||||
cli, err := NewClient(c.endpoints)
|
||||
if err != nil {
|
||||
|
||||
@@ -292,6 +292,31 @@ func TestRegistry_Monitor(t *testing.T) {
|
||||
assert.Error(t, GetRegistry().Monitor(endpoints, "foo", new(mockListener), false))
|
||||
}
|
||||
|
||||
func TestRegistry_Unmonitor(t *testing.T) {
|
||||
svr, err := mockserver.StartMockServers(1)
|
||||
assert.NoError(t, err)
|
||||
svr.StartAt(0)
|
||||
|
||||
endpoints := []string{svr.Servers[0].Address}
|
||||
GetRegistry().lock.Lock()
|
||||
GetRegistry().clusters = map[string]*cluster{
|
||||
getClusterKey(endpoints): {
|
||||
listeners: map[string][]UpdateListener{},
|
||||
values: map[string]map[string]string{
|
||||
"foo": {
|
||||
"bar": "baz",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
GetRegistry().lock.Unlock()
|
||||
l := new(mockListener)
|
||||
assert.Error(t, GetRegistry().Monitor(endpoints, "foo", l, false))
|
||||
assert.Equal(t, 1, len(GetRegistry().clusters[getClusterKey(endpoints)].listeners["foo"]))
|
||||
GetRegistry().Unmonitor(endpoints, "foo", l)
|
||||
assert.Equal(t, 0, len(GetRegistry().clusters[getClusterKey(endpoints)].listeners["foo"]))
|
||||
}
|
||||
|
||||
type mockListener struct {
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user