Skip to content

Commit 971b53e

Browse files
authored
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
1 parent 573d430 commit 971b53e

File tree

6 files changed

+43
-28
lines changed

6 files changed

+43
-28
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2024, gRPC Authors All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/// A context passed to the client containing additional information about the RPC.
18+
public struct ClientContext: Sendable {
19+
/// A description of the method being called.
20+
public var descriptor: MethodDescriptor
21+
22+
/// Create a new client interceptor context.
23+
public init(descriptor: MethodDescriptor) {
24+
self.descriptor = descriptor
25+
}
26+
}

Sources/GRPCCore/Call/Client/ClientInterceptor.swift

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
///
2424
/// Interceptors are registered with a client and apply to all RPCs. If you need to modify the
2525
/// behavior of an interceptor on a per-RPC basis then you can use the
26-
/// ``ClientInterceptorContext/descriptor`` to determine which RPC is being called and
26+
/// ``ClientContext/descriptor`` to determine which RPC is being called and
2727
/// conditionalise behavior accordingly.
2828
///
2929
/// - TODO: Update example and documentation to show how to register an interceptor.
@@ -41,10 +41,10 @@
4141
///
4242
/// func intercept<Input: Sendable, Output: Sendable>(
4343
/// request: ClientRequest.Stream<Input>,
44-
/// context: ClientInterceptorContext,
44+
/// context: ClientContext,
4545
/// next: @Sendable (
4646
/// _ request: ClientRequest.Stream<Input>,
47-
/// _ context: ClientInterceptorContext
47+
/// _ context: ClientContext
4848
/// ) async throws -> ClientResponse.Stream<Output>
4949
/// ) async throws -> ClientResponse.Stream<Output> {
5050
/// // Fetch the metadata value and attach it.
@@ -66,10 +66,10 @@
6666
/// struct LoggingClientInterceptor: ClientInterceptor {
6767
/// func intercept<Input: Sendable, Output: Sendable>(
6868
/// request: ClientRequest.Stream<Input>,
69-
/// context: ClientInterceptorContext,
69+
/// context: ClientContext,
7070
/// next: @Sendable (
7171
/// _ request: ClientRequest.Stream<Input>,
72-
/// _ context: ClientInterceptorContext
72+
/// _ context: ClientContext
7373
/// ) async throws -> ClientResponse.Stream<Output>
7474
/// ) async throws -> ClientResponse.Stream<Output> {
7575
/// print("Invoking method '\(context.descriptor)'")
@@ -101,21 +101,10 @@ public protocol ClientInterceptor: Sendable {
101101
/// - Returns: A response object.
102102
func intercept<Input: Sendable, Output: Sendable>(
103103
request: ClientRequest.Stream<Input>,
104-
context: ClientInterceptorContext,
104+
context: ClientContext,
105105
next: (
106106
_ request: ClientRequest.Stream<Input>,
107-
_ context: ClientInterceptorContext
107+
_ context: ClientContext
108108
) async throws -> ClientResponse.Stream<Output>
109109
) async throws -> ClientResponse.Stream<Output>
110110
}
111-
112-
/// A context passed to client interceptors containing additional information about the RPC.
113-
public struct ClientInterceptorContext: Sendable {
114-
/// A description of the method being called.
115-
public var descriptor: MethodDescriptor
116-
117-
/// Create a new client interceptor context.
118-
public init(descriptor: MethodDescriptor) {
119-
self.descriptor = descriptor
120-
}
121-
}

Sources/GRPCCore/Call/Client/Internal/ClientRPCExecutor.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ extension ClientRPCExecutor {
124124
interceptors: [any ClientInterceptor],
125125
stream: RPCStream<ClientTransport.Inbound, ClientTransport.Outbound>
126126
) async -> ClientResponse.Stream<Output> {
127-
let context = ClientInterceptorContext(descriptor: method)
127+
let context = ClientContext(descriptor: method)
128128

129129
if interceptors.isEmpty {
130130
return await ClientStreamExecutor.execute(
@@ -160,12 +160,12 @@ extension ClientRPCExecutor {
160160
static func _intercept<Input, Output>(
161161
in group: inout TaskGroup<Void>,
162162
request: ClientRequest.Stream<Input>,
163-
context: ClientInterceptorContext,
163+
context: ClientContext,
164164
iterator: Array<any ClientInterceptor>.Iterator,
165165
finally: (
166166
_ group: inout TaskGroup<Void>,
167167
_ request: ClientRequest.Stream<Input>,
168-
_ context: ClientInterceptorContext
168+
_ context: ClientContext
169169
) async -> ClientResponse.Stream<Output>
170170
) async -> ClientResponse.Stream<Output> {
171171
var iterator = iterator

Sources/GRPCCore/Call/Client/Internal/ClientStreamExecutor.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ internal enum ClientStreamExecutor {
2929
static func execute<Input: Sendable, Output: Sendable>(
3030
in group: inout TaskGroup<Void>,
3131
request: ClientRequest.Stream<Input>,
32-
context: ClientInterceptorContext,
32+
context: ClientContext,
3333
attempt: Int,
3434
serializer: some MessageSerializer<Input>,
3535
deserializer: some MessageDeserializer<Output>,

Sources/GRPCInterceptors/ClientTracingInterceptor.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ public struct ClientTracingInterceptor: ClientInterceptor {
4545
/// that has been configured when bootstrapping `swift-distributed-tracing` in your application.
4646
public func intercept<Input, Output>(
4747
request: ClientRequest.Stream<Input>,
48-
context: ClientInterceptorContext,
48+
context: ClientContext,
4949
next: (
5050
ClientRequest.Stream<Input>,
51-
ClientInterceptorContext
51+
ClientContext
5252
) async throws -> ClientResponse.Stream<Output>
5353
) async throws -> ClientResponse.Stream<Output> where Input: Sendable, Output: Sendable {
5454
var request = request

Tests/GRPCCoreTests/Test Utilities/Call/Client/ClientInterceptors.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ struct RejectAllClientInterceptor: ClientInterceptor {
5151

5252
func intercept<Input: Sendable, Output: Sendable>(
5353
request: ClientRequest.Stream<Input>,
54-
context: ClientInterceptorContext,
54+
context: ClientContext,
5555
next: (
5656
ClientRequest.Stream<Input>,
57-
ClientInterceptorContext
57+
ClientContext
5858
) async throws -> ClientResponse.Stream<Output>
5959
) async throws -> ClientResponse.Stream<Output> {
6060
if self.throw {
@@ -76,10 +76,10 @@ struct RequestCountingClientInterceptor: ClientInterceptor {
7676

7777
func intercept<Input: Sendable, Output: Sendable>(
7878
request: ClientRequest.Stream<Input>,
79-
context: ClientInterceptorContext,
79+
context: ClientContext,
8080
next: (
8181
ClientRequest.Stream<Input>,
82-
ClientInterceptorContext
82+
ClientContext
8383
) async throws -> ClientResponse.Stream<Output>
8484
) async throws -> ClientResponse.Stream<Output> {
8585
self.counter.increment()

0 commit comments

Comments
 (0)