mirror of
https://github.com/go-kratos/kratos.git
synced 2025-03-17 21:07:54 +02:00
add grpc interceptor option (#990)
This commit is contained in:
parent
29e387a39c
commit
92b6bf54eb
@ -48,6 +48,13 @@ func WithDiscovery(d registry.Discovery) ClientOption {
|
||||
}
|
||||
}
|
||||
|
||||
// WithUnaryInterceptor returns a DialOption that specifies the interceptor for unary RPCs.
|
||||
func WithUnaryInterceptor(in ...grpc.UnaryClientInterceptor) ClientOption {
|
||||
return func(o *clientOptions) {
|
||||
o.ints = in
|
||||
}
|
||||
}
|
||||
|
||||
// WithOptions with gRPC options.
|
||||
func WithOptions(opts ...grpc.DialOption) ClientOption {
|
||||
return func(o *clientOptions) {
|
||||
@ -61,6 +68,7 @@ type clientOptions struct {
|
||||
timeout time.Duration
|
||||
middleware middleware.Middleware
|
||||
discovery registry.Discovery
|
||||
ints []grpc.UnaryClientInterceptor
|
||||
grpcOpts []grpc.DialOption
|
||||
}
|
||||
|
||||
@ -84,9 +92,15 @@ func dial(ctx context.Context, insecure bool, opts ...ClientOption) (*grpc.Clien
|
||||
for _, o := range opts {
|
||||
o(&options)
|
||||
}
|
||||
var ints = []grpc.UnaryClientInterceptor{
|
||||
unaryClientInterceptor(options.middleware, options.timeout),
|
||||
}
|
||||
if len(options.ints) > 0 {
|
||||
ints = append(ints, options.ints...)
|
||||
}
|
||||
var grpcOpts = []grpc.DialOption{
|
||||
grpc.WithBalancerName(roundrobin.Name),
|
||||
grpc.WithUnaryInterceptor(unaryClientInterceptor(options.middleware, options.timeout)),
|
||||
grpc.WithChainUnaryInterceptor(ints...),
|
||||
}
|
||||
if options.discovery != nil {
|
||||
grpcOpts = append(grpcOpts, grpc.WithResolvers(discovery.NewBuilder(options.discovery)))
|
||||
|
@ -61,8 +61,7 @@ func Middleware(m ...middleware.Middleware) ServerOption {
|
||||
}
|
||||
}
|
||||
|
||||
// UnaryInterceptor returns a ServerOption that sets the UnaryServerInterceptor for the
|
||||
// server.
|
||||
// UnaryInterceptor returns a ServerOption that sets the UnaryServerInterceptor for the server.
|
||||
func UnaryInterceptor(in ...grpc.UnaryServerInterceptor) ServerOption {
|
||||
return func(s *Server) {
|
||||
s.ints = in
|
||||
|
@ -145,7 +145,7 @@ func NewClient(ctx context.Context, opts ...ClientOption) (*Client, error) {
|
||||
return nil, err
|
||||
}
|
||||
var r *resolver
|
||||
if target.Endpoint != "" && options.discovery != nil {
|
||||
if options.discovery != nil {
|
||||
if target.Scheme == "discovery" {
|
||||
if r, err = newResolver(ctx, options.discovery, target); err != nil {
|
||||
return nil, fmt.Errorf("[http client] new resolver failed!err: %v", options.endpoint)
|
||||
|
Loading…
x
Reference in New Issue
Block a user