Skip to content

Commit

Permalink
Rename target to operation
Browse files Browse the repository at this point in the history
  • Loading branch information
gjcairo committed Oct 29, 2024
1 parent 7ae7ca8 commit 852425a
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 60 deletions.
2 changes: 1 addition & 1 deletion Sources/GRPCCore/Call/Server/ServerInterceptor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
/// been returned from a service. They are typically used for cross-cutting concerns like filtering
/// requests, validating messages, logging additional data, and tracing.
///
/// Interceptors are registered with the server via ``ServerInterceptorTarget``s.
/// Interceptors can be registered with the server either directly or via ``ServerInterceptorOperation``s.
/// You may register them for all services registered with a server, for RPCs directed to specific services, or
/// for RPCs directed to specific methods. If you need to modify the behavior of an interceptor on a
/// per-RPC basis in more detail, then you can use the ``ServerContext/descriptor`` to determine
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,39 +14,39 @@
* limitations under the License.
*/

/// A `ServerInterceptorTarget` describes to which RPCs a server interceptor should be applied.
/// 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
/// ``ClientInterceptorTarget`` for the client-side version of this type.
public struct ServerInterceptorTarget: Sendable {
/// ``ClientInterceptorOperation`` for the client-side version of this type.
public struct ServerInterceptorOperation: Sendable {
internal enum Wrapped: Sendable {
case allServices(interceptor: any ServerInterceptor)
case serviceSpecific(interceptor: any ServerInterceptor, services: [String])
case methodSpecific(interceptor: any ServerInterceptor, methods: [MethodDescriptor])
}

/// A target specifying an interceptor that applies to all RPCs across all services registered with this server.
/// An operation specifying an interceptor that applies to all RPCs across all services will be registered with this server.
/// - Parameter interceptor: The interceptor to register with the server.
/// - Returns: A ``ServerInterceptorTarget``.
public static func allServices(
interceptor: any ServerInterceptor
/// - Returns: A ``ServerInterceptorOperation``.
public static func applyToAllServices(
_ interceptor: any ServerInterceptor
) -> Self {
Self(wrapped: .allServices(interceptor: interceptor))
}

/// A target specifying an interceptor that applies to RPCs directed only to the specified services.
/// An operation specifying an interceptor that will be applied only to RPCs directed to the specified services.
/// - Parameters:
/// - interceptor: The interceptor to register with the server.
/// - services: The list of service names for which this interceptor should intercept RPCs.
/// - Returns: A ``ServerInterceptorTarget``.
public static func serviceSpecific(
interceptor: any ServerInterceptor,
services: [String]
/// - Returns: A ``ServerInterceptorOperation``.
public static func apply(
_ interceptor: any ServerInterceptor,
onlyToServices services: [String]
) -> Self {
Self(
wrapped: .serviceSpecific(
Expand All @@ -56,14 +56,14 @@ public struct ServerInterceptorTarget: Sendable {
)
}

/// A target specifying an interceptor that applies to RPCs directed only to the specified service methods.
/// An operation specifying an interceptor that will be applied only to RPCs directed to the specified service methods.
/// - Parameters:
/// - interceptor: The interceptor to register with the server.
/// - services: The list of method descriptors for which this interceptor should intercept RPCs.
/// - Returns: A ``ServerInterceptorTarget``.
public static func methodSpecific(
interceptor: any ServerInterceptor,
methods: [MethodDescriptor]
/// - Returns: A ``ServerInterceptorOperation``.
public static func apply(
_ interceptor: any ServerInterceptor,
onlyToMethods methods: [MethodDescriptor]
) -> Self {
Self(
wrapped: .methodSpecific(
Expand All @@ -79,7 +79,7 @@ public struct ServerInterceptorTarget: Sendable {
self.wrapped = wrapped
}

/// Get the ``ServerInterceptor`` associated with this ``ServerInterceptorTarget``.
/// Get the ``ServerInterceptor`` associated with this ``ServerInterceptorOperation``.
public var interceptor: any ServerInterceptor {
switch self.wrapped {
case .allServices(let interceptor):
Expand All @@ -91,7 +91,7 @@ public struct ServerInterceptorTarget: Sendable {
}
}

/// Returns whether this ``ServerInterceptorTarget`` applies to the given `descriptor`.
/// 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 {
Expand Down
22 changes: 13 additions & 9 deletions Sources/GRPCCore/GRPCServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ public final class GRPCServer: Sendable {
/// The services registered which the server is serving.
private let router: RPCRouter

/// A collection of ``ServerInterceptorTarget``s which may be applied to all accepted RPCs.
/// A collection of ``ServerInterceptorOperation``s which may be applied to all accepted RPCs.
///
/// RPCs are intercepted in the order that interceptors are added. That is, a request received
/// from the client will first be intercepted by the first added interceptor followed by the
/// second, and so on.
private let interceptors: [ServerInterceptorTarget]
private let interceptors: [ServerInterceptorOperation]

/// The state of the server.
private let state: Mutex<State>
Expand Down Expand Up @@ -156,7 +156,7 @@ public final class GRPCServer: Sendable {
self.init(
transport: transport,
services: services,
interceptors: interceptors.map { .allServices(interceptor: $0) }
interceptorPipeline: interceptors.map { .applyToAllServices($0) }
)
}

Expand All @@ -165,22 +165,26 @@ public final class GRPCServer: Sendable {
/// - Parameters:
/// - transport: The transport the server should listen on.
/// - services: Services offered by the server.
/// - interceptors: A collection of interceptors providing cross-cutting functionality to each
/// - interceptorPipeline: A collection of interceptors 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 convenience init(
transport: any ServerTransport,
services: [any RegistrableRPCService],
interceptors: [ServerInterceptorTarget]
interceptorPipeline: [ServerInterceptorOperation]
) {
var router = RPCRouter()
for service in services {
service.registerMethods(with: &router)
}

self.init(transport: transport, router: router, interceptors: interceptors)
self.init(
transport: transport,
router: router,
interceptorPipeline: interceptorPipeline
)
}

/// Creates a new server with no resources.
Expand All @@ -201,7 +205,7 @@ public final class GRPCServer: Sendable {
self.init(
transport: transport,
router: router,
interceptors: interceptors.map { .allServices(interceptor: $0)}
interceptorPipeline: interceptors.map { .applyToAllServices($0) }
)
}

Expand All @@ -218,12 +222,12 @@ public final class GRPCServer: Sendable {
public init(
transport: any ServerTransport,
router: RPCRouter,
interceptors: [ServerInterceptorTarget]
interceptorPipeline: [ServerInterceptorOperation]
) {
self.state = Mutex(.notStarted)
self.transport = transport
self.router = router
self.interceptors = interceptors
self.interceptors = interceptorPipeline
}

/// Starts the server and runs until the registered transport has closed.
Expand Down
62 changes: 31 additions & 31 deletions Tests/GRPCCoreTests/GRPCServerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ import XCTest
final class GRPCServerTests: XCTestCase {
func withInProcessClientConnectedToServer(
services: [any RegistrableRPCService],
interceptors: [ServerInterceptorTarget] = [],
interceptorPipeline: [ServerInterceptorOperation] = [],
_ body: (InProcessTransport.Client, GRPCServer) async throws -> Void
) async throws {
let inProcess = InProcessTransport()
let server = GRPCServer(
transport: inProcess.server,
services: services,
interceptors: interceptors
interceptorPipeline: interceptorPipeline
)

try await withThrowingTaskGroup(of: Void.self) { group in
Expand Down Expand Up @@ -220,10 +220,10 @@ final class GRPCServerTests: XCTestCase {

try await self.withInProcessClientConnectedToServer(
services: [BinaryEcho()],
interceptors: [
.allServices(interceptor: .requestCounter(counter1)),
.allServices(interceptor: .rejectAll(with: RPCError(code: .unavailable, message: ""))),
.allServices(interceptor: .requestCounter(counter2)),
interceptorPipeline: [
.applyToAllServices(.requestCounter(counter1)),
.applyToAllServices(.rejectAll(with: RPCError(code: .unavailable, message: ""))),
.applyToAllServices(.requestCounter(counter2)),
]
) { client, _ in
try await client.withStream(
Expand All @@ -249,7 +249,7 @@ final class GRPCServerTests: XCTestCase {

try await self.withInProcessClientConnectedToServer(
services: [BinaryEcho()],
interceptors: [.allServices(interceptor: .requestCounter(counter))]
interceptorPipeline: [.applyToAllServices(.requestCounter(counter))]
) { client, _ in
try await client.withStream(
descriptor: MethodDescriptor(service: "not", method: "implemented"),
Expand Down Expand Up @@ -387,19 +387,19 @@ struct ServerTests {

try await self.withInProcessClientConnectedToServer(
services: [BinaryEcho(), HelloWorld()],
interceptors: [
.serviceSpecific(
interceptor: .requestCounter(onlyBinaryEchoCounter),
services: [BinaryEcho.serviceName]
interceptorPipeline: [
.apply(
.requestCounter(onlyBinaryEchoCounter),
onlyToServices: [BinaryEcho.serviceName]
),
.allServices(interceptor: .requestCounter(allServicesCounter)),
.serviceSpecific(
interceptor: .requestCounter(onlyHelloWorldCounter),
services: [HelloWorld.serviceName]
.applyToAllServices(.requestCounter(allServicesCounter)),
.apply(
.requestCounter(onlyHelloWorldCounter),
onlyToServices: [HelloWorld.serviceName]
),
.serviceSpecific(
interceptor: .requestCounter(bothServicesCounter),
services: [BinaryEcho.serviceName, HelloWorld.serviceName]
.apply(
.requestCounter(bothServicesCounter),
onlyToServices: [BinaryEcho.serviceName, HelloWorld.serviceName]
),
]
) { client, _ in
Expand Down Expand Up @@ -474,19 +474,19 @@ struct ServerTests {

try await self.withInProcessClientConnectedToServer(
services: [BinaryEcho()],
interceptors: [
.methodSpecific(
interceptor: .requestCounter(onlyBinaryEchoGetCounter),
methods: [BinaryEcho.Methods.get]
interceptorPipeline: [
.apply(
.requestCounter(onlyBinaryEchoGetCounter),
onlyToMethods: [BinaryEcho.Methods.get]
),
.allServices(interceptor: .requestCounter(allMethodsCounter)),
.methodSpecific(
interceptor: .requestCounter(onlyBinaryEchoCollectCounter),
methods: [BinaryEcho.Methods.collect]
.applyToAllServices(.requestCounter(allMethodsCounter)),
.apply(
.requestCounter(onlyBinaryEchoCollectCounter),
onlyToMethods: [BinaryEcho.Methods.collect]
),
.methodSpecific(
interceptor: .requestCounter(bothBinaryEchoMethodsCounter),
methods: [BinaryEcho.Methods.get, BinaryEcho.Methods.collect]
.apply(
.requestCounter(bothBinaryEchoMethodsCounter),
onlyToMethods: [BinaryEcho.Methods.get, BinaryEcho.Methods.collect]
),
]
) { client, _ in
Expand Down Expand Up @@ -554,14 +554,14 @@ struct ServerTests {

func withInProcessClientConnectedToServer(
services: [any RegistrableRPCService],
interceptors: [ServerInterceptorTarget] = [],
interceptorPipeline: [ServerInterceptorOperation] = [],
_ body: (InProcessTransport.Client, GRPCServer) async throws -> Void
) async throws {
let inProcess = InProcessTransport()
let server = GRPCServer(
transport: inProcess.server,
services: services,
interceptors: interceptors
interceptorPipeline: interceptorPipeline
)

try await withThrowingTaskGroup(of: Void.self) { group in
Expand Down

0 comments on commit 852425a

Please sign in to comment.