-
Notifications
You must be signed in to change notification settings - Fork 433
Allow adding ClientInterceptor
s to specific services and methods
#2113
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
Merged
glbrntt
merged 7 commits into
grpc:main
from
gjcairo:service-specific-client-interceptors
Nov 15, 2024
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5ec3bbb
Allow adding `ClientInterceptor`s to specific services and methods
gjcairo 296c825
Move applicable method interceptors to client state
gjcairo b7db7ab
Merge branch 'main' into service-specific-client-interceptors
glbrntt 6765abb
Add pipeline operation tests
gjcairo 6a0401d
Create StateMachine for GRPCClient
gjcairo 63c6148
Parametrise tests
gjcairo 2bf4d02
Formatting
gjcairo 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 hidden or 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
99 changes: 99 additions & 0 deletions
99
Sources/GRPCCore/Call/Client/ClientInterceptorPipelineOperation.swift
This file contains hidden or 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,99 @@ | ||
/* | ||
* 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 `ClientInterceptorPipelineOperation` describes to which RPCs a client interceptor should be applied. | ||
/// | ||
/// You can configure a client interceptor to be applied to: | ||
/// - all RPCs and services; | ||
/// - requests directed only to specific services; or | ||
/// - requests directed only to specific methods (of a specific service). | ||
/// | ||
/// - SeeAlso: ``ClientInterceptor`` for more information on client interceptors, and | ||
/// ``ServerInterceptorPipelineOperation`` for the server-side version of this type. | ||
public struct ClientInterceptorPipelineOperation: Sendable { | ||
/// The subject of a ``ClientInterceptorPipelineOperation``. | ||
/// 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(Set<ServiceDescriptor>) | ||
case methods(Set<MethodDescriptor>) | ||
} | ||
|
||
private let wrapped: Wrapped | ||
|
||
/// An operation subject specifying an interceptor that applies to all RPCs across all services will be registered with this client. | ||
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 ``ClientInterceptorPipelineOperation``. | ||
public static func services(_ services: Set<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 ``ClientInterceptorPipelineOperation``. | ||
public static func methods(_ methods: Set<MethodDescriptor>) -> Self { | ||
Self(wrapped: .methods(methods)) | ||
} | ||
|
||
@usableFromInline | ||
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 ClientInterceptor | ||
|
||
@usableFromInline | ||
internal let subject: Subject | ||
|
||
private init(interceptor: any ClientInterceptor, appliesTo: Subject) { | ||
self.interceptor = interceptor | ||
self.subject = appliesTo | ||
} | ||
|
||
/// Create an operation, specifying which ``ClientInterceptor`` to apply and to which ``Subject``. | ||
/// - Parameters: | ||
/// - interceptor: The ``ClientInterceptor`` to register with the client. | ||
/// - subject: The ``Subject`` to which the `interceptor` applies. | ||
/// - Returns: A ``ClientInterceptorPipelineOperation``. | ||
public static func apply(_ interceptor: any ClientInterceptor, to subject: Subject) -> Self { | ||
Self(interceptor: interceptor, appliesTo: subject) | ||
} | ||
|
||
/// Returns whether this ``ClientInterceptorPipelineOperation`` 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. | ||
@inlinable | ||
internal func applies(to descriptor: MethodDescriptor) -> Bool { | ||
self.subject.applies(to: descriptor) | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -112,13 +112,18 @@ public final class GRPCClient: Sendable { | |
/// The transport which provides a bidirectional communication channel with the server. | ||
private let transport: any ClientTransport | ||
|
||
/// A collection of interceptors providing cross-cutting functionality to each accepted RPC. | ||
private let interceptorPipeline: [ClientInterceptorPipelineOperation] | ||
|
||
/// A collection of interceptors providing cross-cutting functionality to each accepted RPC, keyed by the method to which they apply. | ||
/// | ||
/// The list of interceptors for each method is computed from `interceptorsPipeline` when calling a method for the first time. | ||
/// This caching is done to avoid having to compute the applicable interceptors for each request made. | ||
/// | ||
/// The order in which interceptors are added reflects the order in which they are called. The | ||
/// first interceptor added will be the first interceptor to intercept each request. The last | ||
/// interceptor added will be the final interceptor to intercept each request before calling | ||
/// the appropriate handler. | ||
private let interceptors: [any ClientInterceptor] | ||
private let interceptorsPerMethod: Mutex<[MethodDescriptor: [any ClientInterceptor]]> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought this caching could be useful to speed things up, similar to what we do in the |
||
|
||
/// The current state of the client. | ||
private let state: Mutex<State> | ||
|
@@ -191,17 +196,37 @@ public final class GRPCClient: Sendable { | |
/// | ||
/// - Parameters: | ||
/// - transport: The transport used to establish a communication channel with a server. | ||
/// - interceptors: A collection of interceptors providing cross-cutting functionality to each | ||
/// - interceptors: A collection of ``ClientInterceptor``s providing cross-cutting functionality to each | ||
/// accepted RPC. The order in which interceptors are added reflects the order in which they | ||
/// are called. The first interceptor added will be the first interceptor to intercept each | ||
/// request. The last interceptor added will be the final interceptor to intercept each | ||
/// request before calling the appropriate handler. | ||
public init( | ||
convenience public init( | ||
transport: some ClientTransport, | ||
interceptors: [any ClientInterceptor] = [] | ||
) { | ||
self.init( | ||
transport: transport, | ||
interceptorPipeline: interceptors.map { .apply($0, to: .all) } | ||
) | ||
} | ||
|
||
/// Creates a new client with the given transport, interceptors and configuration. | ||
/// | ||
/// - Parameters: | ||
/// - transport: The transport used to establish a communication channel with a server. | ||
/// - interceptorPipeline: A collection of ``ClientInterceptorPipelineOperation`` providing cross-cutting | ||
/// functionality to each accepted RPC. Only applicable interceptors from the pipeline will be applied to each RPC. | ||
/// The order in which interceptors are added reflects the order in which they are called. | ||
/// The first interceptor added will be the first interceptor to intercept each request. | ||
/// The last interceptor added will be the final interceptor to intercept each request before calling the appropriate handler. | ||
public init( | ||
transport: some ClientTransport, | ||
interceptorPipeline: [ClientInterceptorPipelineOperation] | ||
) { | ||
self.transport = transport | ||
self.interceptors = interceptors | ||
self.interceptorPipeline = interceptorPipeline | ||
self.interceptorsPerMethod = Mutex([:]) | ||
self.state = Mutex(.notStarted) | ||
} | ||
|
||
|
@@ -361,14 +386,26 @@ public final class GRPCClient: Sendable { | |
var options = options | ||
options.formUnion(with: methodConfig) | ||
|
||
let applicableInterceptors = self.interceptorsPerMethod.withLock { | ||
if let interceptors = $0[descriptor] { | ||
return interceptors | ||
} else { | ||
let interceptors = self.interceptorPipeline | ||
.filter { $0.applies(to: descriptor) } | ||
.map { $0.interceptor } | ||
$0[descriptor] = interceptors | ||
return interceptors | ||
} | ||
} | ||
|
||
return try await ClientRPCExecutor.execute( | ||
request: request, | ||
method: descriptor, | ||
options: options, | ||
serializer: serializer, | ||
deserializer: deserializer, | ||
transport: self.transport, | ||
interceptors: self.interceptors, | ||
interceptors: applicableInterceptors, | ||
handler: handler | ||
) | ||
} | ||
|
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.
Uh oh!
There was an error while loading. Please reload this page.