mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-05-14 02:10:00 +08:00
chore: refactor code (#5352)
This commit is contained in:
@@ -1,10 +1,11 @@
|
|||||||
package subscriber
|
package subscriber
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/zeromicro/go-zero/core/discov"
|
|
||||||
"github.com/zeromicro/go-zero/core/logx"
|
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
|
|
||||||
|
"github.com/zeromicro/go-zero/core/discov"
|
||||||
|
"github.com/zeromicro/go-zero/core/logx"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
@@ -39,7 +40,7 @@ func NewEtcdSubscriber(conf EtcdConf) (Subscriber, error) {
|
|||||||
func buildSubOptions(conf EtcdConf) []discov.SubOption {
|
func buildSubOptions(conf EtcdConf) []discov.SubOption {
|
||||||
opts := []discov.SubOption{
|
opts := []discov.SubOption{
|
||||||
discov.WithExactMatch(),
|
discov.WithExactMatch(),
|
||||||
discov.WithContainer(newConfigCenterContainer()),
|
discov.WithContainer(newContainer()),
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(conf.User) > 0 {
|
if len(conf.User) > 0 {
|
||||||
@@ -69,41 +70,41 @@ func (s *etcdSubscriber) Value() (string, error) {
|
|||||||
return "", nil
|
return "", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type configCenterContainer struct {
|
type container struct {
|
||||||
value atomic.Value
|
value atomic.Value
|
||||||
lock sync.Mutex
|
|
||||||
listeners []func()
|
listeners []func()
|
||||||
|
lock sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func newConfigCenterContainer() *configCenterContainer {
|
func newContainer() *container {
|
||||||
return &configCenterContainer{}
|
return &container{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *configCenterContainer) OnAdd(kv discov.KV) {
|
func (c *container) OnAdd(kv discov.KV) {
|
||||||
c.value.Store([]string{kv.Val})
|
c.value.Store([]string{kv.Val})
|
||||||
c.notifyChange()
|
c.notifyChange()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *configCenterContainer) OnDelete(_ discov.KV) {
|
func (c *container) OnDelete(_ discov.KV) {
|
||||||
c.value.Store([]string(nil))
|
c.value.Store([]string(nil))
|
||||||
c.notifyChange()
|
c.notifyChange()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *configCenterContainer) AddListener(listener func()) {
|
func (c *container) AddListener(listener func()) {
|
||||||
c.lock.Lock()
|
c.lock.Lock()
|
||||||
c.listeners = append(c.listeners, listener)
|
c.listeners = append(c.listeners, listener)
|
||||||
c.lock.Unlock()
|
c.lock.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *configCenterContainer) GetValues() []string {
|
func (c *container) GetValues() []string {
|
||||||
vals, ok := c.value.Load().([]string)
|
if vals, ok := c.value.Load().([]string); ok {
|
||||||
if !ok {
|
|
||||||
return []string(nil)
|
|
||||||
}
|
|
||||||
return vals
|
return vals
|
||||||
|
}
|
||||||
|
|
||||||
|
return []string(nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *configCenterContainer) notifyChange() {
|
func (c *container) notifyChange() {
|
||||||
c.lock.Lock()
|
c.lock.Lock()
|
||||||
listeners := append(([]func())(nil), c.listeners...)
|
listeners := append(([]func())(nil), c.listeners...)
|
||||||
c.lock.Unlock()
|
c.lock.Unlock()
|
||||||
|
|||||||
@@ -158,7 +158,7 @@ func TestConfigCenterContainer(t *testing.T) {
|
|||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
t.Run(test.name, func(t *testing.T) {
|
t.Run(test.name, func(t *testing.T) {
|
||||||
var changed bool
|
var changed bool
|
||||||
c := newConfigCenterContainer()
|
c := newContainer()
|
||||||
c.AddListener(func() {
|
c.AddListener(func() {
|
||||||
changed = true
|
changed = true
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -105,6 +105,7 @@ type (
|
|||||||
AddListener(listener func())
|
AddListener(listener func())
|
||||||
GetValues() []string
|
GetValues() []string
|
||||||
}
|
}
|
||||||
|
|
||||||
container struct {
|
container struct {
|
||||||
exclusive bool
|
exclusive bool
|
||||||
values map[string][]string
|
values map[string][]string
|
||||||
|
|||||||
@@ -205,6 +205,7 @@ func TestSubscriber(t *testing.T) {
|
|||||||
sub := new(Subscriber)
|
sub := new(Subscriber)
|
||||||
Exclusive()(sub)
|
Exclusive()(sub)
|
||||||
c := newContainer(sub.exclusive)
|
c := newContainer(sub.exclusive)
|
||||||
|
WithContainer(c)(sub)
|
||||||
sub.items = c
|
sub.items = c
|
||||||
var count int32
|
var count int32
|
||||||
sub.AddListener(func() {
|
sub.AddListener(func() {
|
||||||
|
|||||||
Reference in New Issue
Block a user