mirror of
				https://github.com/linka-cloud/grpc.git
				synced 2025-11-04 03:21:50 +00:00 
			
		
		
		
	client: add interceptors option
This commit is contained in:
		@@ -6,6 +6,7 @@ import (
 | 
				
			|||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
 | 
				
			||||||
	"google.golang.org/grpc"
 | 
						"google.golang.org/grpc"
 | 
				
			||||||
	"google.golang.org/grpc/credentials"
 | 
						"google.golang.org/grpc/credentials"
 | 
				
			||||||
	"google.golang.org/grpc/resolver"
 | 
						"google.golang.org/grpc/resolver"
 | 
				
			||||||
@@ -36,6 +37,12 @@ func New(opts ...Option) (Client, error) {
 | 
				
			|||||||
	if !c.opts.secure {
 | 
						if !c.opts.secure {
 | 
				
			||||||
		c.opts.dialOptions = append(c.opts.dialOptions, grpc.WithInsecure())
 | 
							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 == "" {
 | 
						if c.opts.addr == "" {
 | 
				
			||||||
		c.addr = fmt.Sprintf("%s:///%s", c.opts.registry.String(), c.opts.name)
 | 
							c.addr = fmt.Sprintf("%s:///%s", c.opts.registry.String(), c.opts.name)
 | 
				
			||||||
	} else if strings.HasPrefix(c.opts.addr, "tcp://"){
 | 
						} else if strings.HasPrefix(c.opts.addr, "tcp://"){
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -5,6 +5,7 @@ import (
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	"google.golang.org/grpc"
 | 
						"google.golang.org/grpc"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"go.linka.cloud/grpc/interceptors"
 | 
				
			||||||
	"go.linka.cloud/grpc/registry"
 | 
						"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 {
 | 
					type options struct {
 | 
				
			||||||
	registry    registry.Registry
 | 
						registry    registry.Registry
 | 
				
			||||||
	name        string
 | 
						name        string
 | 
				
			||||||
@@ -68,6 +90,9 @@ type options struct {
 | 
				
			|||||||
	tlsConfig   *tls.Config
 | 
						tlsConfig   *tls.Config
 | 
				
			||||||
	secure      bool
 | 
						secure      bool
 | 
				
			||||||
	dialOptions []grpc.DialOption
 | 
						dialOptions []grpc.DialOption
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						unaryInterceptors []grpc.UnaryClientInterceptor
 | 
				
			||||||
 | 
						streamInterceptors []grpc.StreamClientInterceptor
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (o *options) Name() string {
 | 
					func (o *options) Name() string {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -12,6 +12,7 @@ import (
 | 
				
			|||||||
	"time"
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/sirupsen/logrus"
 | 
						"github.com/sirupsen/logrus"
 | 
				
			||||||
 | 
						"google.golang.org/grpc"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"go.linka.cloud/grpc/client"
 | 
						"go.linka.cloud/grpc/client"
 | 
				
			||||||
	"go.linka.cloud/grpc/interceptors/defaulter"
 | 
						"go.linka.cloud/grpc/interceptors/defaulter"
 | 
				
			||||||
@@ -113,6 +114,10 @@ func main() {
 | 
				
			|||||||
		client.WithAddress("localhost:9991"),
 | 
							client.WithAddress("localhost:9991"),
 | 
				
			||||||
		// client.WithRegistry(mdns.NewRegistry()),
 | 
							// client.WithRegistry(mdns.NewRegistry()),
 | 
				
			||||||
		client.WithSecure(secure),
 | 
							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 {
 | 
						if err != nil {
 | 
				
			||||||
		logrus.Fatal(err)
 | 
							logrus.Fatal(err)
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user