From b7f09b3a37cece8a30d2093c6dc0c15a0afe76a5 Mon Sep 17 00:00:00 2001 From: linxiaowei Date: Thu, 28 Jul 2022 19:10:38 +0800 Subject: [PATCH] fix(transport): transport/grpc/resolver update instance's addrs not directly return with firstUpdateState --- transport/grpc/resolver/discovery/builder.go | 1 + transport/grpc/resolver/discovery/resolver.go | 9 ++++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/transport/grpc/resolver/discovery/builder.go b/transport/grpc/resolver/discovery/builder.go index a4425a518..762c99ece 100644 --- a/transport/grpc/resolver/discovery/builder.go +++ b/transport/grpc/resolver/discovery/builder.go @@ -91,6 +91,7 @@ func (b *builder) Build(target resolver.Target, cc resolver.ClientConn, opts res cancel: cancel, insecure: b.insecure, debugLogDisabled: b.debugLogDisabled, + firstUpdateState: true, } go r.watch() return r, nil diff --git a/transport/grpc/resolver/discovery/resolver.go b/transport/grpc/resolver/discovery/resolver.go index 0dc115b65..c8097b140 100644 --- a/transport/grpc/resolver/discovery/resolver.go +++ b/transport/grpc/resolver/discovery/resolver.go @@ -23,6 +23,7 @@ type discoveryResolver struct { insecure bool debugLogDisabled bool + firstUpdateState bool } func (r *discoveryResolver) watch() { @@ -72,11 +73,17 @@ func (r *discoveryResolver) update(ins []*registry.ServiceInstance) { } if len(addrs) == 0 { log.Warnf("[resolver] Zero endpoint found,refused to write, instances: %v", ins) - return + // Not directly return with firstUpdateState + // because gRPC client will block until the resolver has provided addresses or the context expires. + if !r.firstUpdateState { + return + } } err := r.cc.UpdateState(resolver.State{Addresses: addrs}) if err != nil { log.Errorf("[resolver] failed to update state: %s", err) + } else { + r.firstUpdateState = false } if !r.debugLogDisabled {