From 971b53e4de9f40547574c9c3c0f0ec5d7dc6b881 Mon Sep 17 00:00:00 2001 From: George Barnett Date: Wed, 18 Sep 2024 13:03:23 +0100 Subject: [PATCH] Rename client interceptor context (#2061) Motivation: The `ServerInterceptorContext` was renamed to `ServerContext`. The same should be done for the client. Modifications: - Rename `ClientInterceptorContext` to `ClientContext` Result: More consistent naming --- .../GRPCCore/Call/Client/ClientContext.swift | 26 +++++++++++++++++++ .../Call/Client/ClientInterceptor.swift | 25 +++++------------- .../Client/Internal/ClientRPCExecutor.swift | 6 ++--- .../Internal/ClientStreamExecutor.swift | 2 +- .../ClientTracingInterceptor.swift | 4 +-- .../Call/Client/ClientInterceptors.swift | 8 +++--- 6 files changed, 43 insertions(+), 28 deletions(-) create mode 100644 Sources/GRPCCore/Call/Client/ClientContext.swift diff --git a/Sources/GRPCCore/Call/Client/ClientContext.swift b/Sources/GRPCCore/Call/Client/ClientContext.swift new file mode 100644 index 000000000..51eaa1a21 --- /dev/null +++ b/Sources/GRPCCore/Call/Client/ClientContext.swift @@ -0,0 +1,26 @@ +/* + * 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 context passed to the client containing additional information about the RPC. +public struct ClientContext: Sendable { + /// A description of the method being called. + public var descriptor: MethodDescriptor + + /// Create a new client interceptor context. + public init(descriptor: MethodDescriptor) { + self.descriptor = descriptor + } +} diff --git a/Sources/GRPCCore/Call/Client/ClientInterceptor.swift b/Sources/GRPCCore/Call/Client/ClientInterceptor.swift index 2876fbe76..93ddf9cf5 100644 --- a/Sources/GRPCCore/Call/Client/ClientInterceptor.swift +++ b/Sources/GRPCCore/Call/Client/ClientInterceptor.swift @@ -23,7 +23,7 @@ /// /// Interceptors are registered with a client and apply to all RPCs. If you need to modify the /// behavior of an interceptor on a per-RPC basis then you can use the -/// ``ClientInterceptorContext/descriptor`` to determine which RPC is being called and +/// ``ClientContext/descriptor`` to determine which RPC is being called and /// conditionalise behavior accordingly. /// /// - TODO: Update example and documentation to show how to register an interceptor. @@ -41,10 +41,10 @@ /// /// func intercept( /// request: ClientRequest.Stream, -/// context: ClientInterceptorContext, +/// context: ClientContext, /// next: @Sendable ( /// _ request: ClientRequest.Stream, -/// _ context: ClientInterceptorContext +/// _ context: ClientContext /// ) async throws -> ClientResponse.Stream /// ) async throws -> ClientResponse.Stream { /// // Fetch the metadata value and attach it. @@ -66,10 +66,10 @@ /// struct LoggingClientInterceptor: ClientInterceptor { /// func intercept( /// request: ClientRequest.Stream, -/// context: ClientInterceptorContext, +/// context: ClientContext, /// next: @Sendable ( /// _ request: ClientRequest.Stream, -/// _ context: ClientInterceptorContext +/// _ context: ClientContext /// ) async throws -> ClientResponse.Stream /// ) async throws -> ClientResponse.Stream { /// print("Invoking method '\(context.descriptor)'") @@ -101,21 +101,10 @@ public protocol ClientInterceptor: Sendable { /// - Returns: A response object. func intercept( request: ClientRequest.Stream, - context: ClientInterceptorContext, + context: ClientContext, next: ( _ request: ClientRequest.Stream, - _ context: ClientInterceptorContext + _ context: ClientContext ) async throws -> ClientResponse.Stream ) async throws -> ClientResponse.Stream } - -/// A context passed to client interceptors containing additional information about the RPC. -public struct ClientInterceptorContext: Sendable { - /// A description of the method being called. - public var descriptor: MethodDescriptor - - /// Create a new client interceptor context. - public init(descriptor: MethodDescriptor) { - self.descriptor = descriptor - } -} diff --git a/Sources/GRPCCore/Call/Client/Internal/ClientRPCExecutor.swift b/Sources/GRPCCore/Call/Client/Internal/ClientRPCExecutor.swift index 5811d20b1..4a7c958c4 100644 --- a/Sources/GRPCCore/Call/Client/Internal/ClientRPCExecutor.swift +++ b/Sources/GRPCCore/Call/Client/Internal/ClientRPCExecutor.swift @@ -124,7 +124,7 @@ extension ClientRPCExecutor { interceptors: [any ClientInterceptor], stream: RPCStream ) async -> ClientResponse.Stream { - let context = ClientInterceptorContext(descriptor: method) + let context = ClientContext(descriptor: method) if interceptors.isEmpty { return await ClientStreamExecutor.execute( @@ -160,12 +160,12 @@ extension ClientRPCExecutor { static func _intercept( in group: inout TaskGroup, request: ClientRequest.Stream, - context: ClientInterceptorContext, + context: ClientContext, iterator: Array.Iterator, finally: ( _ group: inout TaskGroup, _ request: ClientRequest.Stream, - _ context: ClientInterceptorContext + _ context: ClientContext ) async -> ClientResponse.Stream ) async -> ClientResponse.Stream { var iterator = iterator diff --git a/Sources/GRPCCore/Call/Client/Internal/ClientStreamExecutor.swift b/Sources/GRPCCore/Call/Client/Internal/ClientStreamExecutor.swift index c0f4b166a..433232d7c 100644 --- a/Sources/GRPCCore/Call/Client/Internal/ClientStreamExecutor.swift +++ b/Sources/GRPCCore/Call/Client/Internal/ClientStreamExecutor.swift @@ -29,7 +29,7 @@ internal enum ClientStreamExecutor { static func execute( in group: inout TaskGroup, request: ClientRequest.Stream, - context: ClientInterceptorContext, + context: ClientContext, attempt: Int, serializer: some MessageSerializer, deserializer: some MessageDeserializer, diff --git a/Sources/GRPCInterceptors/ClientTracingInterceptor.swift b/Sources/GRPCInterceptors/ClientTracingInterceptor.swift index 065d9936e..9da8a1f26 100644 --- a/Sources/GRPCInterceptors/ClientTracingInterceptor.swift +++ b/Sources/GRPCInterceptors/ClientTracingInterceptor.swift @@ -45,10 +45,10 @@ public struct ClientTracingInterceptor: ClientInterceptor { /// that has been configured when bootstrapping `swift-distributed-tracing` in your application. public func intercept( request: ClientRequest.Stream, - context: ClientInterceptorContext, + context: ClientContext, next: ( ClientRequest.Stream, - ClientInterceptorContext + ClientContext ) async throws -> ClientResponse.Stream ) async throws -> ClientResponse.Stream where Input: Sendable, Output: Sendable { var request = request diff --git a/Tests/GRPCCoreTests/Test Utilities/Call/Client/ClientInterceptors.swift b/Tests/GRPCCoreTests/Test Utilities/Call/Client/ClientInterceptors.swift index d8d355969..a45d64fd5 100644 --- a/Tests/GRPCCoreTests/Test Utilities/Call/Client/ClientInterceptors.swift +++ b/Tests/GRPCCoreTests/Test Utilities/Call/Client/ClientInterceptors.swift @@ -51,10 +51,10 @@ struct RejectAllClientInterceptor: ClientInterceptor { func intercept( request: ClientRequest.Stream, - context: ClientInterceptorContext, + context: ClientContext, next: ( ClientRequest.Stream, - ClientInterceptorContext + ClientContext ) async throws -> ClientResponse.Stream ) async throws -> ClientResponse.Stream { if self.throw { @@ -76,10 +76,10 @@ struct RequestCountingClientInterceptor: ClientInterceptor { func intercept( request: ClientRequest.Stream, - context: ClientInterceptorContext, + context: ClientContext, next: ( ClientRequest.Stream, - ClientInterceptorContext + ClientContext ) async throws -> ClientResponse.Stream ) async throws -> ClientResponse.Stream { self.counter.increment()