Skip to content

Commit

Permalink
extract method for get zookeeper connection
Browse files Browse the repository at this point in the history
  • Loading branch information
zouyx committed Jun 2, 2020
1 parent 1d1bbe0 commit 9ae184f
Showing 1 changed file with 20 additions and 25 deletions.
45 changes: 20 additions & 25 deletions remoting/zookeeper/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,15 +416,15 @@ func (z *ZookeeperClient) CreateWithValue(basePath string, value []byte) error {
)

logger.Debugf("zookeeperClient.Create(basePath{%s})", basePath)
conn := z.getConn()
err = errNilZkClientConn
if conn == nil {
return perrors.WithMessagef(err, "zk.Create(path:%s)", basePath)
}

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))
}
_, err = conn.Create(tmpPath, value, 0, zk.WorldACL(zk.PermAll))

if err != nil {
if err == zk.ErrNodeExists {
Expand All @@ -446,9 +446,7 @@ func (z *ZookeeperClient) Delete(basePath string) error {
)

err = errNilZkClientConn
z.RLock()
conn := z.Conn
z.RUnlock()
conn := z.getConn()
if conn != nil {
err = conn.Delete(basePath, -1)
}
Expand All @@ -468,9 +466,7 @@ 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()
conn := z.getConn()
if conn != nil {
tmpPath, err = conn.Create(zkPath, data, zk.FlagEphemeral, zk.WorldACL(zk.PermAll))
}
Expand All @@ -493,9 +489,7 @@ func (z *ZookeeperClient) RegisterTempSeq(basePath string, data []byte) (string,
)

err = errNilZkClientConn
z.RLock()
conn := z.Conn
z.RUnlock()
conn := z.getConn()
if conn != nil {
tmpPath, err = conn.Create(
path.Join(basePath)+"/",
Expand Down Expand Up @@ -526,9 +520,7 @@ func (z *ZookeeperClient) GetChildrenW(path string) ([]string, <-chan zk.Event,
)

err = errNilZkClientConn
z.RLock()
conn := z.Conn
z.RUnlock()
conn := z.getConn()
if conn != nil {
children, stat, watcher, err = conn.ChildrenW(path)
}
Expand Down Expand Up @@ -562,9 +554,7 @@ func (z *ZookeeperClient) GetChildren(path string) ([]string, error) {
)

err = errNilZkClientConn
z.RLock()
conn := z.Conn
z.RUnlock()
conn := z.getConn()
if conn != nil {
children, stat, err = conn.Children(path)
}
Expand Down Expand Up @@ -595,9 +585,7 @@ func (z *ZookeeperClient) ExistW(zkPath string) (<-chan zk.Event, error) {
)

err = errNilZkClientConn
z.RLock()
conn := z.Conn
z.RUnlock()
conn := z.getConn()
if conn != nil {
exist, _, watcher, err = conn.ExistsW(zkPath)
}
Expand All @@ -618,3 +606,10 @@ func (z *ZookeeperClient) ExistW(zkPath string) (<-chan zk.Event, error) {
func (z *ZookeeperClient) GetContent(zkPath string) ([]byte, *zk.Stat, error) {
return z.Conn.Get(zkPath)
}

// getConn gets zookeeper connection safely
func (z *ZookeeperClient) getConn() *zk.Conn {
z.RLock()
defer z.RUnlock()
return z.Conn
}

0 comments on commit 9ae184f

Please sign in to comment.