fix(grpc/balancer): fix the problem that the watch log cannot be closed (#2726)

pull/2730/head
包子 2 years ago committed by GitHub
parent 9ee4fcb48a
commit 768ffd71d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 9
      transport/grpc/client.go
  2. 16
      transport/grpc/resolver/discovery/builder.go
  3. 4
      transport/grpc/resolver/discovery/builder_test.go
  4. 4
      transport/grpc/resolver/discovery/resolver.go

@ -109,6 +109,12 @@ func WithLogger(log log.Logger) ClientOption {
return func(o *clientOptions) {}
}
func WithPrintDiscoveryDebugLog(p bool) ClientOption {
return func(o *clientOptions) {
o.printDiscoveryDebugLog = p
}
}
// clientOptions is gRPC Client
type clientOptions struct {
endpoint string
@ -122,6 +128,7 @@ type clientOptions struct {
grpcOpts []grpc.DialOption
balancerName string
filters []selector.NodeFilter
printDiscoveryDebugLog bool
}
// Dial returns a GRPC connection.
@ -139,6 +146,7 @@ func dial(ctx context.Context, insecure bool, opts ...ClientOption) (*grpc.Clien
timeout: 2000 * time.Millisecond,
balancerName: balancerName,
subsetSize: 25,
printDiscoveryDebugLog: true,
}
for _, o := range opts {
o(&options)
@ -169,6 +177,7 @@ func dial(ctx context.Context, insecure bool, opts ...ClientOption) (*grpc.Clien
options.discovery,
discovery.WithInsecure(insecure),
discovery.WithSubset(options.subsetSize),
discovery.PrintDebugLog(options.printDiscoveryDebugLog),
)))
}
if insecure {

@ -38,10 +38,18 @@ func WithSubset(size int) Option {
}
}
// Deprecated: please use PrintDebugLog
// DisableDebugLog disables update instances log.
func DisableDebugLog() Option {
return func(b *builder) {
b.debugLogDisabled = true
b.debugLog = false
}
}
// PrintDebugLog print grpc resolver watch service log
func PrintDebugLog(p bool) Option {
return func(b *builder) {
b.debugLog = p
}
}
@ -50,7 +58,7 @@ type builder struct {
timeout time.Duration
insecure bool
subsetSize int
debugLogDisabled bool
debugLog bool
}
// NewBuilder creates a builder which is used to factory registry resolvers.
@ -59,7 +67,7 @@ func NewBuilder(d registry.Discovery, opts ...Option) resolver.Builder {
discoverer: d,
timeout: time.Second * 10,
insecure: false,
debugLogDisabled: false,
debugLog: true,
subsetSize: 25,
}
for _, o := range opts {
@ -101,7 +109,7 @@ func (b *builder) Build(target resolver.Target, cc resolver.ClientConn, opts res
ctx: ctx,
cancel: cancel,
insecure: b.insecure,
debugLogDisabled: b.debugLogDisabled,
debugLog: b.debugLog,
subsetSize: b.subsetSize,
selecterKey: uuid.New().String(),
}

@ -33,8 +33,8 @@ func TestWithTimeout(t *testing.T) {
func TestDisableDebugLog(t *testing.T) {
o := &builder{}
DisableDebugLog()(o)
if !o.debugLogDisabled {
t.Errorf("expected debugLogDisabled true, got %v", o.debugLogDisabled)
if o.debugLog {
t.Errorf("expected debugLog true, got %v", o.debugLog)
}
}

@ -23,7 +23,7 @@ type discoveryResolver struct {
cancel context.CancelFunc
insecure bool
debugLogDisabled bool
debugLog bool
selecterKey string
subsetSize int
}
@ -90,7 +90,7 @@ func (r *discoveryResolver) update(ins []*registry.ServiceInstance) {
log.Errorf("[resolver] failed to update state: %s", err)
}
if !r.debugLogDisabled {
if r.debugLog {
b, _ := json.Marshal(filtered)
log.Infof("[resolver] update instances: %s", b)
}

Loading…
Cancel
Save