Skip to content
Snippets Groups Projects
Commit cee7fe1f authored by Patrick's avatar Patrick
Browse files

add CreateTempWithValue in zookeeper client

parent 4feed90c
No related branches found
No related tags found
No related merge requests found
......@@ -432,6 +432,44 @@ func (z *ZookeeperClient) CreateWithValue(basePath string, value []byte) error {
return nil
}
// CreateTempWithValue will create the node recursively, which means that if the parent node is absent,
// it will create parent node first,and set value in last child path
func (z *ZookeeperClient) CreateTempWithValue(basePath string, value []byte) error {
var (
err error
tmpPath string
)
logger.Debugf("zookeeperClient.Create(basePath{%s})", basePath)
conn := z.getConn()
err = errNilZkClientConn
if conn == nil {
return perrors.WithMessagef(err, "zk.Create(path:%s)", basePath)
}
pathSlice := strings.Split(basePath, "/")[1:]
length := len(pathSlice)
for i, str := range pathSlice {
tmpPath = path.Join(tmpPath, "/", str)
// last child need be ephemeral
if i == length-1 {
_, err = conn.Create(tmpPath, value, zk.FlagEphemeral, zk.WorldACL(zk.PermAll))
} else {
_, err = conn.Create(tmpPath, []byte{}, 0, zk.WorldACL(zk.PermAll))
}
if err != nil {
if err == zk.ErrNodeExists {
logger.Debugf("zk.create(\"%s\") exists", tmpPath)
} else {
logger.Errorf("zk.create(\"%s\") error(%v)", tmpPath, perrors.WithStack(err))
return perrors.WithMessagef(err, "zk.Create(path:%s)", basePath)
}
}
}
return nil
}
// nolint
func (z *ZookeeperClient) Delete(basePath string) error {
err := errNilZkClientConn
......
......@@ -70,7 +70,7 @@ func (sd *ServiceDiscovery) registerService(instance *ServiceInstance) error {
if err != nil {
return err
}
err = sd.client.CreateWithValue(path, data)
err = sd.client.CreateTempWithValue(path, data)
if err != nil {
return err
}
......
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