|
1 | 1 | package subscriber |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "github.com/zeromicro/go-zero/core/discov" |
5 | | - "github.com/zeromicro/go-zero/core/logx" |
6 | 4 | "sync" |
7 | 5 | "sync/atomic" |
| 6 | + |
| 7 | + "github.com/zeromicro/go-zero/core/discov" |
| 8 | + "github.com/zeromicro/go-zero/core/logx" |
8 | 9 | ) |
9 | 10 |
|
10 | 11 | type ( |
@@ -39,7 +40,7 @@ func NewEtcdSubscriber(conf EtcdConf) (Subscriber, error) { |
39 | 40 | func buildSubOptions(conf EtcdConf) []discov.SubOption { |
40 | 41 | opts := []discov.SubOption{ |
41 | 42 | discov.WithExactMatch(), |
42 | | - discov.WithContainer(newConfigCenterContainer()), |
| 43 | + discov.WithContainer(newContainer()), |
43 | 44 | } |
44 | 45 |
|
45 | 46 | if len(conf.User) > 0 { |
@@ -69,41 +70,41 @@ func (s *etcdSubscriber) Value() (string, error) { |
69 | 70 | return "", nil |
70 | 71 | } |
71 | 72 |
|
72 | | -type configCenterContainer struct { |
| 73 | +type container struct { |
73 | 74 | value atomic.Value |
74 | | - lock sync.Mutex |
75 | 75 | listeners []func() |
| 76 | + lock sync.Mutex |
76 | 77 | } |
77 | 78 |
|
78 | | -func newConfigCenterContainer() *configCenterContainer { |
79 | | - return &configCenterContainer{} |
| 79 | +func newContainer() *container { |
| 80 | + return &container{} |
80 | 81 | } |
81 | 82 |
|
82 | | -func (c *configCenterContainer) OnAdd(kv discov.KV) { |
| 83 | +func (c *container) OnAdd(kv discov.KV) { |
83 | 84 | c.value.Store([]string{kv.Val}) |
84 | 85 | c.notifyChange() |
85 | 86 | } |
86 | 87 |
|
87 | | -func (c *configCenterContainer) OnDelete(_ discov.KV) { |
| 88 | +func (c *container) OnDelete(_ discov.KV) { |
88 | 89 | c.value.Store([]string(nil)) |
89 | 90 | c.notifyChange() |
90 | 91 | } |
91 | 92 |
|
92 | | -func (c *configCenterContainer) AddListener(listener func()) { |
| 93 | +func (c *container) AddListener(listener func()) { |
93 | 94 | c.lock.Lock() |
94 | 95 | c.listeners = append(c.listeners, listener) |
95 | 96 | c.lock.Unlock() |
96 | 97 | } |
97 | 98 |
|
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 |
102 | 102 | } |
103 | | - return vals |
| 103 | + |
| 104 | + return []string(nil) |
104 | 105 | } |
105 | 106 |
|
106 | | -func (c *configCenterContainer) notifyChange() { |
| 107 | +func (c *container) notifyChange() { |
107 | 108 | c.lock.Lock() |
108 | 109 | listeners := append(([]func())(nil), c.listeners...) |
109 | 110 | c.lock.Unlock() |
|
0 commit comments