Skip to content

Commit

Permalink
Adopt ServerContext changes
Browse files Browse the repository at this point in the history
  • Loading branch information
gjcairo committed Jan 17, 2025
1 parent b8a3c3b commit 7a6d8e9
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 51 deletions.
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!)"
}
}
getAddressInfoWithFallbackIfUDS(
address: self.channel.remoteAddress,
udsFallback: self.channel.localAddress
)
}

var localAddressInfo: String {
guard let local = self.channel.localAddress else {
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

0 comments on commit 7a6d8e9

Please sign in to comment.