Przeglądaj źródła

Update examples to use channel pool (#1293)

Motivation:

Examples are still using `ClientConnection`. They should use
`GRPCChannelPool` instead.

Modifications:

- Update echo, helloworld, and routeguide
- Update tutorials

Result:

Examples and tutorials are more up-to-date
George Barnett 4 lat temu
rodzic
commit
03fe0fa866

+ 17 - 11
Sources/Examples/Echo/Runtime/main.swift

@@ -91,7 +91,7 @@ struct Echo: ParsableCommand {
         try! group.syncShutdownGracefully()
         try! group.syncShutdownGracefully()
       }
       }
 
 
-      let client = makeClient(
+      let client = try makeClient(
         group: group,
         group: group,
         port: self.port,
         port: self.port,
         useTLS: self.tls,
         useTLS: self.tls,
@@ -150,8 +150,8 @@ func makeClient(
   port: Int,
   port: Int,
   useTLS: Bool,
   useTLS: Bool,
   useInterceptor: Bool
   useInterceptor: Bool
-) -> Echo_EchoClient {
-  let builder: ClientConnection.Builder
+) throws -> Echo_EchoClient {
+  let security: GRPCChannelPool.Configuration.TransportSecurity
 
 
   if useTLS {
   if useTLS {
     // We're using some self-signed certs here: check they aren't expired.
     // We're using some self-signed certs here: check they aren't expired.
@@ -162,19 +162,25 @@ func makeClient(
       "SSL certificates are expired. Please submit an issue at https://github.com/grpc/grpc-swift."
       "SSL certificates are expired. Please submit an issue at https://github.com/grpc/grpc-swift."
     )
     )
 
 
-    builder = ClientConnection.usingTLSBackedByNIOSSL(on: group)
-      .withTLS(certificateChain: [clientCert.certificate])
-      .withTLS(privateKey: SamplePrivateKey.client)
-      .withTLS(trustRoots: .certificates([caCert.certificate]))
+    let tlsConfiguration = GRPCTLSConfiguration.makeServerConfigurationBackedByNIOSSL(
+      certificateChain: [.certificate(clientCert.certificate)],
+      privateKey: .privateKey(SamplePrivateKey.client),
+      trustRoots: .certificates([caCert.certificate])
+    )
+
+    security = .tls(tlsConfiguration)
   } else {
   } else {
-    builder = ClientConnection.insecure(group: group)
+    security = .plaintext
   }
   }
 
 
-  // Start the connection and create the client:
-  let connection = builder.connect(host: "localhost", port: port)
+  let channel = try GRPCChannelPool.with(
+    target: .host("localhost", port: port),
+    transportSecurity: security,
+    eventLoopGroup: group
+  )
 
 
   return Echo_EchoClient(
   return Echo_EchoClient(
-    channel: connection,
+    channel: channel,
     interceptors: useInterceptor ? ExampleClientInterceptorFactory() : nil
     interceptors: useInterceptor ? ExampleClientInterceptorFactory() : nil
   )
   )
 }
 }

+ 5 - 2
Sources/Examples/HelloWorld/Client/main.swift

@@ -56,8 +56,11 @@ struct HelloWorld: ParsableCommand {
     }
     }
 
 
     // Configure the channel, we're not using TLS so the connection is `insecure`.
     // Configure the channel, we're not using TLS so the connection is `insecure`.
-    let channel = ClientConnection.insecure(group: group)
-      .connect(host: "localhost", port: self.port)
+    let channel = try GRPCChannelPool.with(
+      target: .host("localhost", port: self.port),
+      transportSecurity: .plaintext,
+      eventLoopGroup: group
+    )
 
 
     // Close the connection when we're done with it.
     // Close the connection when we're done with it.
     defer {
     defer {

+ 7 - 4
Sources/Examples/RouteGuide/Client/main.swift

@@ -21,9 +21,12 @@ import NIO
 import RouteGuideModel
 import RouteGuideModel
 
 
 /// Makes a `RouteGuide` client for a service hosted on "localhost" and listening on the given port.
 /// Makes a `RouteGuide` client for a service hosted on "localhost" and listening on the given port.
-func makeClient(port: Int, group: EventLoopGroup) -> Routeguide_RouteGuideClient {
-  let channel = ClientConnection.insecure(group: group)
-    .connect(host: "localhost", port: port)
+func makeClient(port: Int, group: EventLoopGroup) throws -> Routeguide_RouteGuideClient {
+  let channel = try GRPCChannelPool.with(
+    target: .host("localhost", port: port),
+    transportSecurity: .plaintext,
+    eventLoopGroup: group
+  )
 
 
   return Routeguide_RouteGuideClient(channel: channel)
   return Routeguide_RouteGuideClient(channel: channel)
 }
 }
@@ -207,7 +210,7 @@ struct RouteGuide: ParsableCommand {
     }
     }
 
 
     // Make a client, make sure we close it when we're done.
     // Make a client, make sure we close it when we're done.
-    let routeGuide = makeClient(port: self.port, group: group)
+    let routeGuide = try makeClient(port: self.port, group: group)
     defer {
     defer {
       try? routeGuide.channel.close().wait()
       try? routeGuide.channel.close().wait()
     }
     }

+ 7 - 4
docs/basic-tutorial.md

@@ -469,8 +469,8 @@ To call service methods, we first need to create a *stub*. All generated Swift
 stubs are *non-blocking/asynchronous*.
 stubs are *non-blocking/asynchronous*.
 
 
 First we need to create a gRPC channel for our stub, we're not using TLS so we
 First we need to create a gRPC channel for our stub, we're not using TLS so we
-use the `insecure` builder and specify the server address and port we want to
-connect to:
+use the `.plaintext` security transport and specify the server address and port
+we want to connect to:
 
 
 ```swift
 ```swift
 let group = PlatformSupport.makeEventLoopGroup(loopCount: 1)
 let group = PlatformSupport.makeEventLoopGroup(loopCount: 1)
@@ -478,8 +478,11 @@ defer {
   try? group.syncShutdownGracefully()
   try? group.syncShutdownGracefully()
 }
 }
 
 
