Skip to content

Commit bca5dd9

Browse files
committed
Add remote and local peers to ServerContext
1 parent dcea5fb commit bca5dd9

File tree

4 files changed

+41
-7
lines changed

4 files changed

+41
-7
lines changed

Sources/GRPCCore/Call/Server/ServerContext.swift

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,36 @@ public struct ServerContext: Sendable {
3030
/// - "ipv4:127.0.0.1:31415",
3131
/// - "ipv6:[::1]:443",
3232
/// - "in-process:27182".
33-
public var peer: String
33+
@available(*, deprecated, renamed: "remotePeer")
34+
public var peer: String {
35+
remotePeer
36+
}
37+
38+
/// A description of the remote peer.
39+
///
40+
/// The format of the description should follow the pattern "<transport>:<address>" where
41+
/// "<transport>" indicates the underlying network transport (such as "ipv4", "unix", or
42+
/// "in-process"). This is a guideline for how descriptions should be formatted; different
43+
/// implementations may not follow this format so you shouldn't make assumptions based on it.
44+
///
45+
/// Some examples include:
46+
/// - "ipv4:127.0.0.1:31415",
47+
/// - "ipv6:[::1]:443",
48+
/// - "in-process:27182".
49+
public var remotePeer: String
50+
51+
/// A description of the local peer.
52+
///
53+
/// The format of the description should follow the pattern "<transport>:<address>" where
54+
/// "<transport>" indicates the underlying network transport (such as "ipv4", "unix", or
55+
/// "in-process"). This is a guideline for how descriptions should be formatted; different
56+
/// implementations may not follow this format so you shouldn't make assumptions based on it.
57+
///
58+
/// Some examples include:
59+
/// - "ipv4:127.0.0.1:31415",
60+
/// - "ipv6:[::1]:443",
61+
/// - "in-process:27182".
62+
public var localPeer: String
3463

3564
/// A handle for checking the cancellation status of an RPC.
3665
public var cancellation: RPCCancellationHandle
@@ -39,16 +68,19 @@ public struct ServerContext: Sendable {
3968
///
4069
/// - Parameters:
4170
/// - descriptor: A description of the method being called.
42-
/// - peer: A description of the remote peer.
71+
/// - remotePeer: A description of the remote peer.
72+
/// - localPeer: A description of the local peer.
4373
/// - cancellation: A cancellation handle. You can create a cancellation handle
4474
/// using ``withServerContextRPCCancellationHandle(_:)``.
4575
public init(
4676
descriptor: MethodDescriptor,
47-
peer: String,
77+
remotePeer: String,
78+
localPeer: String,
4879
cancellation: RPCCancellationHandle
4980
) {
5081
self.descriptor = descriptor
51-
self.peer = peer
82+
self.remotePeer = remotePeer
83+
self.localPeer = localPeer
5284
self.cancellation = cancellation
5385
}
5486
}

Sources/GRPCInProcessTransport/InProcessTransport+Server.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ extension InProcessTransport {
122122

123123
let context = ServerContext(
124124
descriptor: stream.descriptor,
125-
peer: self.peer,
125+
remotePeer: self.peer,
126+
localPeer: self.peer,
126127
cancellation: handle
127128
)
128129
await streamHandler(stream, context)

Tests/GRPCCoreTests/Call/Server/Internal/ServerRPCExecutorTestSupport/ServerRPCExecutorTestHarness.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ struct ServerRPCExecutorTestHarness {
102102
await withServerContextRPCCancellationHandle { cancellation in
103103
let context = ServerContext(
104104
descriptor: MethodDescriptor(fullyQualifiedService: "foo", method: "bar"),
105-
peer: "tests",
105+
remotePeer: "remote",
106+
localPeer: "local",
106107
cancellation: cancellation
107108
)
108109

Tests/GRPCInProcessTransportTests/InProcessTransportTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ private struct TestService: RegistrableRPCService {
123123
request: ServerRequest<Void>,
124124
context: ServerContext
125125
) async throws -> ServerResponse<String> {
126-
return ServerResponse(message: context.peer)
126+
return ServerResponse(message: context.remotePeer)
127127
}
128128

129129
func registerMethods(with router: inout RPCRouter) {

0 commit comments

Comments
 (0)