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. 37
      transport/grpc/client.go
  2. 46
      transport/grpc/resolver/discovery/builder.go
  3. 4
      transport/grpc/resolver/discovery/builder_test.go
  4. 10
      transport/grpc/resolver/discovery/resolver.go

@ -109,19 +109,26 @@ 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
subsetSize int subsetSize int
tlsConf *tls.Config tlsConf *tls.Config
timeout time.Duration timeout time.Duration
discovery registry.Discovery discovery registry.Discovery
middleware []middleware.Middleware middleware []middleware.Middleware
ints []grpc.UnaryClientInterceptor ints []grpc.UnaryClientInterceptor
streamInts []grpc.StreamClientInterceptor streamInts []grpc.StreamClientInterceptor
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.
@ -136,9 +143,10 @@ func DialInsecure(ctx context.Context, opts ...ClientOption) (*grpc.ClientConn,
func dial(ctx context.Context, insecure bool, opts ...ClientOption) (*grpc.ClientConn, error) { func dial(ctx context.Context, insecure bool, opts ...ClientOption) (*grpc.ClientConn, error) {
options := clientOptions{ options := clientOptions{
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,29 +38,37 @@ 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
} }
} }
type builder struct { type builder struct {
discoverer registry.Discovery discoverer registry.Discovery
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.
func NewBuilder(d registry.Discovery, opts ...Option) resolver.Builder { func NewBuilder(d registry.Discovery, opts ...Option) resolver.Builder {
b := &builder{ b := &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 {
o(b) o(b)
@ -96,14 +104,14 @@ func (b *builder) Build(target resolver.Target, cc resolver.ClientConn, opts res
} }
r := &discoveryResolver{ r := &discoveryResolver{
w: watchRes.w, w: watchRes.w,
cc: cc, cc: cc,
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(),
} }
go r.watch() go r.watch()
return r, nil return r, nil

@ -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)
} }
} }

@ -22,10 +22,10 @@ type discoveryResolver struct {
ctx context.Context ctx context.Context
cancel context.CancelFunc cancel context.CancelFunc
insecure bool insecure bool
debugLogDisabled bool debugLog bool
selecterKey string selecterKey string
subsetSize int subsetSize int
} }
func (r *discoveryResolver) watch() { func (r *discoveryResolver) watch() {
@ -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