Skip to content

Commit 2de57b7

Browse files
compoundradiusWendellXY
authored andcommitted
update quick-start.md (grpc#1527)
1 parent 6ce7249 commit 2de57b7

File tree

2 files changed

+53
-35
lines changed

2 files changed

+53
-35
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ SWIFT:=swift
33
# Where products will be built; this is the SPM default.
44
SWIFT_BUILD_PATH:=./.build
55
SWIFT_BUILD_CONFIGURATION=debug
6-
SWIFT_FLAGS=--build-path=${SWIFT_BUILD_PATH} --configuration=${SWIFT_BUILD_CONFIGURATION} --enable-test-discovery
6+
SWIFT_FLAGS=--scratch-path=${SWIFT_BUILD_PATH} --configuration=${SWIFT_BUILD_CONFIGURATION}
77
# Force release configuration (for plugins)
88
SWIFT_FLAGS_RELEASE=$(patsubst --configuration=%,--configuration=release,$(SWIFT_FLAGS))
99

docs/quick-start.md

+52-34
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ and other tutorials):
2828

2929
```sh
3030
$ # Clone the repository at the latest release to get the example code:
31-
$ git clone -b 1.0.0 https://github.com/grpc/grpc-swift
31+
$ git clone -b 1.13.0 https://github.com/grpc/grpc-swift
3232
$ # Navigate to the repository
3333
$ cd grpc-swift/
3434
```
@@ -131,64 +131,82 @@ In the same directory, open
131131
method like this:
132132

133133
```swift
134-
class GreeterProvider: Helloworld_GreeterProvider {
134+
final class GreeterProvider: Helloworld_GreeterAsyncProvider {
135+
let interceptors: Helloworld_GreeterServerInterceptorFactoryProtocol? = nil
136+
135137
func sayHello(
136138
request: Helloworld_HelloRequest,
137-
context: StatusOnlyCallContext
138-
) -> EventLoopFuture<Helloworld_HelloReply> {
139+
context: GRPCAsyncServerCallContext
140+
) async throws -> Helloworld_HelloReply {
139141
let recipient = request.name.isEmpty ? "stranger" : request.name
140-
let response = Helloworld_HelloReply.with {
142+
return Helloworld_HelloReply.with {
141143
$0.message = "Hello \(recipient)!"
142144
}
143-
return context.eventLoop.makeSucceededFuture(response)
144145
}
145146

146147
func sayHelloAgain(
147148
request: Helloworld_HelloRequest,
148-
context: StatusOnlyCallContext
149-
) -> EventLoopFuture<Helloworld_HelloReply> {
149+
context: GRPCAsyncServerCallContext
150+
) async throws -> Helloworld_HelloReply {
150151
let recipient = request.name.isEmpty ? "stranger" : request.name
151-
let response = Helloworld_HelloReply.with {
152+
return Helloworld_HelloReply.with {
152153
$0.message = "Hello again \(recipient)!"
153154
}
154-
return context.eventLoop.makeSucceededFuture(response)
155155
}
156156
}
157157
```
158158

159159
#### Update the client
160160

161161
In the same directory, open
162-
`Sources/Examples/HelloWorld/Client/main.swift`. Call the new method like this:
162+
`Sources/Examples/HelloWorld/Client/HelloWorldClient.swift`. Call the new method like this:
163163

164164
```swift
165-
func greet(name: String?, client greeter: Helloworld_GreeterClient) {
166-
// Form the request with the name, if one was provided.
167-
let request = Helloworld_HelloRequest.with {
168-
$0.name = name ?? ""
169-
}
165+
func run() async throws {
166+
// Setup an `EventLoopGroup` for the connection to run on.
167+
//
168+
// See: https://github.com/apple/swift-nio#eventloops-and-eventloopgroups
169+
let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
170+
171+
// Make sure the group is shutdown when we're done with it.
172+
defer {
173+
try! group.syncShutdownGracefully()
174+
}
170175

171-
// Make the RPC call to the server.
172-
let sayHello = greeter.sayHello(request)
176+
// Configure the channel, we're not using TLS so the connection is `insecure`.
177+
let channel = try GRPCChannelPool.with(
178+
target: .host("localhost", port: self.port),
179+
transportSecurity: .plaintext,
180+
eventLoopGroup: group
181+
)
173182

174-
// wait() on the response to stop the program from exiting before the response is received.
175-
do {
176-
let response = try sayHello.response.wait()
177-
print("Greeter received: \(response.message)")
178-
} catch {
179-
print("Greeter failed: \(error)")
180-
return
181-
}
183+
// Close the connection when we're done with it.
184+
defer {
185+
try! channel.close().wait()
186+
}
187+
188+
// Provide the connection to the generated client.
189+
let greeter = Helloworld_GreeterAsyncClient(channel: channel)
182190

183-
let sayHelloAgain = greeter.sayHelloAgain(request)
184-
do {
185-
let response = try sayHelloAgain.response.wait()
186-
print("Greeter received: \(response.message)")
187-
} catch {
188-
print("Greeter failed: \(error)")
189-
return
191+
// Form the request with the name, if one was provided.
192+
let request = Helloworld_HelloRequest.with {
193+
$0.name = self.name ?? ""
194+
}
195+
196+
do {
197+
let greeting = try await greeter.sayHello(request)
198+
print("Greeter received: \(greeting.message)")
199+
} catch {
200+
print("Greeter failed: \(error)")
201+
}
202+
203+
do {
204+
let greetingAgain = try await greeter.sayHelloAgain(request)
205+
print("Greeter received: \(greetingAgain.message)")
206+
} catch {
207+
print("Greeter failed: \(error)")
208+
}
190209
}
191-
}
192210
```
193211

194212
#### Run!

0 commit comments

Comments
 (0)