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

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

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

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

Loading…
Cancel
Save