Skip to content

Commit 7789f1e

Browse files
authored
Propagate context to server RPC handlers (grpc#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 78da46d commit 7789f1e

File tree

43 files changed

+1133
-432
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1133
-432
lines changed

Examples/v2/echo/Generated/echo.grpc.swift

Lines changed: 76 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,28 @@ extension GRPCCore.ServiceDescriptor {
8686
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
8787
internal protocol Echo_EchoStreamingServiceProtocol: GRPCCore.RegistrableRPCService {
8888
/// Immediately returns an echo of a request.
89-
func get(request: GRPCCore.ServerRequest.Stream<Echo_EchoRequest>) async throws -> GRPCCore.ServerResponse.Stream<Echo_EchoResponse>
89+
func get(
90+
request: GRPCCore.ServerRequest.Stream<Echo_EchoRequest>,
91+
context: GRPCCore.ServerContext
92+
) async throws -> GRPCCore.ServerResponse.Stream<Echo_EchoResponse>
9093

9194
/// Splits a request into words and returns each word in a stream of messages.
92-
func expand(request: GRPCCore.ServerRequest.Stream<Echo_EchoRequest>) async throws -> GRPCCore.ServerResponse.Stream<Echo_EchoResponse>
95+
func expand(
96+
request: GRPCCore.ServerRequest.Stream<Echo_EchoRequest>,
97+
context: GRPCCore.ServerContext
98+
) async throws -> GRPCCore.ServerResponse.Stream<Echo_EchoResponse>
9399

94100
/// Collects a stream of messages and returns them concatenated when the caller closes.
95-
func collect(request: GRPCCore.ServerRequest.Stream<Echo_EchoRequest>) async throws -> GRPCCore.ServerResponse.Stream<Echo_EchoResponse>
101+
func collect(
102+
request: GRPCCore.ServerRequest.Stream<Echo_EchoRequest>,
103+
context: GRPCCore.ServerContext
104+
) async throws -> GRPCCore.ServerResponse.Stream<Echo_EchoResponse>
96105

97106
/// Streams back messages as they are received in an input stream.
98-
func update(request: GRPCCore.ServerRequest.Stream<Echo_EchoRequest>) async throws -> GRPCCore.ServerResponse.Stream<Echo_EchoResponse>
107+
func update(
108+
request: GRPCCore.ServerRequest.Stream<Echo_EchoRequest>,
109+
context: GRPCCore.ServerContext
110+
) async throws -> GRPCCore.ServerResponse.Stream<Echo_EchoResponse>
99111
}
100112

101113
/// Conformance to `GRPCCore.RegistrableRPCService`.
@@ -107,32 +119,44 @@ extension Echo_Echo.StreamingServiceProtocol {
107119
forMethod: Echo_Echo.Method.Get.descriptor,
108120
deserializer: GRPCProtobuf.ProtobufDeserializer<Echo_EchoRequest>(),
109121
serializer: GRPCProtobuf.ProtobufSerializer<Echo_EchoResponse>(),
110-
handler: { request in
111-
try await self.get(request: request)
122+
handler: { request, context in
123+
try await self.get(
124+
request: request,
125+
context: context
126+
)
112127
}
113128
)
114129
router.registerHandler(
115130
forMethod: Echo_Echo.Method.Expand.descriptor,
116131
deserializer: GRPCProtobuf.ProtobufDeserializer<Echo_EchoRequest>(),
117132
serializer: GRPCProtobuf.ProtobufSerializer<Echo_EchoResponse>(),
118-
handler: { request in
119-
try await self.expand(request: request)
133+
handler: { request, context in
134+
try await self.expand(
135+
request: request,
136+
context: context
137+
)
120138
}
121139
)
122140
router.registerHandler(
123141
forMethod: Echo_Echo.Method.Collect.descriptor,
124142
deserializer: GRPCProtobuf.ProtobufDeserializer<Echo_EchoRequest>(),
125143
serializer: GRPCProtobuf.ProtobufSerializer<Echo_EchoResponse>(),
126-
handler: { request in
127-
try await self.collect(request: request)
144+
handler: { request, context in
145+
try await self.collect(
146+
request: request,
147+
context: context
148+
)
128149
}
129150
)
130151
router.registerHandler(
131152
forMethod: Echo_Echo.Method.Update.descriptor,
132153
deserializer: GRPCProtobuf.ProtobufDeserializer<Echo_EchoRequest>(),
133154
serializer: GRPCProtobuf.ProtobufSerializer<Echo_EchoResponse>(),
134-
handler: { request in
135-
try await self.update(request: request)
155+
handler: { request, context in
156+
try await self.update(
157+
request: request,
158+
context: context
159+
)
136160
}
137161
)
138162
}
@@ -141,33 +165,63 @@ extension Echo_Echo.StreamingServiceProtocol {
141165
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
142166
internal protocol Echo_EchoServiceProtocol: Echo_Echo.StreamingServiceProtocol {
143167
/// Immediately returns an echo of a request.
144-
func get(request: GRPCCore.ServerRequest.Single<Echo_EchoRequest>) async throws -> GRPCCore.ServerResponse.Single<Echo_EchoResponse>
168+
func get(
169+
request: GRPCCore.ServerRequest.Single<Echo_EchoRequest>,
170+
context: GRPCCore.ServerContext
171+
) async throws -> GRPCCore.ServerResponse.Single<Echo_EchoResponse>
145172

146173
/// Splits a request into words and returns each word in a stream of messages.
147-
func expand(request: GRPCCore.ServerRequest.Single<Echo_EchoRequest>) async throws -> GRPCCore.ServerResponse.Stream<Echo_EchoResponse>
174+
func expand(
175+
request: GRPCCore.ServerRequest.Single<Echo_EchoRequest>,
176+
context: GRPCCore.ServerContext
177+
) async throws -> GRPCCore.ServerResponse.Stream<Echo_EchoResponse>
148178

149179
/// Collects a stream of messages and returns them concatenated when the caller closes.
150-
func collect(request: GRPCCore.ServerRequest.Stream<Echo_EchoRequest>) async throws -> GRPCCore.ServerResponse.Single<Echo_EchoResponse>
180+
func collect(
181+
request: GRPCCore.ServerRequest.Stream<Echo_EchoRequest>,
182+
context: GRPCCore.ServerContext
183+
) async throws -> GRPCCore.ServerResponse.Single<Echo_EchoResponse>
151184

152185
/// Streams back messages as they are received in an input stream.
153-
func update(request: GRPCCore.ServerRequest.Stream<Echo_EchoRequest>) async throws -> GRPCCore.ServerResponse.Stream<Echo_EchoResponse>
186+
func update(
187+
request: GRPCCore.ServerRequest.Stream<Echo_EchoRequest>,
188+
context: GRPCCore.ServerContext
189+
) async throws -> GRPCCore.ServerResponse.Stream<Echo_EchoResponse>
154190
}
155191

156192
/// Partial conformance to `Echo_EchoStreamingServiceProtocol`.
157193
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
158194
extension Echo_Echo.ServiceProtocol {
159-
internal func get(request: GRPCCore.ServerRequest.Stream<Echo_EchoRequest>) async throws -> GRPCCore.ServerResponse.Stream<Echo_EchoResponse> {
160-
let response = try await self.get(request: GRPCCore.ServerRequest.Single(stream: request))
195+
internal func get(
196+
request: GRPCCore.ServerRequest.Stream<Echo_EchoRequest>,
197+
context: GRPCCore.ServerContext
198+
) async throws -> GRPCCore.ServerResponse.Stream<Echo_EchoResponse> {
199+
let response = try await self.get(
200+
request: GRPCCore.ServerRequest.Single(stream: request),
201+
context: context
202+
)
161203
return GRPCCore.ServerResponse.Stream(single: response)
162204
}
163205

164-
internal func expand(request: GRPCCore.ServerRequest.Stream<Echo_EchoRequest>) async throws -> GRPCCore.ServerResponse.Stream<Echo_EchoResponse> {
165-
let response = try await self.expand(request: GRPCCore.ServerRequest.Single(stream: request))
206+
internal func expand(
207+
request: GRPCCore.ServerRequest.Stream<Echo_EchoRequest>,
208+
context: GRPCCore.ServerContext
209+
) async throws -> GRPCCore.ServerResponse.Stream<Echo_EchoResponse> {
210+
let response = try await self.expand(
211+
request: GRPCCore.ServerRequest.Single(stream: request),
212+
context: context
213+
)
166214
return response
167215
}
168216

169-
internal func collect(request: GRPCCore.ServerRequest.Stream<Echo_EchoRequest>) async throws -> GRPCCore.ServerResponse.Stream<Echo_EchoResponse> {
170-
let response = try await self.collect(request: request)
217+
internal func collect(
218+
request: GRPCCore.ServerRequest.Stream<Echo_EchoRequest>,
219+
context: GRPCCore.ServerContext
220+
) async throws -> GRPCCore.ServerResponse.Stream<Echo_EchoResponse> {
221+
let response = try await self.collect(
222+
request: request,
223+
context: context
224+
)
171225
return GRPCCore.ServerResponse.Stream(single: response)
172226
}
173227
}

Examples/v2/echo/Subcommands/Serve.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,24 @@ struct Serve: AsyncParsableCommand {
4747
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
4848
struct EchoService: Echo_EchoServiceProtocol {
4949
func get(
50-
request: ServerRequest.Single<Echo_EchoRequest>
50+
request: ServerRequest.Single<Echo_EchoRequest>,
51+
context: ServerContext
5152
) async throws -> ServerResponse.Single<Echo_EchoResponse> {
5253
return ServerResponse.Single(message: .with { $0.text = request.message.text })
5354
}
5455

5556
func collect(
56-
request: ServerRequest.Stream<Echo_EchoRequest>
57+
request: ServerRequest.Stream<Echo_EchoRequest>,
58+
context: ServerContext
5759
) async throws -> ServerResponse.Single<Echo_EchoResponse> {
5860
let messages = try await request.messages.reduce(into: []) { $0.append($1.text) }
5961
let joined = messages.joined(separator: " ")
6062
return ServerResponse.Single(message: .with { $0.text = joined })
6163
}
6264

6365
func expand(
64-
request: ServerRequest.Single<Echo_EchoRequest>
66+
request: ServerRequest.Single<Echo_EchoRequest>,
67+
context: ServerContext
6568
) async throws -> ServerResponse.Stream<Echo_EchoResponse> {
6669
return ServerResponse.Stream { writer in
6770
let parts = request.message.text.split(separator: " ")
@@ -72,7 +75,8 @@ struct EchoService: Echo_EchoServiceProtocol {
7275
}
7376

7477
func update(
75-
request: ServerRequest.Stream<Echo_EchoRequest>
78+
request: ServerRequest.Stream<Echo_EchoRequest>,
79+
context: ServerContext
7680
) async throws -> ServerResponse.Stream<Echo_EchoResponse> {
7781
return ServerResponse.Stream { writer in
7882
for try await message in request.messages {

Examples/v2/hello-world/Generated/helloworld.grpc.swift

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,10 @@ extension GRPCCore.ServiceDescriptor {
6060
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
6161
internal protocol Helloworld_GreeterStreamingServiceProtocol: GRPCCore.RegistrableRPCService {
6262
/// Sends a greeting
63-
func sayHello(request: GRPCCore.ServerRequest.Stream<Helloworld_HelloRequest>) async throws -> GRPCCore.ServerResponse.Stream<Helloworld_HelloReply>
63+
func sayHello(
64+
request: GRPCCore.ServerRequest.Stream<Helloworld_HelloRequest>,
65+
context: GRPCCore.ServerContext
66+
) async throws -> GRPCCore.ServerResponse.Stream<Helloworld_HelloReply>
6467
}
6568

6669
/// Conformance to `GRPCCore.RegistrableRPCService`.
@@ -72,8 +75,11 @@ extension Helloworld_Greeter.StreamingServiceProtocol {
7275
forMethod: Helloworld_Greeter.Method.SayHello.descriptor,
7376
deserializer: GRPCProtobuf.ProtobufDeserializer<Helloworld_HelloRequest>(),
7477
serializer: GRPCProtobuf.ProtobufSerializer<Helloworld_HelloReply>(),
75-
handler: { request in
76-
try await self.sayHello(request: request)
78+
handler: { request, context in
79+
try await self.sayHello(
80+
request: request,
81+
context: context
82+
)
7783
}
7884
)
7985
}
@@ -83,14 +89,23 @@ extension Helloworld_Greeter.StreamingServiceProtocol {
8389
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
8490
internal protocol Helloworld_GreeterServiceProtocol: Helloworld_Greeter.StreamingServiceProtocol {
8591
/// Sends a greeting
86-
func sayHello(request: GRPCCore.ServerRequest.Single<Helloworld_HelloRequest>) async throws -> GRPCCore.ServerResponse.Single<Helloworld_HelloReply>
92+
func sayHello(
93+
request: GRPCCore.ServerRequest.Single<Helloworld_HelloRequest>,
94+
context: GRPCCore.ServerContext
95+
) async throws -> GRPCCore.ServerResponse.Single<Helloworld_HelloReply>
8796
}
8897

8998
/// Partial conformance to `Helloworld_GreeterStreamingServiceProtocol`.
9099
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
91100
extension Helloworld_Greeter.ServiceProtocol {
92-
internal func sayHello(request: GRPCCore.ServerRequest.Stream<Helloworld_HelloRequest>) async throws -> GRPCCore.ServerResponse.Stream<Helloworld_HelloReply> {
93-
let response = try await self.sayHello(request: GRPCCore.ServerRequest.Single(stream: request))
101+
internal func sayHello(
102+
request: GRPCCore.ServerRequest.Stream<Helloworld_HelloRequest>,
103+
context: GRPCCore.ServerContext
104+
) async throws -> GRPCCore.ServerResponse.Stream<Helloworld_HelloReply> {
105+
let response = try await self.sayHello(
106+
request: GRPCCore.ServerRequest.Single(stream: request),
107+
context: context
108+
)
94109
return GRPCCore.ServerResponse.Stream(single: response)
95110
}
96111
}

Examples/v2/hello-world/Subcommands/Serve.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ struct Serve: AsyncParsableCommand {
4646
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
4747
struct Greeter: Helloworld_GreeterServiceProtocol {
4848
func sayHello(
49-
request: ServerRequest.Single<Helloworld_HelloRequest>
49+
request: ServerRequest.Single<Helloworld_HelloRequest>,
50+
context: ServerContext
5051
) async throws -> ServerResponse.Single<Helloworld_HelloReply> {
5152
var reply = Helloworld_HelloReply()
5253
let recipient = request.message.name.isEmpty ? "stranger" : request.message.name

0 commit comments

Comments
 (0)