From 35ba02410355c6daf03a109799bca58052e2bf75 Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Tue, 23 Dec 2025 22:29:52 +0800 Subject: [PATCH] chore: refactor code (#5352) --- core/configcenter/subscriber/etcd.go | 33 ++++++++++++----------- core/configcenter/subscriber/etcd_test.go | 2 +- core/discov/subscriber.go | 1 + core/discov/subscriber_test.go | 1 + 4 files changed, 20 insertions(+), 17 deletions(-) diff --git a/core/configcenter/subscriber/etcd.go b/core/configcenter/subscriber/etcd.go index c5a8f5334..4ca8c2b6c 100644 --- a/core/configcenter/subscriber/etcd.go +++ b/core/configcenter/subscriber/etcd.go @@ -1,10 +1,11 @@ package subscriber import ( - "github.com/zeromicro/go-zero/core/discov" - "github.com/zeromicro/go-zero/core/logx" "sync" "sync/atomic" + + "github.com/zeromicro/go-zero/core/discov" + "github.com/zeromicro/go-zero/core/logx" ) type ( @@ -39,7 +40,7 @@ func NewEtcdSubscriber(conf EtcdConf) (Subscriber, error) { func buildSubOptions(conf EtcdConf) []discov.SubOption { opts := []discov.SubOption{ discov.WithExactMatch(), - discov.WithContainer(newConfigCenterContainer()), + discov.WithContainer(newContainer()), } if len(conf.User) > 0 { @@ -69,41 +70,41 @@ func (s *etcdSubscriber) Value() (string, error) { return "", nil } -type configCenterContainer struct { +type container struct { value atomic.Value - lock sync.Mutex listeners []func() + lock sync.Mutex } -func newConfigCenterContainer() *configCenterContainer { - return &configCenterContainer{} +func newContainer() *container { + return &container{} } -func (c *configCenterContainer) OnAdd(kv discov.KV) { +func (c *container) OnAdd(kv discov.KV) { c.value.Store([]string{kv.Val}) c.notifyChange() } -func (c *configCenterContainer) OnDelete(_ discov.KV) { +func (c *container) OnDelete(_ discov.KV) { c.value.Store([]string(nil)) c.notifyChange() } -func (c *configCenterContainer) AddListener(listener func()) { +func (c *container) AddListener(listener func()) { c.lock.Lock() c.listeners = append(c.listeners, listener) c.lock.Unlock() } -func (c *configCenterContainer) GetValues() []string { - vals, ok := c.value.Load().([]string) - if !ok { - return []string(nil) +func (c *container) GetValues() []string { + if vals, ok := c.value.Load().([]string); ok { + return vals } - return vals + + return []string(nil) } -func (c *configCenterContainer) notifyChange() { +func (c *container) notifyChange() { c.lock.Lock() listeners := append(([]func())(nil), c.listeners...) c.lock.Unlock() diff --git a/core/configcenter/subscriber/etcd_test.go b/core/configcenter/subscriber/etcd_test.go index db6482f44..2db240cdc 100644 --- a/core/configcenter/subscriber/etcd_test.go +++ b/core/configcenter/subscriber/etcd_test.go @@ -158,7 +158,7 @@ func TestConfigCenterContainer(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { var changed bool - c := newConfigCenterContainer() + c := newContainer() c.AddListener(func() { changed = true }) diff --git a/core/discov/subscriber.go b/core/discov/subscriber.go index 12fc5d2ea..3b7b43164 100644 --- a/core/discov/subscriber.go +++ b/core/discov/subscriber.go @@ -105,6 +105,7 @@ type ( AddListener(listener func()) GetValues() []string } + container struct { exclusive bool values map[string][]string diff --git a/core/discov/subscriber_test.go b/core/discov/subscriber_test.go index d91d72ae4..4c5c355eb 100644 --- a/core/discov/subscriber_test.go +++ b/core/discov/subscriber_test.go @@ -205,6 +205,7 @@ func TestSubscriber(t *testing.T) { sub := new(Subscriber) Exclusive()(sub) c := newContainer(sub.exclusive) + WithContainer(c)(sub) sub.items = c var count int32 sub.AddListener(func() {