-
Notifications
You must be signed in to change notification settings - Fork 420
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
Allow adding ServerInterceptor
s to specific services and methods
#2096
Merged
glbrntt
merged 12 commits into
grpc:main
from
gjcairo:service-specific-server-interceptors
Nov 13, 2024
Merged
Changes from 8 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
ff0b18b
Add ServerInterceptorTarget
gjcairo 5dcf129
Fix up test
gjcairo 255d585
Nicer overloads for adding interceptors to server
gjcairo 07c68ed
Don't push ServerInterceptorTargets down into the RPC executor
gjcairo 9cf1ae5
Rename target to operation
gjcairo def2ced
Precompute interceptors when creating Server
gjcairo 9475866
Formatting
gjcairo 817291a
PR changes
gjcairo ce490cf
PR changes
gjcairo 2eb1ef2
Fix docs
gjcairo 8d3dfa2
Formatting
gjcairo a5c525d
Merge branch 'main' into service-specific-server-interceptors
glbrntt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
Sources/GRPCCore/Call/Server/ServerInterceptorOperation.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* Copyright 2024, gRPC Authors All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/// A `ServerInterceptorOperation` describes to which RPCs a server interceptor should be applied. | ||
/// | ||
/// You can configure a server interceptor to be applied to: | ||
/// - all RPCs and services; | ||
/// - requests directed only to specific services registered with your server; or | ||
/// - requests directed only to specific methods (of a specific service). | ||
/// | ||
/// - SeeAlso: ``ServerInterceptor`` for more information on server interceptors, and | ||
/// ``ClientInterceptorOperation`` for the client-side version of this type. | ||
public struct ServerInterceptorOperation: Sendable { | ||
gjcairo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// The subject of a ``ServerInterceptorOperation``. | ||
/// The subject of an interceptor can either be all services and methods, only specific services, or only specific methods. | ||
public struct Subject: Sendable { | ||
internal enum Wrapped: Sendable { | ||
case all | ||
case services([ServiceDescriptor]) | ||
case methods([MethodDescriptor]) | ||
gjcairo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
private let wrapped: Wrapped | ||
|
||
/// An operation subject specifying an interceptor that applies to all RPCs across all services will be registered with this server. | ||
public static var all: Self { .init(wrapped: .all) } | ||
|
||
/// An operation subject specifying an interceptor that will be applied only to RPCs directed to the specified services. | ||
/// - Parameters: | ||
/// - services: The list of service names for which this interceptor should intercept RPCs. | ||
/// - Returns: A ``ServerInterceptorOperation``. | ||
public static func services(_ services: [ServiceDescriptor]) -> Self { | ||
Self(wrapped: .services(services)) | ||
} | ||
|
||
/// An operation subject specifying an interceptor that will be applied only to RPCs directed to the specified service methods. | ||
/// - Parameters: | ||
/// - methods: The list of method descriptors for which this interceptor should intercept RPCs. | ||
/// - Returns: A ``ServerInterceptorOperation``. | ||
public static func methods(_ methods: [MethodDescriptor]) -> Self { | ||
Self(wrapped: .methods(methods)) | ||
} | ||
|
||
internal func applies(to descriptor: MethodDescriptor) -> Bool { | ||
switch self.wrapped { | ||
case .all: | ||
return true | ||
|
||
case .services(let services): | ||
return services.map({ $0.fullyQualifiedService }).contains(descriptor.service) | ||
|
||
case .methods(let methods): | ||
return methods.contains(descriptor) | ||
} | ||
} | ||
} | ||
|
||
/// The interceptor specified for this operation. | ||
public let interceptor: any ServerInterceptor | ||
|
||
private let subject: Subject | ||
|
||
private init(interceptor: any ServerInterceptor, appliesTo: Subject) { | ||
self.interceptor = interceptor | ||
self.subject = appliesTo | ||
} | ||
|
||
/// Create an operation, specifying which ``ServerInterceptor`` to apply and to which ``Subject``. | ||
/// - Parameters: | ||
/// - interceptor: The ``ServerInterceptor`` to register with the server. | ||
/// - subject: The ``Subject`` to which the `interceptor` applies. | ||
/// - Returns: A ``ServerInterceptorOperation``. | ||
public static func apply(_ interceptor: any ServerInterceptor, to subject: Subject) -> Self { | ||
Self(interceptor: interceptor, appliesTo: subject) | ||
} | ||
|
||
/// Returns whether this ``ServerInterceptorOperation`` applies to the given `descriptor`. | ||
/// - Parameter descriptor: A ``MethodDescriptor`` for which to test whether this interceptor applies. | ||
/// - Returns: `true` if this interceptor applies to the given `descriptor`, or `false` otherwise. | ||
public func _applies(to descriptor: MethodDescriptor) -> Bool { | ||
gjcairo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.subject.applies(to: descriptor) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now we have two APIs for configuring the router:
I think we need to make this method
public
so that users can register interceptors using path (2).One knock on from this is that we either need to document very clearly that the ordering of
registerHandler
andregisterInterceptors
are important, or we force users to init the router with the interceptor pipeline.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. I think there's value in letting users register the interceptors manually. I've added docs - let me know if you think they're not enough.