-let channel = ClientConnection.insecure(group: group)
-  .connect(host: "localhost", port: port)
+let channel = try GRPCChannelPool.with(
+  target: .host("localhost", port: port),
+  transportSecurity: .plaintext,
+  eventLoopGroup: group
+)
 
 
 let client = Routeguide_RouteGuideClient(channel: channel)
 let client = Routeguide_RouteGuideClient(channel: channel)
 ```
 ```

+ 2 - 2
docs/client-without-codegen.md

@@ -8,8 +8,8 @@ can use `AnyServiceClient`. For example, to call the "SayHello" RPC on the
 [Greeter][helloworld-source] service you can do the following:
 [Greeter][helloworld-source] service you can do the following:
 
 
 ```swift
 ```swift
-let connection = ... // get a ClientConnection
-let anyService = AnyServiceClient(connection: connection)
+let channel = ... // get a GRPCChannel
+let anyService = AnyServiceClient(channel: channel)
 
 
 let sayHello = anyService.makeUnaryCall(
 let sayHello = anyService.makeUnaryCall(
   path: "/helloworld.Greeter/SayHello",
   path: "/helloworld.Greeter/SayHello",

+ 11 - 10
docs/keepalive.md

@@ -2,11 +2,11 @@
 
 
 gRPC sends HTTP2 pings on the transport to detect if the connection is down.
 gRPC sends HTTP2 pings on the transport to detect if the connection is down.
 If the ping is not acknowledged by the other side within a certain period, the connection
 If the ping is not acknowledged by the other side within a certain period, the connection
-will be closed. Note that pings are only necessary when there is no activity on the connection. 
+will be closed. Note that pings are only necessary when there is no activity on the connection.
 
 
 ## What should I set?
 ## What should I set?
 
 
-It should be sufficient for most users to only change `interval` and `timeout` properties, but the 
+It should be sufficient for most users to only change `interval` and `timeout` properties, but the
 following properties can also be useful in certain use cases.
 following properties can also be useful in certain use cases.
 
 
 Property | Client | Server | Description
 Property | Client | Server | Description
@@ -19,7 +19,7 @@ minimumSentPingIntervalWithoutData|.minutes(5)|.minutes(5)|If there are no data/
 minimumReceivedPingIntervalWithoutData|N/A|.minutes(5)|If there are no data/header frames being sent: the minimum amount of time expected between receiving successive pings. If the time between successive pings is less than this value, then the ping will be considered a bad ping from the peer. Such a ping counts as a "ping strike".
 minimumReceivedPingIntervalWithoutData|N/A|.minutes(5)|If there are no data/header frames being sent: the minimum amount of time expected between receiving successive pings. If the time between successive pings is less than this value, then the ping will be considered a bad ping from the peer. Such a ping counts as a "ping strike".
 maximumPingStrikes|N/A|2|Maximum number of bad pings that the server will tolerate before sending an HTTP2 GOAWAY frame and closing the connection. Setting it to `0` allows the server to accept any number of bad pings.
 maximumPingStrikes|N/A|2|Maximum number of bad pings that the server will tolerate before sending an HTTP2 GOAWAY frame and closing the connection. Setting it to `0` allows the server to accept any number of bad pings.
 
 
-### Client 
+### Client
 
 
 ```swift
 ```swift
 let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
 let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
@@ -29,13 +29,14 @@ let keepalive = ClientConnectionKeepalive(
   timeout: .seconds(10)
   timeout: .seconds(10)
 )
 )
 
 
-let configuration = ClientConnection.Configuration(
-  target: .hostAndPort("localhost", 443),
-  eventLoopGroup: group,
-  connectionKeepalive: keepalive
-)
-
-let client = ClientConnection(configuration: configuration)
+let channel = try GRPCChannelPool.with(
+  target: .host("localhost"),
+  transportSecurity: .tls(...),
+  eventLoopGroup: group
+) {
+  // Configure keepalive.
+  $0.keepalive = keepalive
+}
 ```
 ```
 
 
 ### Server
 ### Server