diff --git a/registry/zookeeper/registry.go b/registry/zookeeper/registry.go index b8f0cfb9f4d4304e7469e86889528b3521a82520..cf442a1969e2f5fec16003858d235db806a8eb49 100644 --- a/registry/zookeeper/registry.go +++ b/registry/zookeeper/registry.go @@ -156,15 +156,13 @@ func (r *zkRegistry) DoRegister(root string, node string) error { } func (r *zkRegistry) DoUnregister(root string, node string) error { - client := r.client - if client == nil { - return perrors.New("zk Client is null, can not process registerTempZookeeperNode ") - } + r.cltLock.Lock() + defer r.cltLock.Unlock() - if !client.ZkConnValid() { + if !r.ZkClient().ZkConnValid() { return perrors.Errorf("zk client is not valid.") } - return client.Delete(path.Join(root, node)) + return r.ZkClient().Delete(path.Join(root, node)) } func (r *zkRegistry) DoSubscribe(conf *common.URL) (registry.Listener, error) { @@ -255,7 +253,9 @@ func (r *zkRegistry) getListener(conf *common.URL) (*RegistryConfigurationListen zkListener = NewRegistryConfigurationListener(r.client, r, conf) if r.listener == nil { + r.cltLock.Lock() client := r.client + r.cltLock.Unlock() if client == nil { return nil, perrors.New("zk connection broken") } diff --git a/remoting/zookeeper/client.go b/remoting/zookeeper/client.go index fcf07d4666455ea09a4aeb28cb6c502e3820ae21..cc34c76b4274b8e3f51652f170b0d6222122fa04 100644 --- a/remoting/zookeeper/client.go +++ b/remoting/zookeeper/client.go @@ -51,7 +51,7 @@ var ( type ZookeeperClient struct { name string ZkAddrs []string - sync.Mutex // for conn + sync.RWMutex // for conn Conn *zk.Conn Timeout time.Duration exit chan struct{} @@ -419,7 +419,9 @@ func (z *ZookeeperClient) CreateWithValue(basePath string, value []byte) error { for _, str := range strings.Split(basePath, "/")[1:] { tmpPath = path.Join(tmpPath, "/", str) err = errNilZkClientConn + z.RLock() conn := z.Conn + z.RUnlock() if conn != nil { _, err = conn.Create(tmpPath, value, 0, zk.WorldACL(zk.PermAll)) } @@ -444,7 +446,9 @@ func (z *ZookeeperClient) Delete(basePath string) error { ) err = errNilZkClientConn + z.RLock() conn := z.Conn + z.RUnlock() if conn != nil { err = conn.Delete(basePath, -1) } @@ -464,7 +468,9 @@ func (z *ZookeeperClient) RegisterTemp(basePath string, node string) (string, er err = errNilZkClientConn data = []byte("") zkPath = path.Join(basePath) + "/" + node + z.RLock() conn := z.Conn + z.RUnlock() if conn != nil { tmpPath, err = conn.Create(zkPath, data, zk.FlagEphemeral, zk.WorldACL(zk.PermAll)) } @@ -487,7 +493,9 @@ func (z *ZookeeperClient) RegisterTempSeq(basePath string, data []byte) (string, ) err = errNilZkClientConn + z.RLock() conn := z.Conn + z.RUnlock() if conn != nil { tmpPath, err = conn.Create( path.Join(basePath)+"/", @@ -518,7 +526,9 @@ func (z *ZookeeperClient) GetChildrenW(path string) ([]string, <-chan zk.Event, ) err = errNilZkClientConn + z.RLock() conn := z.Conn + z.RUnlock() if conn != nil { children, stat, watcher, err = conn.ChildrenW(path) } @@ -552,7 +562,9 @@ func (z *ZookeeperClient) GetChildren(path string) ([]string, error) { ) err = errNilZkClientConn + z.RLock() conn := z.Conn + z.RUnlock() if conn != nil { children, stat, err = conn.Children(path) } @@ -583,7 +595,9 @@ func (z *ZookeeperClient) ExistW(zkPath string) (<-chan zk.Event, error) { ) err = errNilZkClientConn + z.RLock() conn := z.Conn + z.RUnlock() if conn != nil { exist, _, watcher, err = conn.ExistsW(zkPath) }