fix: zookeeper add auto re-register (#2235)

* feat(registry): contrib/registry/zookeeper add re-register support

* feat(registry): contrib/registry/zookeeper add re-register support fix with lint
pull/2217/head
林晓炜 2 years ago committed by GitHub
parent 92b3c8f94a
commit 246d8d9c28
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 20
      contrib/registry/zookeeper/register.go

@ -3,6 +3,7 @@ package zookeeper
import ( import (
"context" "context"
"path" "path"
"time"
"github.com/go-zookeeper/zk" "github.com/go-zookeeper/zk"
"golang.org/x/sync/singleflight" "golang.org/x/sync/singleflight"
@ -77,6 +78,7 @@ func (r *Registry) Register(ctx context.Context, service *registry.ServiceInstan
if err = r.ensureName(servicePath, data, zk.FlagEphemeral); err != nil { if err = r.ensureName(servicePath, data, zk.FlagEphemeral); err != nil {
return err return err
} }
go r.reRegister(servicePath, data)
return nil return nil
} }
@ -150,3 +152,21 @@ func (r *Registry) ensureName(path string, data []byte, flags int32) error {
} }
return nil return nil
} }
// reRegister re-register data node info when bad connection recovered
func (r *Registry) reRegister(path string, data []byte) {
sessionID := r.conn.SessionID()
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for range ticker.C {
cur := r.conn.SessionID()
// sessionID changed
if cur > 0 && sessionID != cur {
// re-ensureName
if err := r.ensureName(path, data, zk.FlagEphemeral); err != nil {
return
}
sessionID = cur
}
}
}

Loading…
Cancel
Save