Skip to content

Commit ce490cf

Browse files
committed
PR changes
1 parent 817291a commit ce490cf

File tree

5 files changed

+34
-20
lines changed

5 files changed

+34
-20
lines changed

Sources/GRPCCore/Call/Server/RPCRouter.swift

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
/// the router has a handler for a method with ``hasHandler(forMethod:)`` or get a list of all
2323
/// methods with handlers registered by calling ``methods``. You can also remove the handler for a
2424
/// given method by calling ``removeHandler(forMethod:)``.
25+
/// You can also register any interceptors that you want applied to registered handlers via the
26+
/// ``registerInterceptors(pipeline:)`` method.
2527
///
2628
/// In most cases you won't need to interact with the router directly. Instead you should register
2729
/// your services with ``GRPCServer/init(transport:services:interceptors:)`` which will in turn
@@ -145,10 +147,19 @@ public struct RPCRouter: Sendable {
145147
return self.handlers.removeValue(forKey: descriptor) != nil
146148
}
147149

150+
/// Registers applicable interceptors to all currently-registered handlers.
151+
///
152+
/// - Important: Calling this method will apply the interceptors only to existing handlers. Any handlers registered via
153+
/// ``registerHandler(forMethod:deserializer:serializer:handler:)`` _after_ calling this method will not have
154+
/// any interceptors applied to them. If you want to make sure all registered methods have any applicable interceptors applied,
155+
/// only call this method _after_ you have registered all handlers.
156+
/// - Parameter pipeline: The interceptor pipeline operations to register to all currently-registered handlers. The order of the
157+
/// interceptors matters.
158+
/// - SeeAlso: ``ServerInterceptorPipelineOperation``.
148159
@inlinable
149-
mutating func registerInterceptors(pipeline: [ServerInterceptorOperation]) {
160+
public mutating func registerInterceptors(pipeline: [ServerInterceptorPipelineOperation]) {
150161
for descriptor in self.handlers.keys {
151-
let applicableOperations = pipeline.filter { $0._applies(to: descriptor) }
162+
let applicableOperations = pipeline.filter { $0.applies(to: descriptor) }
152163
if !applicableOperations.isEmpty {
153164
self.handlers[descriptor]?.interceptors = applicableOperations.map { $0.interceptor }
154165
}

Sources/GRPCCore/Call/Server/ServerInterceptor.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
/// been returned from a service. They are typically used for cross-cutting concerns like filtering
2222
/// requests, validating messages, logging additional data, and tracing.
2323
///
24-
/// Interceptors can be registered with the server either directly or via ``ServerInterceptorOperation``s.
24+
/// Interceptors can be registered with the server either directly or via ``ServerInterceptorPipelineOperation``s.
2525
/// You may register them for all services registered with a server, for RPCs directed to specific services, or
2626
/// for RPCs directed to specific methods. If you need to modify the behavior of an interceptor on a
2727
/// per-RPC basis in more detail, then you can use the ``ServerContext/descriptor`` to determine

Sources/GRPCCore/Call/Server/ServerInterceptorOperation.swift renamed to Sources/GRPCCore/Call/Server/ServerInterceptorPipelineOperation.swift

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,23 @@
1414
* limitations under the License.
1515
*/
1616

17-
/// A `ServerInterceptorOperation` describes to which RPCs a server interceptor should be applied.
17+
/// A `ServerInterceptorPipelineOperation` describes to which RPCs a server interceptor should be applied.
1818
///
1919
/// You can configure a server interceptor to be applied to:
2020
/// - all RPCs and services;
2121
/// - requests directed only to specific services registered with your server; or
2222
/// - requests directed only to specific methods (of a specific service).
2323
///
2424
/// - SeeAlso: ``ServerInterceptor`` for more information on server interceptors, and
25-
/// ``ClientInterceptorOperation`` for the client-side version of this type.
26-
public struct ServerInterceptorOperation: Sendable {
27-
/// The subject of a ``ServerInterceptorOperation``.
25+
/// ``ClientInterceptorPipelineOperation`` for the client-side version of this type.
26+
public struct ServerInterceptorPipelineOperation: Sendable {
27+
/// The subject of a ``ServerInterceptorPipelineOperation``.
2828
/// The subject of an interceptor can either be all services and methods, only specific services, or only specific methods.
2929
public struct Subject: Sendable {
3030
internal enum Wrapped: Sendable {
3131
case all
32-
case services([ServiceDescriptor])
33-
case methods([MethodDescriptor])
32+
case services(Set<ServiceDescriptor>)
33+
case methods(Set<MethodDescriptor>)
3434
}
3535

3636
private let wrapped: Wrapped
@@ -41,19 +41,20 @@ public struct ServerInterceptorOperation: Sendable {
4141
/// An operation subject specifying an interceptor that will be applied only to RPCs directed to the specified services.
4242
/// - Parameters:
4343
/// - services: The list of service names for which this interceptor should intercept RPCs.
44-
/// - Returns: A ``ServerInterceptorOperation``.
45-
public static func services(_ services: [ServiceDescriptor]) -> Self {
44+
/// - Returns: A ``ServerInterceptorPipelineOperation``.
45+
public static func services(_ services: Set<ServiceDescriptor>) -> Self {
4646
Self(wrapped: .services(services))
4747
}
4848

4949
/// An operation subject specifying an interceptor that will be applied only to RPCs directed to the specified service methods.
5050
/// - Parameters:
5151
/// - methods: The list of method descriptors for which this interceptor should intercept RPCs.
52-
/// - Returns: A ``ServerInterceptorOperation``.
53-
public static func methods(_ methods: [MethodDescriptor]) -> Self {
52+
/// - Returns: A ``ServerInterceptorPipelineOperation``.
53+
public static func methods(_ methods: Set<MethodDescriptor>) -> Self {
5454
Self(wrapped: .methods(methods))
5555
}
5656

57+
@usableFromInline
5758
internal func applies(to descriptor: MethodDescriptor) -> Bool {
5859
switch self.wrapped {
5960
case .all:
@@ -71,7 +72,8 @@ public struct ServerInterceptorOperation: Sendable {
7172
/// The interceptor specified for this operation.
7273
public let interceptor: any ServerInterceptor
7374

74-
private let subject: Subject
75+
@usableFromInline
76+
internal let subject: Subject
7577

7678
private init(interceptor: any ServerInterceptor, appliesTo: Subject) {
7779
self.interceptor = interceptor
@@ -82,15 +84,16 @@ public struct ServerInterceptorOperation: Sendable {
8284
/// - Parameters:
8385
/// - interceptor: The ``ServerInterceptor`` to register with the server.
8486
/// - subject: The ``Subject`` to which the `interceptor` applies.
85-
/// - Returns: A ``ServerInterceptorOperation``.
87+
/// - Returns: A ``ServerInterceptorPipelineOperation``.
8688
public static func apply(_ interceptor: any ServerInterceptor, to subject: Subject) -> Self {
8789
Self(interceptor: interceptor, appliesTo: subject)
8890
}
8991

90-
/// Returns whether this ``ServerInterceptorOperation`` applies to the given `descriptor`.
92+
/// Returns whether this ``ServerInterceptorPipelineOperation`` applies to the given `descriptor`.
9193
/// - Parameter descriptor: A ``MethodDescriptor`` for which to test whether this interceptor applies.
9294
/// - Returns: `true` if this interceptor applies to the given `descriptor`, or `false` otherwise.
93-
public func _applies(to descriptor: MethodDescriptor) -> Bool {
95+
@inlinable
96+
internal func applies(to descriptor: MethodDescriptor) -> Bool {
9497
self.subject.applies(to: descriptor)
9598
}
9699
}

Sources/GRPCCore/GRPCServer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ public final class GRPCServer: Sendable {
166166
public convenience init(
167167
transport: any ServerTransport,
168168
services: [any RegistrableRPCService],
169-
interceptorPipeline: [ServerInterceptorOperation]
169+
interceptorPipeline: [ServerInterceptorPipelineOperation]
170170
) {
171171
var router = RPCRouter()
172172
for service in services {

Tests/GRPCCoreTests/GRPCServerTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import XCTest
2222
final class GRPCServerTests: XCTestCase {
2323
func withInProcessClientConnectedToServer(
2424
services: [any RegistrableRPCService],
25-
interceptorPipeline: [ServerInterceptorOperation] = [],
25+
interceptorPipeline: [ServerInterceptorPipelineOperation] = [],
2626
_ body: (InProcessTransport.Client, GRPCServer) async throws -> Void
2727
) async throws {
2828
let inProcess = InProcessTransport()
@@ -554,7 +554,7 @@ struct ServerTests {
554554

555555
func withInProcessClientConnectedToServer(
556556
services: [any RegistrableRPCService],
557-
interceptorPipeline: [ServerInterceptorOperation] = [],
557+
interceptorPipeline: [ServerInterceptorPipelineOperation] = [],
558558
_ body: (InProcessTransport.Client, GRPCServer) async throws -> Void
559559
) async throws {
560560
let inProcess = InProcessTransport()

0 commit comments

Comments
 (0)