Skip to content

Commit 64874f2

Browse files
authored
Adopt ServerContext changes (#59)
This PR adopts the changes introduced in grpc/grpc-swift#2161.
1 parent b8a3c3b commit 64874f2

File tree

4 files changed

+48
-64
lines changed

4 files changed

+48
-64
lines changed

Sources/GRPCNIOTransportCore/Internal/Channel+AddressInfo.swift

Lines changed: 25 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -18,75 +18,55 @@ internal import NIOCore
1818

1919
extension NIOAsyncChannel {
2020
var remoteAddressInfo: String {
21-
guard let remote = self.channel.remoteAddress else {
22-
return "<unknown>"
23-
}
24-
25-
switch remote {
26-
case .v4(let address):
27-
// '!' is safe, v4 always has a port.
28-
return "ipv4:\(address.host):\(remote.port!)"
29-
30-
case .v6(let address):
31-
// '!' is safe, v6 always has a port.
32-
return "ipv6:[\(address.host)]:\(remote.port!)"
33-
34-
case .unixDomainSocket:
35-
// '!' is safe, UDS always has a path.
36-
if remote.pathname!.isEmpty {
37-
guard let local = self.channel.localAddress else {
38-
return "unix:<unknown>"
39-
}
40-
41-
switch local {
42-
case .unixDomainSocket:
43-
// '!' is safe, UDS always has a path.
44-
return "unix:\(local.pathname!)"
45-
46-
case .v4, .v6:
47-
// Remote address is UDS but local isn't. This shouldn't ever happen.
48-
return "unix:<unknown>"
49-
}
50-
} else {
51-
// '!' is safe, UDS always has a path.
52-
return "unix:\(remote.pathname!)"
53-
}
54-
}
21+
self.getAddressInfoWithFallbackIfUDS(
22+
address: self.channel.remoteAddress,
23+
udsFallback: self.channel.localAddress
24+
)
5525
}
5626

5727
var localAddressInfo: String {
58-
guard let local = self.channel.localAddress else {
28+
self.getAddressInfoWithFallbackIfUDS(
29+
address: self.channel.localAddress,
30+
udsFallback: self.channel.remoteAddress
31+
)
32+
}
33+
34+
private func getAddressInfoWithFallbackIfUDS(
35+
address: NIOCore.SocketAddress?,
36+
udsFallback: NIOCore.SocketAddress?
37+
) -> String {
38+
guard let address else {
5939
return "<unknown>"
6040
}
6141

62-
switch local {
63-
case .v4(let address):
42+
switch address {
43+
case .v4(let ipv4Address):
6444
// '!' is safe, v4 always has a port.
65-
return "ipv4:\(address.host):\(local.port!)"
45+
return "ipv4:\(ipv4Address.host):\(address.port!)"
6646

67-
case .v6(let address):
47+
case .v6(let ipv6Address):
6848
// '!' is safe, v6 always has a port.
69-
return "ipv6:[\(address.host)]:\(local.port!)"
49+
return "ipv6:[\(ipv6Address.host)]:\(address.port!)"
7050

7151
case .unixDomainSocket:
7252
// '!' is safe, UDS always has a path.
73-
if local.pathname!.isEmpty {
74-
guard let remote = self.channel.remoteAddress else {
53+
if address.pathname!.isEmpty {
54+
guard let udsFallback else {
7555
return "unix:<unknown>"
7656
}
7757

78-
switch remote {
58+
switch udsFallback {
7959
case .unixDomainSocket:
8060
// '!' is safe, UDS always has a path.
81-
return "unix:\(remote.pathname!)"
61+
return "unix:\(udsFallback.pathname!)"
8262

8363
case .v4, .v6:
8464
// Remote address is UDS but local isn't. This shouldn't ever happen.
8565
return "unix:<unknown>"
8666
}
8767
} else {
8868
// '!' is safe, UDS always has a path.
89-
return "unix:\(local.pathname!)"
69+
return "unix:\(address.pathname!)"
9070
}
9171
}
9272
}

Sources/GRPCNIOTransportCore/Server/CommonHTTP2ServerTransport.swift

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,8 @@ package final class CommonHTTP2ServerTransport<
199199
_ context: ServerContext
200200
) async -> Void
201201
) async throws {
202-
let peer = connection.remoteAddressInfo
202+
let remotePeer = connection.remoteAddressInfo
203+
let localPeer = connection.localAddressInfo
203204
try await connection.executeThenClose { inbound, _ in
204205
await withDiscardingTaskGroup { group in
205206
group.addTask {
@@ -218,7 +219,8 @@ package final class CommonHTTP2ServerTransport<
218219
stream,
219220
handler: streamHandler,
220221
descriptor: descriptor,
221-
peer: peer
222+
remotePeer: remotePeer,
223+
localPeer: localPeer
222224
)
223225
}
224226
}
@@ -236,7 +238,8 @@ package final class CommonHTTP2ServerTransport<
236238
_ context: ServerContext
237239
) async -> Void,
238240
descriptor: EventLoopFuture<MethodDescriptor>,
239-
peer: String
241+
remotePeer: String,
242+
localPeer: String
240243
) async {
241244
// It's okay to ignore these errors:
242245
// - If we get an error because the http2Stream failed to close, then there's nothing we can do
@@ -274,7 +277,12 @@ package final class CommonHTTP2ServerTransport<
274277
)
275278
)
276279

277-
let context = ServerContext(descriptor: descriptor, peer: peer, cancellation: handle)
280+
let context = ServerContext(
281+
descriptor: descriptor,
282+
remotePeer: remotePeer,
283+
localPeer: localPeer,
284+
cancellation: handle
285+
)
278286
await streamHandler(rpcStream, context)
279287
}
280288
}

Tests/GRPCNIOTransportHTTP2Tests/ControlService.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,11 @@ extension ControlService {
113113
}
114114

115115
private func serverRemotePeerInfo(context: ServerContext) -> String {
116-
context.peer
116+
context.remotePeer
117117
}
118118

119119
private func serverLocalPeerInfo(context: ServerContext) -> String {
120-
"<not yet implemented>"
120+
context.localPeer
121121
}
122122

123123
private func clientRemotePeerInfo<T>(request: StreamingServerRequest<T>) -> String {

Tests/GRPCNIOTransportHTTP2Tests/HTTP2TransportTests.swift

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1636,13 +1636,11 @@ final class HTTP2TransportTests: XCTestCase {
16361636
let serverRemotePeerMatches = peerInfo.server.remote.wholeMatch(of: /ipv4:127\.0\.0\.1:(\d+)/)
16371637
let clientPort = try XCTUnwrap(serverRemotePeerMatches).1
16381638

1639-
// TODO: Uncomment when server local peer info is implemented
1639+
let serverLocalPeerMatches = peerInfo.server.local.wholeMatch(of: /ipv4:127.0.0.1:(\d+)/)
1640+
let serverPort = try XCTUnwrap(serverLocalPeerMatches).1
16401641

1641-
// let serverLocalPeerMatches = peerInfo.server.local.wholeMatch(of: /<not yet implemented>/)
1642-
// let serverPort = XCTUnwrap(serverLocalPeerMatches).1
1643-
1644-
// let clientRemotePeerMatches = peerInfo.client.remote.wholeMatch(of: /ipv4:127.0.0.1:(\d+)/)
1645-
// XCTAssertEqual(try XCTUnwrap(clientRemotePeerMatches).1, serverPort)
1642+
let clientRemotePeerMatches = peerInfo.client.remote.wholeMatch(of: /ipv4:127.0.0.1:(\d+)/)
1643+
XCTAssertEqual(try XCTUnwrap(clientRemotePeerMatches).1, serverPort)
16461644

16471645
let clientLocalPeerMatches = peerInfo.client.local.wholeMatch(of: /ipv4:127\.0\.0\.1:(\d+)/)
16481646
XCTAssertEqual(try XCTUnwrap(clientLocalPeerMatches).1, clientPort)
@@ -1658,13 +1656,11 @@ final class HTTP2TransportTests: XCTestCase {
16581656
let serverRemotePeerMatches = peerInfo.server.remote.wholeMatch(of: /ipv6:\[::1\]:(\d+)/)
16591657
let clientPort = try XCTUnwrap(serverRemotePeerMatches).1
16601658

1661-
// TODO: Uncomment when server local peer info is implemented
1662-
1663-
// let serverLocalPeerMatches = peerInfo.server.local.wholeMatch(of: /<not yet implemented>/)
1664-
// let serverPort = XCTUnwrap(serverLocalPeerMatches).1
1659+
let serverLocalPeerMatches = peerInfo.server.local.wholeMatch(of: /ipv6:\[::1\]:(\d+)/)
1660+
let serverPort = try XCTUnwrap(serverLocalPeerMatches).1
16651661

1666-
// let clientRemotePeerMatches = peerInfo.client.remote.wholeMatch(of: /ipv6:\[::1\]:(\d+)/)
1667-
// XCTAssertEqual(try XCTUnwrap(clientRemotePeerMatches).1, serverPort)
1662+
let clientRemotePeerMatches = peerInfo.client.remote.wholeMatch(of: /ipv6:\[::1\]:(\d+)/)
1663+
XCTAssertEqual(try XCTUnwrap(clientRemotePeerMatches).1, serverPort)
16681664

16691665
let clientLocalPeerMatches = peerInfo.client.local.wholeMatch(of: /ipv6:\[::1\]:(\d+)/)
16701666
XCTAssertEqual(try XCTUnwrap(clientLocalPeerMatches).1, clientPort)
@@ -1679,7 +1675,7 @@ final class HTTP2TransportTests: XCTestCase {
16791675
let peerInfo = try await control.peerInfo()
16801676

16811677
XCTAssertNotNil(peerInfo.server.remote.wholeMatch(of: /unix:peer-info-uds/))
1682-
XCTAssertNotNil(peerInfo.server.local.wholeMatch(of: /<not yet implemented>/))
1678+
XCTAssertNotNil(peerInfo.server.local.wholeMatch(of: /unix:peer-info-uds/))
16831679

16841680
XCTAssertNotNil(peerInfo.client.remote.wholeMatch(of: /unix:peer-info-uds/))
16851681
XCTAssertNotNil(peerInfo.client.local.wholeMatch(of: /unix:peer-info-uds/))

0 commit comments

Comments
 (0)