Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 25 additions & 45 deletions Sources/GRPCNIOTransportCore/Internal/Channel+AddressInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,75 +18,55 @@ internal import NIOCore

extension NIOAsyncChannel {
var remoteAddressInfo: String {
guard let remote = self.channel.remoteAddress else {
return "<unknown>"
}

switch remote {
case .v4(let address):
// '!' is safe, v4 always has a port.
return "ipv4:\(address.host):\(remote.port!)"

case .v6(let address):
// '!' is safe, v6 always has a port.
return "ipv6:[\(address.host)]:\(remote.port!)"

case .unixDomainSocket:
// '!' is safe, UDS always has a path.
if remote.pathname!.isEmpty {
guard let local = self.channel.localAddress else {
return "unix:<unknown>"
}

switch local {
case .unixDomainSocket:
// '!' is safe, UDS always has a path.
return "unix:\(local.pathname!)"

case .v4, .v6:
// Remote address is UDS but local isn't. This shouldn't ever happen.
return "unix:<unknown>"
}
} else {
// '!' is safe, UDS always has a path.
return "unix:\(remote.pathname!)"
}
}
self.getAddressInfoWithFallbackIfUDS(
address: self.channel.remoteAddress,
udsFallback: self.channel.localAddress
)
}

var localAddressInfo: String {
guard let local = self.channel.localAddress else {
self.getAddressInfoWithFallbackIfUDS(
address: self.channel.localAddress,
udsFallback: self.channel.remoteAddress
)
}

private func getAddressInfoWithFallbackIfUDS(
address: NIOCore.SocketAddress?,
udsFallback: NIOCore.SocketAddress?
) -> String {
guard let address else {
return "<unknown>"
}

switch local {
case .v4(let address):
switch address {
case .v4(let ipv4Address):
// '!' is safe, v4 always has a port.
return "ipv4:\(address.host):\(local.port!)"
return "ipv4:\(ipv4Address.host):\(address.port!)"

case .v6(let address):
case .v6(let ipv6Address):
// '!' is safe, v6 always has a port.
return "ipv6:[\(address.host)]:\(local.port!)"
return "ipv6:[\(ipv6Address.host)]:\(address.port!)"

case .unixDomainSocket:
// '!' is safe, UDS always has a path.
if local.pathname!.isEmpty {
guard let remote = self.channel.remoteAddress else {
if address.pathname!.isEmpty {
guard let udsFallback else {
return "unix:<unknown>"
}

switch remote {
switch udsFallback {
case .unixDomainSocket:
// '!' is safe, UDS always has a path.
return "unix:\(remote.pathname!)"
return "unix:\(udsFallback.pathname!)"

case .v4, .v6:
// Remote address is UDS but local isn't. This shouldn't ever happen.
return "unix:<unknown>"
}
} else {
// '!' is safe, UDS always has a path.
return "unix:\(local.pathname!)"
return "unix:\(address.pathname!)"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,8 @@ package final class CommonHTTP2ServerTransport<
_ context: ServerContext
) async -> Void
) async throws {
let peer = connection.remoteAddressInfo
let remotePeer = connection.remoteAddressInfo
let localPeer = connection.localAddressInfo
try await connection.executeThenClose { inbound, _ in
await withDiscardingTaskGroup { group in
group.addTask {
Expand All @@ -218,7 +219,8 @@ package final class CommonHTTP2ServerTransport<
stream,
handler: streamHandler,
descriptor: descriptor,
peer: peer
remotePeer: remotePeer,
localPeer: localPeer
)
}
}
Expand All @@ -236,7 +238,8 @@ package final class CommonHTTP2ServerTransport<
_ context: ServerContext
) async -> Void,
descriptor: EventLoopFuture<MethodDescriptor>,
peer: String
remotePeer: String,
localPeer: String
) async {
// It's okay to ignore these errors:
// - If we get an error because the http2Stream failed to close, then there's nothing we can do
Expand Down Expand Up @@ -274,7 +277,12 @@ package final class CommonHTTP2ServerTransport<
)
)

let context = ServerContext(descriptor: descriptor, peer: peer, cancellation: handle)
let context = ServerContext(
descriptor: descriptor,
remotePeer: remotePeer,
localPeer: localPeer,
cancellation: handle
)
await streamHandler(rpcStream, context)
}
}
Expand Down
4 changes: 2 additions & 2 deletions Tests/GRPCNIOTransportHTTP2Tests/ControlService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,11 @@ extension ControlService {
}

private func serverRemotePeerInfo(context: ServerContext) -> String {
context.peer
context.remotePeer
}

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

private func clientRemotePeerInfo<T>(request: StreamingServerRequest<T>) -> String {
Expand Down
22 changes: 9 additions & 13 deletions Tests/GRPCNIOTransportHTTP2Tests/HTTP2TransportTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1636,13 +1636,11 @@ final class HTTP2TransportTests: XCTestCase {
let serverRemotePeerMatches = peerInfo.server.remote.wholeMatch(of: /ipv4:127\.0\.0\.1:(\d+)/)
let clientPort = try XCTUnwrap(serverRemotePeerMatches).1

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

// let serverLocalPeerMatches = peerInfo.server.local.wholeMatch(of: /<not yet implemented>/)
// let serverPort = XCTUnwrap(serverLocalPeerMatches).1

// let clientRemotePeerMatches = peerInfo.client.remote.wholeMatch(of: /ipv4:127.0.0.1:(\d+)/)
// XCTAssertEqual(try XCTUnwrap(clientRemotePeerMatches).1, serverPort)
let clientRemotePeerMatches = peerInfo.client.remote.wholeMatch(of: /ipv4:127.0.0.1:(\d+)/)
XCTAssertEqual(try XCTUnwrap(clientRemotePeerMatches).1, serverPort)

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

// TODO: Uncomment when server local peer info is implemented

// let serverLocalPeerMatches = peerInfo.server.local.wholeMatch(of: /<not yet implemented>/)
// let serverPort = XCTUnwrap(serverLocalPeerMatches).1
let serverLocalPeerMatches = peerInfo.server.local.wholeMatch(of: /ipv6:\[::1\]:(\d+)/)
let serverPort = try XCTUnwrap(serverLocalPeerMatches).1

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

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

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

XCTAssertNotNil(peerInfo.client.remote.wholeMatch(of: /unix:peer-info-uds/))
XCTAssertNotNil(peerInfo.client.local.wholeMatch(of: /unix:peer-info-uds/))
Expand Down
Loading