Unverified Commit 944d0be7 authored by wenxuwan's avatar wenxuwan Committed by GitHub

change code more readable (#57)

* change code more readable

* fix nacos ut failed

* fix nacos ut
Co-authored-by: 's avatar文徐 <wangwenxue.wwx@alibaba-inc.com>
parent e7592d92
......@@ -39,10 +39,12 @@ func TestNewNacosClient(t *testing.T) {
t.Run("naming_client", func(t *testing.T) {
client1, err := NewNacosNamingClient("nacos", true, scs, cc)
assert.Nil(t, err)
client2, err := NewNacosNamingClient("nacos", true, scs, cc)
assert.Nil(t, err)
client3, err := NewNacosNamingClient("nacos", false, scs, cc)
assert.Nil(t, err)
client4, err := NewNacosNamingClient("test", true, scs, cc)
assert.Nil(t, err)
assert.Equal(t, client1, client2)
assert.NotEqual(t, client1, client3)
......@@ -51,8 +53,11 @@ func TestNewNacosClient(t *testing.T) {
t.Run("config_client", func(t *testing.T) {
client1, err := NewNacosConfigClient("nacos", true, scs, cc)
assert.Nil(t, err)
client2, err := NewNacosConfigClient("nacos", true, scs, cc)
assert.Nil(t, err)
client3, err := NewNacosConfigClient("nacos", false, scs, cc)
assert.Nil(t, err)
client4, err := NewNacosConfigClient("test", true, scs, cc)
assert.Nil(t, err)
......
......@@ -111,16 +111,25 @@ func initZookeeperClientPool() {
// NewZookeeperClient will create a ZookeeperClient
func NewZookeeperClient(name string, zkAddrs []string, share bool, opts ...zkClientOption) (*ZookeeperClient, error) {
if share {
clientPoolOnce.Do(initZookeeperClientPool)
zkClientPool.Lock()
defer zkClientPool.Unlock()
if zkClient, ok := zkClientPool.zkClient[name]; ok {
zkClient.activeNumber++
return zkClient, nil
}
if !share {
return newClient(name, zkAddrs, share, opts...)
}
clientPoolOnce.Do(initZookeeperClientPool)
zkClientPool.Lock()
defer zkClientPool.Unlock()
if zkClient, ok := zkClientPool.zkClient[name]; ok {
zkClient.activeNumber++
return zkClient, nil
}
newZkClient, err := newClient(name, zkAddrs, share, opts...)
if err != nil {
return nil, err
}
zkClientPool.zkClient[name] = newZkClient
return newZkClient, nil
}
func newClient(name string, zkAddrs []string, share bool, opts ...zkClientOption) (*ZookeeperClient, error) {
newZkClient := &ZookeeperClient{
name: name,
ZkAddrs: zkAddrs,
......@@ -139,9 +148,6 @@ func NewZookeeperClient(name string, zkAddrs []string, share bool, opts ...zkCli
return nil, err
}
newZkClient.activeNumber++
if share {
zkClientPool.zkClient[name] = newZkClient
}
return newZkClient, nil
}
......
Markdown is supported
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