Skip to content

Commit c57c6bd

Browse files
authored
Merge pull request grpc#208 from dgquintas/async_shutdown
Added section for async server shutdown in helloworld example
2 parents 42cc8bb + e8e26d0 commit c57c6bd

File tree

1 file changed

+54
-10
lines changed

1 file changed

+54
-10
lines changed

docs/tutorials/async/helloasync-cpp.md

+54-10
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,24 @@ title: Asynchronous Basics - C++
44
---
55
<h1 class="page-header">Asynchronous Basics: C++</h1>
66

7-
This tutorial shows you how to write a simple server and client in C++ using gRPC's asynchronous/non-blocking APIs. It assumes you are already familiar with writing simple synchronous gRPC code, as described in [gRPC Basics: C++](/docs/tutorials/basic/c.html). The example used in this tutorial follows on from the basic [Greeter example](https://github.com/grpc/grpc/tree/{{ site.data.config.grpc_release_branch }}/examples/cpp/helloworld) we used in the [overview](/docs/index.html). You'll find it along with installation instructions in [grpc/examples/cpp/helloworld](https://github.com/grpc/grpc/tree/{{ site.data.config.grpc_release_branch }}/examples/cpp/helloworld).
7+
This tutorial shows you how to write a simple server and client in C++ using
8+
gRPC's asynchronous/non-blocking APIs. It assumes you are already familiar with
9+
writing simple synchronous gRPC code, as described in [gRPC Basics:
10+
C++](/docs/tutorials/basic/c.html). The example used in this tutorial follows on
11+
from the basic [Greeter example](https://github.com/grpc/grpc/tree/{{
12+
site.data.config.grpc_release_branch }}/examples/cpp/helloworld) we used in the
13+
[overview](/docs/index.html). You'll find it along with installation
14+
instructions in
15+
[grpc/examples/cpp/helloworld](https://github.com/grpc/grpc/tree/{{
16+
site.data.config.grpc_release_branch }}/examples/cpp/helloworld).
817

918
<div id="toc"></div>
1019

1120
## Overview
1221

13-
gRPC uses the [`CompletionQueue`](http://www.grpc.io/grpc/cpp/classgrpc_1_1_completion_queue.html) API for asynchronous operations. The basic work flow
22+
gRPC uses the
23+
[`CompletionQueue`](http://www.grpc.io/grpc/cpp/classgrpc_1_1_completion_queue.html)
24+
API for asynchronous operations. The basic work flow
1425
is as follows:
1526

1627
- bind a `CompletionQueue` to an RPC call
@@ -20,7 +31,12 @@ is as follows:
2031

2132
## Async client
2233

23-
To use an asynchronous client to call a remote method, you first create a channel and stub, just as you do in a [synchronous client](https://github.com/grpc/grpc/blob/{{ site.data.config.grpc_release_branch }}/examples/cpp/helloworld/greeter_client.cc). Once you have your stub, you do the following to make an asynchronous call:
34+
To use an asynchronous client to call a remote method, you first create a
35+
channel and stub, just as you do in a [synchronous
36+
client](https://github.com/grpc/grpc/blob/{{
37+
site.data.config.grpc_release_branch
38+
}}/examples/cpp/helloworld/greeter_client.cc). Once you have your stub, you do
39+
the following to make an asynchronous call:
2440

2541
- Initiate the RPC and create a handle for it. Bind the RPC to a
2642
`CompletionQueue`.
@@ -50,12 +66,16 @@ To use an asynchronous client to call a remote method, you first create a channe
5066
}
5167
```
5268
53-
You can see the complete client example in [greeter&#95;async&#95;client.cc](https://github.com/grpc/grpc/blob/{{ site.data.config.grpc_release_branch }}/examples/cpp/helloworld/greeter_async_client.cc).
69+
You can see the complete client example in
70+
[greeter&#95;async&#95;client.cc](https://github.com/grpc/grpc/blob/{{
71+
site.data.config.grpc_release_branch
72+
}}/examples/cpp/helloworld/greeter_async_client.cc).
5473
5574
## Async server
5675
5776
The server implementation requests an RPC call with a tag and then waits for the
58-
completion queue to return the tag. The basic flow for handling an RPC asynchronously is:
77+
completion queue to return the tag. The basic flow for handling an RPC
78+
asynchronously is:
5979
6080
- Build a server exporting the async service
6181
@@ -104,8 +124,10 @@ completion queue to return the tag. The basic flow for handling an RPC asynchron
104124
}
105125
```
106126
107-
This basic flow, however, doesn't take into account the server handling multiple requests concurrently. To deal with this, our complete async server example uses a `CallData` object to
108-
maintain the state of each RPC, and uses the address of this object as the unique tag for the call.
127+
This basic flow, however, doesn't take into account the server handling multiple
128+
requests concurrently. To deal with this, our complete async server example uses
129+
a `CallData` object to maintain the state of each RPC, and uses the address of
130+
this object as the unique tag for the call.
109131
110132
```
111133
class CallData {
@@ -153,8 +175,8 @@ maintain the state of each RPC, and uses the address of this object as the uniqu
153175
}
154176
```
155177
156-
For simplicity the server only uses one completion queue for all events, and runs a
157-
main loop in `HandleRpcs` to query the queue:
178+
For simplicity the server only uses one completion queue for all events, and
179+
runs a main loop in `HandleRpcs` to query the queue:
158180
159181
```
160182
void HandleRpcs() {
@@ -173,7 +195,29 @@ main loop in `HandleRpcs` to query the queue:
173195
}
174196
```
175197
176-
You can see our complete server example in [greeter&#95;async&#95;server.cc](https://github.com/grpc/grpc/blob/{{ site.data.config.grpc_release_branch }}/examples/cpp/helloworld/greeter_async_server.cc).
198+
### Shutting Down the Server
199+
We've been using a completion queue to get the async notifications. Care must be
200+
taken to shut it down *after* the server has also been shut down.
177201
202+
Remember we got our completion queue instance `cq_` in `ServerImpl::Run()` by
203+
running `cq_ = builder.AddCompletionQueue()`. Looking at
204+
`ServerBuilder::AddCompletionQueue`'s documentation we see that
178205
206+
> ... Caller is required to shutdown the server prior to shutting down the
207+
> returned completion queue.
179208
209+
Refer to `ServerBuilder::AddCompletionQueue`'s full docstring for more details.
210+
What this means in our example is that `ServerImpl's` destructor looks like:
211+
212+
```
213+
~ServerImpl() {
214+
server_->Shutdown();
215+
// Always shutdown the completion queue after the server.
216+
cq_->Shutdown();
217+
}
218+
```
219+
220+
You can see our complete server example in
221+
[greeter&#95;async&#95;server.cc](https://github.com/grpc/grpc/blob/{{
222+
site.data.config.grpc_release_branch
223+
}}/examples/cpp/helloworld/greeter_async_server.cc).

0 commit comments

Comments
 (0)