Skip to content

Commit 35ba024

Browse files
authored
chore: refactor code (#5352)
1 parent 52df1c5 commit 35ba024

File tree

4 files changed

+20
-17
lines changed

4 files changed

+20
-17
lines changed

core/configcenter/subscriber/etcd.go

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package subscriber
22

33
import (
4-
"github.com/zeromicro/go-zero/core/discov"
5-
"github.com/zeromicro/go-zero/core/logx"
64
"sync"
75
"sync/atomic"
6+
7+
"github.com/zeromicro/go-zero/core/discov"
8+
"github.com/zeromicro/go-zero/core/logx"
89
)
910

1011
type (
@@ -39,7 +40,7 @@ func NewEtcdSubscriber(conf EtcdConf) (Subscriber, error) {
3940
func buildSubOptions(conf EtcdConf) []discov.SubOption {
4041
opts := []discov.SubOption{
4142
discov.WithExactMatch(),
42-
discov.WithContainer(newConfigCenterContainer()),
43+
discov.WithContainer(newContainer()),
4344
}
4445

4546
if len(conf.User) > 0 {
@@ -69,41 +70,41 @@ func (s *etcdSubscriber) Value() (string, error) {
6970
return "", nil
7071
}
7172

72-
type configCenterContainer struct {
73+
type container struct {
7374
value atomic.Value
74-
lock sync.Mutex
7575
listeners []func()
76+
lock sync.Mutex
7677
}
7778

78-
func newConfigCenterContainer() *configCenterContainer {
79-
return &configCenterContainer{}
79+
func newContainer() *container {
80+
return &container{}
8081
}
8182

82-
func (c *configCenterContainer) OnAdd(kv discov.KV) {
83+
func (c *container) OnAdd(kv discov.KV) {
8384
c.value.Store([]string{kv.Val})
8485
c.notifyChange()
8586
}
8687

87-
func (c *configCenterContainer) OnDelete(_ discov.KV) {
88+
func (c *container) OnDelete(_ discov.KV) {
8889
c.value.Store([]string(nil))
8990
c.notifyChange()
9091
}
9192

92-
func (c *configCenterContainer) AddListener(listener func()) {
93+
func (c *container) AddListener(listener func()) {
9394
c.lock.Lock()
9495
c.listeners = append(c.listeners, listener)
9596
c.lock.Unlock()
9697
}
9798

98-
func (c *configCenterContainer) GetValues() []string {
99-
vals, ok := c.value.Load().([]string)
100-
if !ok {
101-
return []string(nil)
99+
func (c *container) GetValues() []string {
100+
if vals, ok := c.value.Load().([]string); ok {
101+
return vals
102102
}
103-
return vals
103+
104+
return []string(nil)
104105
}
105106

106-
func (c *configCenterContainer) notifyChange() {
107+
func (c *container) notifyChange() {
107108
c.lock.Lock()
108109
listeners := append(([]func())(nil), c.listeners...)
109110
c.lock.Unlock()

core/configcenter/subscriber/etcd_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ func TestConfigCenterContainer(t *testing.T) {
158158
for _, test := range tests {
159159
t.Run(test.name, func(t *testing.T) {
160160
var changed bool
161-
c := newConfigCenterContainer()
161+
c := newContainer()
162162
c.AddListener(func() {
163163
changed = true
164164
})

core/discov/subscriber.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ type (
105105
AddListener(listener func())
106106
GetValues() []string
107107
}
108+
108109
container struct {
109110
exclusive bool
110111
values map[string][]string

core/discov/subscriber_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ func TestSubscriber(t *testing.T) {
205205
sub := new(Subscriber)
206206
Exclusive()(sub)
207207
c := newContainer(sub.exclusive)
208+
WithContainer(c)(sub)
208209
sub.items = c
209210
var count int32
210211
sub.AddListener(func() {

0 commit comments

Comments
 (0)