fix(registry): contrib/registry/zookeeper ephemeral nodes handling after restart (#2245)

pull/2249/head
林晓炜 2 years ago committed by GitHub
parent aae0339692
commit 1f3ac4ea22
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 12
      contrib/registry/zookeeper/register.go

@ -135,12 +135,20 @@ func (r *Registry) Watch(ctx context.Context, serviceName string) (registry.Watc
// ensureName ensure node exists, if not exist, create and set data // ensureName ensure node exists, if not exist, create and set data
func (r *Registry) ensureName(path string, data []byte, flags int32) error { func (r *Registry) ensureName(path string, data []byte, flags int32) error {
exists, _, err := r.conn.Exists(path) exists, stat, err := r.conn.Exists(path)
if err != nil { if err != nil {
return err return err
} }
// ephemeral nodes handling after restart
// fixes a race condition if the server crashes without using CreateProtectedEphemeralSequential()
if flags&zk.FlagEphemeral == zk.FlagEphemeral {
err = r.conn.Delete(path, stat.Version)
if err != nil && err != zk.ErrNoNode {
return err
}
exists = false
}
if !exists { if !exists {
var err error
if len(r.opts.user) > 0 && len(r.opts.password) > 0 { if len(r.opts.user) > 0 && len(r.opts.password) > 0 {
_, err = r.conn.Create(path, data, flags, zk.DigestACL(zk.PermAll, r.opts.user, r.opts.password)) _, err = r.conn.Create(path, data, flags, zk.DigestACL(zk.PermAll, r.opts.user, r.opts.password))
} else { } else {

Loading…
Cancel
Save