Compound Radius 3 лет назад
Родитель
Сommit
7b13c340cc
2 измененных файлов с 53 добавлено и 35 удалено
  1. 1 1
      Makefile
  2. 52 34
      docs/quick-start.md

+ 1 - 1
Makefile

@@ -3,7 +3,7 @@ SWIFT:=swift
 # Where products will be built; this is the SPM default.
 SWIFT_BUILD_PATH:=./.build
 SWIFT_BUILD_CONFIGURATION=debug
-SWIFT_FLAGS=--build-path=${SWIFT_BUILD_PATH} --configuration=${SWIFT_BUILD_CONFIGURATION} --enable-test-discovery
+SWIFT_FLAGS=--scratch-path=${SWIFT_BUILD_PATH} --configuration=${SWIFT_BUILD_CONFIGURATION}
 # Force release configuration (for plugins)
 SWIFT_FLAGS_RELEASE=$(patsubst --configuration=%,--configuration=release,$(SWIFT_FLAGS))
 

+ 52 - 34
docs/quick-start.md

@@ -28,7 +28,7 @@ and other tutorials):
 
 ```sh
 $ # Clone the repository at the latest release to get the example code:
-$ git clone -b 1.0.0 https://github.com/grpc/grpc-swift
+$ git clone -b 1.13.0 https://github.com/grpc/grpc-swift
 $ # Navigate to the repository
 $ cd grpc-swift/
 ```
@@ -131,27 +131,27 @@ In the same directory, open
 method like this:
 
 ```swift
-class GreeterProvider: Helloworld_GreeterProvider {
+final class GreeterProvider: Helloworld_GreeterAsyncProvider {
+  let interceptors: Helloworld_GreeterServerInterceptorFactoryProtocol? = nil
+
   func sayHello(
     request: Helloworld_HelloRequest,
-    context: StatusOnlyCallContext
-  ) -> EventLoopFuture<Helloworld_HelloReply> {
+    context: GRPCAsyncServerCallContext
+  ) async throws -> Helloworld_HelloReply {
     let recipient = request.name.isEmpty ? "stranger" : request.name
-    let response = Helloworld_HelloReply.with {
+    return Helloworld_HelloReply.with {
       $0.message = "Hello \(recipient)!"
     }
-    return context.eventLoop.makeSucceededFuture(response)
   }
 
   func sayHelloAgain(
     request: Helloworld_HelloRequest,
-    context: StatusOnlyCallContext
-  ) -> EventLoopFuture<Helloworld_HelloReply> {
+    context: GRPCAsyncServerCallContext
+  ) async throws -> Helloworld_HelloReply {
     let recipient = request.name.isEmpty ? "stranger" : request.name
-    let response = Helloworld_HelloReply.with {
+    return Helloworld_HelloReply.with {
       $0.message = "Hello again \(recipient)!"
     }
-    return context.eventLoop.makeSucceededFuture(response)
   }
 }
 ```
@@ -159,36 +159,54 @@ class GreeterProvider: Helloworld_GreeterProvider {
 #### Update the client
 
 In the same directory, open
-`Sources/Examples/HelloWorld/Client/main.swift`. Call the new method like this:
+`Sources/Examples/HelloWorld/Client/HelloWorldClient.swift`. Call the new method like this:
 
 ```swift
-func greet(name: String?, client greeter: Helloworld_GreeterClient) {
-  // Form the request with the name, if one was provided.
-  let request = Helloworld_HelloRequest.with {
-    $0.name = name ?? ""
-  }
+func run() async throws {
+    // Setup an `EventLoopGroup` for the connection to run on.
+    //
+    // See: https://github.com/apple/swift-nio#eventloops-and-eventloopgroups
+    let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
+
+    // Make sure the group is shutdown when we're done with it.
+    defer {
+      try! group.syncShutdownGracefully()
+    }
 
-  // Make the RPC call to the server.
-  let sayHello = greeter.sayHello(request)
+    // Configure the channel, we're not using TLS so the connection is `insecure`.
+    let channel = try GRPCChannelPool.with(
+      target: .host("localhost", port: self.port),
+      transportSecurity: .plaintext,
+      eventLoopGroup: group
+    )
 
-  // wait() on the response to stop the program from exiting before the response is received.
-  do {
-    let response = try sayHello.response.wait()
-    print("Greeter received: \(response.message)")
-  } catch {
-    print("Greeter failed: \(error)")
-    return
-  }
+    // Close the connection when we're done with it.
+    defer {
+      try! channel.close().wait()
+    }
+
+    // Provide the connection to the generated client.
+    let greeter = Helloworld_GreeterAsyncClient(channel: channel)
 
-  let sayHelloAgain = greeter.sayHelloAgain(request)
-  do {
-    let response = try sayHelloAgain.response.wait()
-    print("Greeter received: \(response.message)")
-  } catch {
-    print("Greeter failed: \(error)")
-    return
+    // Form the request with the name, if one was provided.
+    let request = Helloworld_HelloRequest.with {
+      $0.name = self.name ?? ""
+    }
+
+    do {
+      let greeting = try await greeter.sayHello(request)
+      print("Greeter received: \(greeting.message)")
+    } catch {
+      print("Greeter failed: \(error)")
+    }
+
+    do {
+      let greetingAgain = try await greeter.sayHelloAgain(request)
+      print("Greeter received: \(greetingAgain.message)")
+    } catch {
+      print("Greeter failed: \(error)")
+    }
   }
-}
 ```
 
 #### Run!