diff --git a/zrpc/resolver/internal/discovbuilder.go b/zrpc/resolver/internal/discovbuilder.go index ed377d138fa1..65b314453f09 100644 --- a/zrpc/resolver/internal/discovbuilder.go +++ b/zrpc/resolver/internal/discovbuilder.go @@ -9,10 +9,41 @@ import ( "google.golang.org/grpc/resolver" ) -type discovBuilder struct{} +type discovBuilder struct { + cc resolver.ClientConn + update func() +} func (b *discovBuilder) Build(target resolver.Target, cc resolver.ClientConn, _ resolver.BuildOptions) ( resolver.Resolver, error) { + b.cc = cc + if err := b.updateState(target); err != nil { + return nil, err + } + + return &nopResolver{cc: cc}, nil +} + +func (b *discovBuilder) Scheme() string { + return DiscovScheme +} + +func (b *discovBuilder) updateState(target resolver.Target) error { + if b.update == nil { + update, err := b.buildEndpointsUpdater(target) + if err != nil { + return err + } + + b.update = update + } + + b.update() + + return nil +} + +func (b *discovBuilder) buildEndpointsUpdater(target resolver.Target) (func(), error) { hosts := strings.FieldsFunc(targets.GetAuthority(target), func(r rune) bool { return r == EndpointSepChar }) @@ -29,18 +60,13 @@ func (b *discovBuilder) Build(target resolver.Target, cc resolver.ClientConn, _ Addr: val, }) } - if err := cc.UpdateState(resolver.State{ + if err := b.cc.UpdateState(resolver.State{ Addresses: addrs, }); err != nil { logx.Error(err) } } sub.AddListener(update) - update() - return &nopResolver{cc: cc}, nil -} - -func (b *discovBuilder) Scheme() string { - return DiscovScheme + return update, nil } diff --git a/zrpc/resolver/internal/kubebuilder.go b/zrpc/resolver/internal/kubebuilder.go index 5eecec4630aa..f26c3ab46231 100644 --- a/zrpc/resolver/internal/kubebuilder.go +++ b/zrpc/resolver/internal/kubebuilder.go @@ -28,7 +28,8 @@ type kubeResolver struct { stopCh chan struct{} } -func (r *kubeResolver) ResolveNow(_ resolver.ResolveNowOptions) {} +func (r *kubeResolver) ResolveNow(_ resolver.ResolveNowOptions) { +} func (r *kubeResolver) start() { threading.GoSafe(func() {