Ver Fonte

Adopt `ServerContext` changes (#59)

This PR adopts the changes introduced in
https://github.com/grpc/grpc-swift/pull/2161.
Gus Cairo há 10 meses atrás
pai
commit
64874f2d59

+ 25 - 45
Sources/GRPCNIOTransportCore/Internal/Channel+AddressInfo.swift

@@ -18,67 +18,47 @@ 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.
@@ -86,7 +66,7 @@ extension NIOAsyncChannel {
         }
       } else {
         // '!' is safe, UDS always has a path.
-        return "unix:\(local.pathname!)"
+        return "unix:\(address.pathname!)"
       }
     }
   }

+ 12 - 4
Sources/GRPCNIOTransportCore/Server/CommonHTTP2ServerTransport.swift

@@ -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 {
@@ -218,7 +219,8 @@ package final class CommonHTTP2ServerTransport<
                 stream,
                 handler: streamHandler,
                 descriptor: descriptor,
-                peer: peer
+                remotePeer: remotePeer,
+                localPeer: localPeer
               )
             }
           }
@@ -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
@@ -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)
       }
     }

+ 2 - 2
Tests/GRPCNIOTransportHTTP2Tests/ControlService.swift

@@ -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 {

+ 9 - 13
Tests/GRPCNIOTransportHTTP2Tests/HTTP2TransportTests.swift

@@ -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)
@@ -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)
@@ -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/))