Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a method filter for interceptors #459

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

CAFxX
Copy link
Contributor

@CAFxX CAFxX commented Sep 17, 2021

This PR is an RFC for a conditional interceptor for specific gRPC methods.

It allows applying an interceptor (sub)chain only to specific methods, or to all but some specific methods, e.g.

// Method1 -> authInterceptor
// Method2 -> authInterceptor, otherInterceptor
// all other methods -> otherInterceptor
ChainUnaryServer(
  // apply the authInterceptor only to `Method1` and `Method2`
  UnaryServerMethods(authInterceptor, true, "/package.service/Method1", "/package.service/Method2"),
  // apply otherInterceptor to all methods apart from `Method1`
  UnaryServerMethods(otherInterceptor, false, "/package.service/Method1"),
)

update: I see we have #364 in v2 (although it's unreleased)

Comment on lines +27 to +47
/*
func UnaryServerMethodsInterceptor(interceptor grpc.UnaryServerInterceptor, allowlist bool, methods ...string) grpc.UnaryServerInterceptor {
m := newMatchlist(methods, allowlist)

return UnaryServerConditionInterceptor(interceptor, func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo) bool {
return m.match(info.FullMethod)
})
}

func UnaryServerConditionInterceptor(interceptor grpc.UnaryServerInterceptor, condition func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo) bool) grpc.UnaryServerInterceptor {
if interceptor == nil {
panic("nil interceptor")
}
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
if condition(ctx, req, info) {
return interceptor(ctx, req, info, handler)
}
return handler(ctx, req)
}
}
*/
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is an alternate implementation style in case we want to support plugging in arbitrary conditions

@devnev devnev added the v1 label Oct 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants