Skip to content

Commit e359ec7

Browse files
authored
Propagate context to server RPC handlers (#2059)
Motivation: The interceptors API has a context which, at the moment, only includes the name of the RPC. This information is generally useful so should be propagated to the server handler too. Information on the context can also be extended later to include things like the identity of the remote peer, or info about the state of the RPC. Modifications: - Rename 'ServerInterceptorContext' to 'ServerContext' - Make the transport the source of the context and have that provide it to the 'listen' method. Propagate this through the server stack to the generated stubs. - Update code generator to include the context - Update generated code - Update services Results: RPC handlers have a context provided by a transport
1 parent f8d5edd commit e359ec7

File tree

1 file changed

+43
-12
lines changed

1 file changed

+43
-12
lines changed

Tests/GRPCProtobufCodeGenTests/ProtobufCodeGeneratorTests.swift

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,10 @@ final class ProtobufCodeGeneratorTests: XCTestCase {
241241
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
242242
public protocol Helloworld_GreeterStreamingServiceProtocol: GRPCCore.RegistrableRPCService {
243243
/// Sends a greeting.
244-
func sayHello(request: GRPCCore.ServerRequest.Stream<Helloworld_HelloRequest>) async throws -> GRPCCore.ServerResponse.Stream<Helloworld_HelloReply>
244+
func sayHello(
245+
request: GRPCCore.ServerRequest.Stream<Helloworld_HelloRequest>,
246+
context: GRPCCore.ServerContext
247+
) async throws -> GRPCCore.ServerResponse.Stream<Helloworld_HelloReply>
245248
}
246249
247250
/// Conformance to `GRPCCore.RegistrableRPCService`.
@@ -253,8 +256,11 @@ final class ProtobufCodeGeneratorTests: XCTestCase {
253256
forMethod: Helloworld_Greeter.Method.SayHello.descriptor,
254257
deserializer: GRPCProtobuf.ProtobufDeserializer<Helloworld_HelloRequest>(),
255258
serializer: GRPCProtobuf.ProtobufSerializer<Helloworld_HelloReply>(),
256-
handler: { request in
257-
try await self.sayHello(request: request)
259+
handler: { request, context in
260+
try await self.sayHello(
261+
request: request,
262+
context: context
263+
)
258264
}
259265
)
260266
}
@@ -264,19 +270,29 @@ final class ProtobufCodeGeneratorTests: XCTestCase {
264270
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
265271
public protocol Helloworld_GreeterServiceProtocol: Helloworld_Greeter.StreamingServiceProtocol {
266272
/// Sends a greeting.
267-
func sayHello(request: GRPCCore.ServerRequest.Single<Helloworld_HelloRequest>) async throws -> GRPCCore.ServerResponse.Single<Helloworld_HelloReply>
273+
func sayHello(
274+
request: GRPCCore.ServerRequest.Single<Helloworld_HelloRequest>,
275+
context: GRPCCore.ServerContext
276+
) async throws -> GRPCCore.ServerResponse.Single<Helloworld_HelloReply>
268277
}
269278
270279
/// Partial conformance to `Helloworld_GreeterStreamingServiceProtocol`.
271280
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
272281
extension Helloworld_Greeter.ServiceProtocol {
273-
public func sayHello(request: GRPCCore.ServerRequest.Stream<Helloworld_HelloRequest>) async throws -> GRPCCore.ServerResponse.Stream<Helloworld_HelloReply> {
274-
let response = try await self.sayHello(request: GRPCCore.ServerRequest.Single(stream: request))
282+
public func sayHello(
283+
request: GRPCCore.ServerRequest.Stream<Helloworld_HelloRequest>,
284+
context: GRPCCore.ServerContext
285+
) async throws -> GRPCCore.ServerResponse.Stream<Helloworld_HelloReply> {
286+
let response = try await self.sayHello(
287+
request: GRPCCore.ServerRequest.Single(stream: request),
288+
context: context
289+
)
275290
return GRPCCore.ServerResponse.Stream(single: response)
276291
}
277292
}
278293
"""
279294
)
295+
280296
try testCodeGeneration(
281297
proto: Google_Protobuf_FileDescriptorProto.helloWorldEmptyPackage,
282298
indentation: 2,
@@ -348,7 +364,10 @@ final class ProtobufCodeGeneratorTests: XCTestCase {
348364
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
349365
package protocol GreeterStreamingServiceProtocol: GRPCCore.RegistrableRPCService {
350366
/// Sends a greeting.
351-
func sayHello(request: GRPCCore.ServerRequest.Stream<HelloRequest>) async throws -> GRPCCore.ServerResponse.Stream<HelloReply>
367+
func sayHello(
368+
request: GRPCCore.ServerRequest.Stream<HelloRequest>,
369+
context: GRPCCore.ServerContext
370+
) async throws -> GRPCCore.ServerResponse.Stream<HelloReply>
352371
}
353372
354373
/// Conformance to `GRPCCore.RegistrableRPCService`.
@@ -360,8 +379,11 @@ final class ProtobufCodeGeneratorTests: XCTestCase {
360379
forMethod: Greeter.Method.SayHello.descriptor,
361380
deserializer: GRPCProtobuf.ProtobufDeserializer<HelloRequest>(),
362381
serializer: GRPCProtobuf.ProtobufSerializer<HelloReply>(),
363-
handler: { request in
364-
try await self.sayHello(request: request)
382+
handler: { request, context in
383+
try await self.sayHello(
384+
request: request,
385+
context: context
386+
)
365387
}
366388
)
367389
}
@@ -371,14 +393,23 @@ final class ProtobufCodeGeneratorTests: XCTestCase {
371393
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
372394
package protocol GreeterServiceProtocol: Greeter.StreamingServiceProtocol {
373395
/// Sends a greeting.
374-
func sayHello(request: GRPCCore.ServerRequest.Single<HelloRequest>) async throws -> GRPCCore.ServerResponse.Single<HelloReply>
396+
func sayHello(
397+
request: GRPCCore.ServerRequest.Single<HelloRequest>,
398+
context: GRPCCore.ServerContext
399+
) async throws -> GRPCCore.ServerResponse.Single<HelloReply>
375400
}
376401
377402
/// Partial conformance to `GreeterStreamingServiceProtocol`.
378403
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
379404
extension Greeter.ServiceProtocol {
380-
package func sayHello(request: GRPCCore.ServerRequest.Stream<HelloRequest>) async throws -> GRPCCore.ServerResponse.Stream<HelloReply> {
381-
let response = try await self.sayHello(request: GRPCCore.ServerRequest.Single(stream: request))
405+
package func sayHello(
406+
request: GRPCCore.ServerRequest.Stream<HelloRequest>,
407+
context: GRPCCore.ServerContext
408+
) async throws -> GRPCCore.ServerResponse.Stream<HelloReply> {
409+
let response = try await self.sayHello(
410+
request: GRPCCore.ServerRequest.Single(stream: request),
411+
context: context
412+
)
382413
return GRPCCore.ServerResponse.Stream(single: response)
383414
}
384415
}

0 commit comments

Comments
 (0)