From c8ecf58b3ef9648c2c5451c34876aaff471f598f Mon Sep 17 00:00:00 2001 From: Adphi Date: Tue, 23 Nov 2021 15:15:47 +0100 Subject: [PATCH] client: add interceptors option --- client/client.go | 7 +++++++ client/options.go | 25 +++++++++++++++++++++++++ example/example.go | 5 +++++ 3 files changed, 37 insertions(+) diff --git a/client/client.go b/client/client.go index 5e4a5fc..866e8c6 100644 --- a/client/client.go +++ b/client/client.go @@ -6,6 +6,7 @@ import ( "fmt" "strings" + grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware" "google.golang.org/grpc" "google.golang.org/grpc/credentials" "google.golang.org/grpc/resolver" @@ -36,6 +37,12 @@ func New(opts ...Option) (Client, error) { if !c.opts.secure { c.opts.dialOptions = append(c.opts.dialOptions, grpc.WithInsecure()) } + if len(c.opts.unaryInterceptors) > 0 { + c.opts.dialOptions = append(c.opts.dialOptions, grpc.WithUnaryInterceptor(grpc_middleware.ChainUnaryClient(c.opts.unaryInterceptors...))) + } + if len(c.opts.streamInterceptors) > 0 { + c.opts.dialOptions = append(c.opts.dialOptions, grpc.WithStreamInterceptor(grpc_middleware.ChainStreamClient(c.opts.streamInterceptors...))) + } if c.opts.addr == "" { c.addr = fmt.Sprintf("%s:///%s", c.opts.registry.String(), c.opts.name) } else if strings.HasPrefix(c.opts.addr, "tcp://"){ diff --git a/client/options.go b/client/options.go index 5854207..c66b70d 100644 --- a/client/options.go +++ b/client/options.go @@ -5,6 +5,7 @@ import ( "google.golang.org/grpc" + "go.linka.cloud/grpc/interceptors" "go.linka.cloud/grpc/registry" ) @@ -60,6 +61,27 @@ func WithDialOptions(opts ...grpc.DialOption) Option { } } +func WithInterceptors(i ...interceptors.ClientInterceptors) Option { + return func(o *options) { + for _, v := range i { + o.unaryInterceptors = append(o.unaryInterceptors, v.UnaryClientInterceptor()) + o.streamInterceptors = append(o.streamInterceptors, v.StreamClientInterceptor()) + } + } +} + +func WithUnaryInterceptors(i ...grpc.UnaryClientInterceptor) Option { + return func(o *options) { + o.unaryInterceptors = append(o.unaryInterceptors, i...) + } +} + +func WithStreamInterceptors(i ...grpc.StreamClientInterceptor) Option { + return func(o *options) { + o.streamInterceptors = append(o.streamInterceptors, i...) + } +} + type options struct { registry registry.Registry name string @@ -68,6 +90,9 @@ type options struct { tlsConfig *tls.Config secure bool dialOptions []grpc.DialOption + + unaryInterceptors []grpc.UnaryClientInterceptor + streamInterceptors []grpc.StreamClientInterceptor } func (o *options) Name() string { diff --git a/example/example.go b/example/example.go index 4a048be..890d097 100644 --- a/example/example.go +++ b/example/example.go @@ -12,6 +12,7 @@ import ( "time" "github.com/sirupsen/logrus" + "google.golang.org/grpc" "go.linka.cloud/grpc/client" "go.linka.cloud/grpc/interceptors/defaulter" @@ -113,6 +114,10 @@ func main() { client.WithAddress("localhost:9991"), // client.WithRegistry(mdns.NewRegistry()), client.WithSecure(secure), + client.WithUnaryInterceptors(func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error { + logger.From(ctx).WithFields("party", "client", "method", method).Info(req) + return invoker(ctx, method, req, reply, cc, opts...) + }), ) if err != nil { logrus.Fatal(err)