Forráskód Böngészése

Replace old API usage (#479)

George Barnett 6 éve
szülő
commit
7bbdffc188

+ 9 - 8
Examples/Google/NaturalLanguage/Sources/main.swift

@@ -21,11 +21,10 @@ import NIO
 import NIOHTTP1
 import NIOSSL
 
-/// Prepare a TLSMode for a general SSL client that supports HTTP/2.
-func makeClientTLS() throws -> GRPCClientConnection.TLSMode {
+/// Prepare an SSL context for a general SSL client that supports HTTP/2.
+func makeClientTLS() throws -> NIOSSLContext {
   let configuration = TLSConfiguration.forClient(applicationProtocols: ["h2"])
-  let context = try NIOSSLContext(configuration: configuration)
-  return .custom(context)
+  return try NIOSSLContext(configuration: configuration)
 }
 
 /// Create a client and return a future to provide its value.
@@ -35,10 +34,12 @@ func makeServiceClient(host: String,
   -> EventLoopFuture<Google_Cloud_Language_V1_LanguageServiceServiceClient> {
     let promise = eventLoopGroup.next().makePromise(of: Google_Cloud_Language_V1_LanguageServiceServiceClient.self)
     do {
-      try GRPCClientConnection.start(host: host,
-                                     port: port,
-                                     eventLoopGroup: eventLoopGroup,
-                                     tls: makeClientTLS())
+      let configuration = GRPCClientConnection.Configuration(
+        target: .hostAndPort(host, port),
+        eventLoopGroup: eventLoopGroup,
+        tlsConfiguration: .init(sslContext: try makeClientTLS())
+
+      try GRPCClientConnection.start(configuration)
         .map { client in
           Google_Cloud_Language_V1_LanguageServiceServiceClient(connection: client)
         }.cascade(to: promise)

+ 2 - 73
Sources/GRPC/GRPCClientConnection.swift

@@ -203,7 +203,7 @@ extension GRPCClientConnection {
       eventLoopGroup: EventLoopGroup,
       errorDelegate: ClientErrorDelegate? = DebugOnlyLoggingClientErrorDelegate.shared,
       tlsConfiguration: TLSConfiguration? = nil
-      ) {
+    ) {
       self.target = target
       self.eventLoopGroup = eventLoopGroup
       self.errorDelegate = errorDelegate
@@ -256,7 +256,7 @@ fileprivate extension Channel {
   func configureTLS(
     _ configuration: GRPCClientConnection.TLSConfiguration,
     errorDelegate: ClientErrorDelegate?
-    ) -> EventLoopFuture<Void> {
+  ) -> EventLoopFuture<Void> {
     do {
       let sslClientHandler = try NIOSSLClientHandler(
         context: configuration.sslContext,
@@ -269,74 +269,3 @@ fileprivate extension Channel {
     }
   }
 }
-
-// MARK: - Legacy APIs
-
-extension GRPCClientConnection {
-  /// Starts a connection to the given host and port.
-  ///
-  /// - Parameters:
-  ///   - host: Host to connect to.
-  ///   - port: Port on the host to connect to.
-  ///   - eventLoopGroup: Event loop group to run the connection on.
-  ///   - errorDelegate: An error delegate which is called when errors are caught. Provided
-  ///       delegates **must not maintain a strong reference to this `GRPCClientConnection`**. Doing
-  ///       so will cause a retain cycle. Defaults to a delegate which logs errors in debug builds
-  ///       only.
-  ///   - tlsMode: How TLS should be configured for this connection.
-  ///   - hostOverride: Value to use for TLS SNI extension; this must not be an IP address. Ignored
-  ///       if `tlsMode` is `.none`.
-  /// - Returns: A future which will be fulfilled with a connection to the remote peer.
-  public static func start(
-    host: String,
-    port: Int,
-    eventLoopGroup: EventLoopGroup,
-    errorDelegate: ClientErrorDelegate? = DebugOnlyLoggingClientErrorDelegate.shared,
-    tls tlsMode: TLSMode = .none,
-    hostOverride: String? = nil
-  ) throws -> EventLoopFuture<GRPCClientConnection> {
-    var configuration = Configuration(
-      target: .hostAndPort(host, port),
-      eventLoopGroup: eventLoopGroup,
-      errorDelegate: errorDelegate)
-
-    if let sslContext = try tlsMode.makeSSLContext() {
-      configuration.tlsConfiguration = .init(sslContext: sslContext, hostnameOverride: hostOverride)
-    }
-
-    return GRPCClientConnection.start(configuration)
-  }
-
-  public enum TLSMode {
-    case none
-    case anonymous
-    case custom(NIOSSLContext)
-
-    /// Returns an SSL context for the TLS mode.
-    ///
-    /// - Returns: An SSL context for the TLS mode, or `nil` if TLS is not being used.
-    public func makeSSLContext() throws -> NIOSSLContext? {
-      switch self {
-      case .none:
-        return nil
-
-      case .anonymous:
-        return try NIOSSLContext(configuration: .forClient())
-
-      case .custom(let context):
-        return context
-      }
-    }
-
-    /// Rethrns the HTTP protocol for the TLS mode.
-    public var httpProtocol: HTTP2ToHTTP1ClientCodec.HTTPProtocol {
-      switch self {
-      case .none:
-        return .http
-
-      case .anonymous, .custom:
-        return .https
-      }
-    }
-  }
-}

+ 7 - 14
Sources/GRPCInteroperabilityTests/InteroperabilityTestClientConnection.swift

@@ -32,28 +32,21 @@ public func makeInteroperabilityTestClientConnection(
   eventLoopGroup: EventLoopGroup,
   useTLS: Bool
 ) throws -> EventLoopFuture<GRPCClientConnection> {
-  let tlsMode: GRPCClientConnection.TLSMode
-  let hostOverride: String?
+  var configuration = GRPCClientConnection.Configuration(
+    target: .hostAndPort(host, port),
+    eventLoopGroup: eventLoopGroup)
 
   if useTLS {
     // The CA certificate has a common name of "*.test.google.fr", use the following host override
     // so we can do full certificate verification.
-    hostOverride = "foo.test.google.fr"
+    let hostOverride = "foo.test.google.fr"
     let tlsConfiguration = TLSConfiguration.forClient(
       trustRoots: .certificates([InteroperabilityTestCredentials.caCertificate]),
       applicationProtocols: ["h2"])
 
-    tlsMode = .custom(try NIOSSLContext(configuration: tlsConfiguration))
-  } else {
-    hostOverride = nil
-    tlsMode = .none
+    let context = try NIOSSLContext(configuration: tlsConfiguration)
+    configuration.tlsConfiguration = .init(sslContext: context, hostnameOverride: hostOverride)
   }
 
-  return try GRPCClientConnection.start(
-    host: host,
-    port: port,
-    eventLoopGroup: eventLoopGroup,
-    tls: tlsMode,
-    hostOverride: hostOverride
-  )
+  return GRPCClientConnection.start(configuration)
 }

+ 17 - 19
Sources/GRPCPerformanceTests/main.swift

@@ -5,19 +5,10 @@ import NIOSSL
 import Commander
 
 struct ConnectionFactory {
-  var host: String
-  var port: Int
-  var group: EventLoopGroup
-  var context: NIOSSLContext?
-  var serverHostOverride: String?
+  var configuration: GRPCClientConnection.Configuration
 
   func makeConnection() throws -> EventLoopFuture<GRPCClientConnection> {
-    return try GRPCClientConnection.start(
-      host: self.host,
-      port: self.port,
-      eventLoopGroup: self.group,
-      tls: self.context.map { .custom($0) } ?? .none,
-      hostOverride: self.serverHostOverride)
+    return GRPCClientConnection.start(configuration)
   }
 
   func makeEchoClient() throws -> EventLoopFuture<Echo_EchoServiceClient> {
@@ -110,7 +101,9 @@ final class ConnectionCreationThroughput: Benchmark {
       try self.factory.makeConnection()
     }
 
-    try EventLoopFuture.andAllSucceed(self.createdConnections, on: self.factory.group.next()).wait()
+    try EventLoopFuture.andAllSucceed(
+      self.createdConnections,
+      on: self.factory.configuration.eventLoopGroup.next()).wait()
   }
 
   func tearDown() throws {
@@ -120,7 +113,9 @@ final class ConnectionCreationThroughput: Benchmark {
       }
     }
 
-    try EventLoopFuture.andAllSucceed(connectionClosures, on: self.factory.group.next()).wait()
+    try EventLoopFuture.andAllSucceed(
+      connectionClosures,
+      on: self.factory.configuration.eventLoopGroup.next()).wait()
   }
 }
 
@@ -320,18 +315,21 @@ Group { group in
     privateKeyOption,
     hostOverrideOption
   ) { benchmarkNames, host, port, caCertificatePath, certificatePath, privateKeyPath, hostOverride in
+    var configuration = GRPCClientConnection.Configuration(
+      target: .hostAndPort(host, port),
+      eventLoopGroup: MultiThreadedEventLoopGroup(numberOfThreads: 1))
+
     let sslContext = makeSSLContext(
       caCertificatePath: caCertificatePath,
       certificatePath: certificatePath,
       privateKeyPath: privateKeyPath,
       server: false)
 
-    let factory = ConnectionFactory(
-      host: host,
-      port: port,
-      group: MultiThreadedEventLoopGroup(numberOfThreads: 1),
-      context: sslContext,
-      serverHostOverride: hostOverride.isEmpty ? nil : hostOverride)
+    if let sslContext = sslContext {
+      configuration.tlsConfiguration = .init(sslContext: sslContext, hostnameOverride: hostOverride)
+    }
+
+    let factory = ConnectionFactory(configuration: configuration)
 
     let names = benchmarkNames.components(separatedBy: ",")
 

+ 15 - 8
Tests/GRPCTests/BasicEchoTestCase.swift

@@ -77,8 +77,13 @@ extension TransportSecurity {
     }
   }
 
-  func makeClientTLS() throws -> GRPCClientConnection.TLSMode {
-    return try makeClientTLSConfiguration().map { .custom(try NIOSSLContext(configuration: $0)) } ?? .none
+  func makeConfiguration() throws -> GRPCClientConnection.TLSConfiguration? {
+    guard let config = try self.makeClientTLSConfiguration() else {
+      return nil
+    }
+
+    let context = try NIOSSLContext(configuration: config)
+    return GRPCClientConnection.TLSConfiguration(sslContext: context)
   }
 
   func makeClientTLSConfiguration() throws -> TLSConfiguration? {
@@ -112,6 +117,13 @@ class EchoTestCaseBase: XCTestCase {
   var server: GRPCServer!
   var client: Echo_EchoServiceClient!
 
+  func makeClientConfiguration() throws -> GRPCClientConnection.Configuration {
+    return .init(
+      target: .hostAndPort("localhost", 5050),
+      eventLoopGroup: self.clientEventLoopGroup,
+      tlsConfiguration: try self.transportSecurity.makeConfiguration())
+  }
+
   func makeServer() throws -> GRPCServer {
     return try GRPCServer.start(
       hostname: "localhost",
@@ -124,12 +136,7 @@ class EchoTestCaseBase: XCTestCase {
   }
 
   func makeClientConnection() throws -> GRPCClientConnection {
-    return try GRPCClientConnection.start(
-      host: "localhost",
-      port: 5050,
-      eventLoopGroup: self.clientEventLoopGroup,
-      tls: try self.transportSecurity.makeClientTLS()
-    ).wait()
+    return try GRPCClientConnection.start(self.makeClientConfiguration()).wait()
   }
 
   func makeEchoProvider() -> Echo_EchoProvider { return EchoProvider() }

+ 8 - 5
Tests/GRPCTests/ClientTLSFailureTests.swift

@@ -43,12 +43,15 @@ class ClientTLSFailureTests: XCTestCase {
     configuration: TLSConfiguration,
     hostOverride: String? = SampleCertificate.server.commonName
   ) throws -> EventLoopFuture<GRPCClientConnection> {
-    return try GRPCClientConnection.start(
-      host: "localhost",
-      port: self.port,
+    let context = try NIOSSLContext(configuration: configuration)
+    let clientConfiguration = GRPCClientConnection.Configuration(
+      target: .hostAndPort("localhost", self.port),
       eventLoopGroup: self.clientEventLoopGroup,
-      tls: .custom(try NIOSSLContext(configuration: configuration)),
-      hostOverride: hostOverride)
+      tlsConfiguration: GRPCClientConnection.TLSConfiguration(
+        sslContext: context,
+        hostnameOverride: hostOverride))
+
+    return GRPCClientConnection.start(clientConfiguration)
   }
 
   func makeClientConnectionExpectation() -> XCTestExpectation {