chore: refactor code (#5352)

This commit is contained in:
Kevin Wan
2025-12-23 22:29:52 +08:00
committed by GitHub
parent 52df1c532a
commit 35ba024103
4 changed files with 20 additions and 17 deletions

View File

@@ -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()

View File

@@ -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
})

View File

@@ -105,6 +105,7 @@ type (
AddListener(listener func())
GetValues() []string
}
container struct {
exclusive bool
values map[string][]string

View File

@@ -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() {