mirror of
https://github.com/go-kratos/kratos.git
synced 2025-01-14 02:33:03 +02:00
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
This commit is contained in:
parent
92b3c8f94a
commit
246d8d9c28
@ -3,6 +3,7 @@ package zookeeper
|
||||
import (
|
||||
"context"
|
||||
"path"
|
||||
"time"
|
||||
|
||||
"github.com/go-zookeeper/zk"
|
||||
"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 {
|
||||
return err
|
||||
}
|
||||
go r.reRegister(servicePath, data)
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -150,3 +152,21 @@ func (r *Registry) ensureName(path string, data []byte, flags int32) error {
|
||||
}
|
||||
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…
Reference in New Issue
Block a user