From 3f51c37cbee90a389e25f4d2c20fe7459f675036 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BE=E9=87=8C=28barry=29?= Date: Sat, 24 Aug 2024 21:53:41 +0800 Subject: [PATCH 1/2] fix: barry 2024-08-24 21:53:40 --- clients/grpcc/grpcc_config/config.go | 2 +- clients/grpcc/util.go | 32 ++++++++++++++++++++-------- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/clients/grpcc/grpcc_config/config.go b/clients/grpcc/grpcc_config/config.go index 0a0380db..a3e7e9c4 100644 --- a/clients/grpcc/grpcc_config/config.go +++ b/clients/grpcc/grpcc_config/config.go @@ -34,7 +34,7 @@ var defaultOpts = []grpc.DialOption{grpc.WithDefaultServiceConfig(`{ type Cfg struct { Client *GrpcClientCfg `yaml:"grpc_client"` Service *ServiceCfg `yaml:"service"` - Resolvers []resolver.Builder `yaml:"-"` + Resolvers []resolver.Builder `yaml:"-" json:"-"` } type ServiceCfg struct { diff --git a/clients/grpcc/util.go b/clients/grpcc/util.go index 46cb2629..1db160b3 100644 --- a/clients/grpcc/util.go +++ b/clients/grpcc/util.go @@ -9,6 +9,7 @@ import ( "github.com/pubgo/lava/clients/grpcc/grpcc_resolver" "github.com/pubgo/lava/core/logging/logkey" "github.com/pubgo/lava/lava" + "github.com/rs/zerolog" "google.golang.org/grpc" ) @@ -31,22 +32,35 @@ func buildTarget(cfg *grpcc_config.ServiceCfg) string { } } -func createConn(cfg *grpcc_config.Cfg, log log.Logger, mm []lava.Middleware) (grpc.ClientConnInterface, error) { +func createConn(cfg *grpcc_config.Cfg, log log.Logger, mm []lava.Middleware) (_ grpc.ClientConnInterface, gErr error) { addr := buildTarget(cfg.Service) - ee := log.Info(). - Str(logkey.Service, cfg.Service.Name). - Str("addr", addr) - ee.Msg("grpc client init") + var logMsg = func(e *zerolog.Event) { + e.Any(logkey.Service, cfg.Service) + e.Any("config", cfg.Client) + e.Str("addr", addr) + } + + defer func() { + if gErr == nil { + log.Info(). + Func(logMsg).Msg("succeed to create grpc client") + } else { + log.Err(gErr). + Func(logMsg).Msg("failed to create grpc client") + } + }() - conn, err := grpc.NewClient(addr, append( - append(cfg.Client.ToOpts(), grpc.WithResolvers(cfg.Resolvers...)), + opts := append( + cfg.Client.ToOpts(), + grpc.WithResolvers(cfg.Resolvers...), grpc.WithChainUnaryInterceptor(unaryInterceptor(mm)), - grpc.WithChainStreamInterceptor(streamInterceptor(mm)))...) + grpc.WithChainStreamInterceptor(streamInterceptor(mm)), + ) + conn, err := grpc.NewClient(addr, opts...) if err != nil { return nil, errors.Wrapf(err, "grpc dial failed, target=>%s", addr) } - ee.Msg("grpc client init ok") return conn, nil } From 4b8d3c1ebd8a6e127e5efba70043f9853d0527cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BE=E9=87=8C=28barry=29?= Date: Sat, 24 Aug 2024 22:12:38 +0800 Subject: [PATCH 2/2] fix: barry 2024-08-24 22:12:38 --- clients/grpcc/middleware.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/clients/grpcc/middleware.go b/clients/grpcc/middleware.go index 8ba65d00..371d3963 100644 --- a/clients/grpcc/middleware.go +++ b/clients/grpcc/middleware.go @@ -42,13 +42,12 @@ func unaryInterceptor(middlewares []lava.Middleware) grpc.UnaryClientInterceptor reqCtx := req.(*request) header := make(metadata.MD) trailer := make(metadata.MD) - reqCtx.opts = append(reqCtx.opts, grpc.Header(&header), grpc.Trailer(&trailer)) - - if err := reqCtx.invoker(ctx, reqCtx.method, reqCtx.req, reqCtx.reply, reqCtx.cc, reqCtx.opts...); err != nil { + opts := append(reqCtx.opts, grpc.Header(&header), grpc.Trailer(&trailer)) + if err := reqCtx.invoker(ctx, reqCtx.method, reqCtx.req, reqCtx.reply, reqCtx.cc, opts...); err != nil { return nil, err } - rsp := &response{resp: reqCtx.resp, header: new(lava.ResponseHeader)} + rsp := &response{header: new(lava.ResponseHeader)} md2Head(header, rsp.header) md2Head(trailer, rsp.header) return rsp, nil