Skip to content
Snippets Groups Projects
Commit 7f87ea6f authored by lzp0412's avatar lzp0412
Browse files

coding standard

parent 0c987d33
No related branches found
No related tags found
No related merge requests found
...@@ -24,21 +24,21 @@ import ( ...@@ -24,21 +24,21 @@ import (
) )
type nacosListener struct { type nacosListener struct {
namingClient naming_client.INamingClient namingClient naming_client.INamingClient
listenUrl common.URL listenUrl common.URL
events chan *remoting.ConfigChangeEvent events chan *remoting.ConfigChangeEvent
hostMapInstance map[string]model.Instance instanceMap map[string]model.Instance
cacheLock sync.Mutex cacheLock sync.Mutex
done chan struct{} done chan struct{}
subscribeParam *vo.SubscribeParam subscribeParam *vo.SubscribeParam
} }
func NewNacosListener(url common.URL, namingClient naming_client.INamingClient) (*nacosListener, error) { func NewNacosListener(url common.URL, namingClient naming_client.INamingClient) (*nacosListener, error) {
listener := &nacosListener{ listener := &nacosListener{
namingClient: namingClient, namingClient: namingClient,
listenUrl: url, events: make(chan *remoting.ConfigChangeEvent, 32), listenUrl: url, events: make(chan *remoting.ConfigChangeEvent, 32),
hostMapInstance: map[string]model.Instance{}, instanceMap: map[string]model.Instance{},
done: make(chan struct{}), done: make(chan struct{}),
} }
err := listener.startListen() err := listener.startListen()
return listener, err return listener, err
...@@ -111,7 +111,7 @@ func (nl *nacosListener) Callback(services []model.SubscribeService, err error) ...@@ -111,7 +111,7 @@ func (nl *nacosListener) Callback(services []model.SubscribeService, err error)
host := services[i].Ip + ":" + strconv.Itoa(int(services[i].Port)) host := services[i].Ip + ":" + strconv.Itoa(int(services[i].Port))
instance := generateInstance(services[i]) instance := generateInstance(services[i])
newInstanceMap[host] = instance newInstanceMap[host] = instance
if old, ok := nl.hostMapInstance[host]; !ok { if old, ok := nl.instanceMap[host]; !ok {
//instance is not exsit in cache,add it to cache //instance is not exsit in cache,add it to cache
addInstances = append(addInstances, instance) addInstances = append(addInstances, instance)
} else { } else {
...@@ -122,14 +122,14 @@ func (nl *nacosListener) Callback(services []model.SubscribeService, err error) ...@@ -122,14 +122,14 @@ func (nl *nacosListener) Callback(services []model.SubscribeService, err error)
} }
} }
for host, inst := range nl.hostMapInstance { for host, inst := range nl.instanceMap {
if _, ok := newInstanceMap[host]; !ok { if _, ok := newInstanceMap[host]; !ok {
//cache instance is not exsit in new instance list, remove it from cache //cache instance is not exsit in new instance list, remove it from cache
delInstances = append(delInstances, inst) delInstances = append(delInstances, inst)
} }
} }
nl.hostMapInstance = newInstanceMap nl.instanceMap = newInstanceMap
for i := range addInstances { for i := range addInstances {
newUrl := generateUrl(addInstances[i]) newUrl := generateUrl(addInstances[i])
......
...@@ -139,7 +139,7 @@ func TestNacosRegistry_Subscribe_del(t *testing.T) { ...@@ -139,7 +139,7 @@ func TestNacosRegistry_Subscribe_del(t *testing.T) {
assert.Regexp(t, ".*ServiceEvent{Action{add}.*", serviceEvent2.String()) assert.Regexp(t, ".*ServiceEvent{Action{add}.*", serviceEvent2.String())
nacosReg := reg.(*nacosRegistry) nacosReg := reg.(*nacosRegistry)
//手动注销实例 //deregister instance to mock instance offline
nacosReg.namingClient.DeregisterInstance(vo.DeregisterInstanceParam{Ip: "127.0.0.2", Port: 20000, ServiceName: "providers:com.ikurento.user.UserProvider:1.0.0:guangzhou-idc"}) nacosReg.namingClient.DeregisterInstance(vo.DeregisterInstanceParam{Ip: "127.0.0.2", Port: 20000, ServiceName: "providers:com.ikurento.user.UserProvider:1.0.0:guangzhou-idc"})
serviceEvent3, _ := listener.Next() serviceEvent3, _ := listener.Next()
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment