-
Notifications
You must be signed in to change notification settings - Fork 36
/
streaming-client.cpp
264 lines (206 loc) · 9.54 KB
/
streaming-client.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// Copyright 2024 Dennis Hezel
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "awaitable_client_rpc.hpp"
#include "example/v1/example.grpc.pb.h"
#include "example/v1/example_ext.grpc.pb.h"
#include "helper.hpp"
#include "rethrow_first_arg.hpp"
#include <agrpc/alarm.hpp>
#include <agrpc/client_rpc.hpp>
#include <boost/asio/bind_executor.hpp>
#include <boost/asio/co_spawn.hpp>
#include <boost/asio/deferred.hpp>
#include <boost/asio/detached.hpp>
#include <boost/asio/experimental/awaitable_operators.hpp>
#include <boost/asio/experimental/parallel_group.hpp>
#include <grpcpp/client_context.h>
#include <grpcpp/create_channel.h>
#include <iostream>
namespace asio = boost::asio;
using ExampleStub = example::v1::Example::Stub;
using ExampleExtStub = example::v1::ExampleExt::Stub;
// Example showing some of the features of the ClientRPC API of asio-grpc with Boost.Asio.
// begin-snippet: client-side-client-rpc-streaming
// A simple client-streaming request with coroutines.
// end-snippet
asio::awaitable<void> make_client_streaming_request(agrpc::GrpcContext& grpc_context, ExampleStub& stub)
{
using RPC = example::AwaitableClientRPC<&ExampleStub::PrepareAsyncClientStreaming>;
RPC rpc{grpc_context};
rpc.context().set_deadline(std::chrono::system_clock::now() + std::chrono::seconds(5));
example::v1::Response response;
const bool start_ok = co_await rpc.start(stub, response);
abort_if_not(start_ok);
// Optionally read initial metadata first. Otherwise it will be read along with the first write.
const bool read_ok = co_await rpc.read_initial_metadata();
// Send a message.
example::v1::Request request;
const bool write_ok = co_await rpc.write(request);
// Wait for the server to recieve all our messages and obtain the server's response + status.
const grpc::Status status = co_await rpc.finish();
abort_if_not(status.ok());
std::cout << "ClientRPC: Client streaming completed. Response: " << response.integer() << '\n';
silence_unused(read_ok, write_ok);
}
// ---------------------------------------------------
//
// begin-snippet: client-rpc-server-streaming
// A simple server-streaming request with coroutines.
// end-snippet
asio::awaitable<void> make_server_streaming_request(agrpc::GrpcContext& grpc_context, ExampleStub& stub)
{
using RPC = example::AwaitableClientRPC<&ExampleStub::PrepareAsyncServerStreaming>;
RPC rpc{grpc_context};
rpc.context().set_deadline(std::chrono::system_clock::now() + std::chrono::seconds(5));
example::v1::Request request;
request.set_integer(5);
abort_if_not(co_await rpc.start(stub, request));
example::v1::Response response;
while (co_await rpc.read(response))
{
std::cout << "ClientRPC: Server streaming: " << response.integer() << "\n";
}
const grpc::Status status = co_await rpc.finish();
abort_if_not(status.ok());
std::cout << "ClientRPC: Server streaming completed\n";
}
// ---------------------------------------------------
//
// A server-streaming request that is cancelled.
asio::awaitable<void> make_server_streaming_notify_when_done_request(agrpc::GrpcContext& grpc_context,
ExampleStub& stub)
{
using RPC = example::AwaitableClientRPC<&ExampleStub::PrepareAsyncServerStreaming>;
RPC rpc{grpc_context};
rpc.context().set_deadline(std::chrono::system_clock::now() + std::chrono::seconds(5));
example::v1::Request request;
request.set_integer(1);
abort_if_not(co_await rpc.start(stub, request));
example::v1::Response response;
co_await rpc.read(response);
// cancellation also happens automatically at the end of this scope
rpc.cancel();
const grpc::Status status = co_await rpc.finish();
abort_if_not(grpc::StatusCode::CANCELLED == status.error_code());
std::cout << "ClientRPC: Server streaming notify_when_done completed\n";
}
// ---------------------------------------------------
//
// begin-snippet: client-rpc-bidirectional-streaming
// A bidirectional-streaming request that simply sends the response from the server back to it.
// end-snippet
asio::awaitable<void> make_bidirectional_streaming_request(agrpc::GrpcContext& grpc_context, ExampleStub& stub)
{
using RPC = example::AwaitableClientRPC<&ExampleStub::PrepareAsyncBidirectionalStreaming>;
RPC rpc{grpc_context};
rpc.context().set_deadline(std::chrono::system_clock::now() + std::chrono::seconds(5));
if (!co_await rpc.start(stub))
{
// Channel is either permanently broken or transiently broken but with the fail-fast option.
co_return;
}
// Perform a request/response ping-pong.
example::v1::Request request;
request.set_integer(1);
example::v1::Response response;
// Reads and writes can be performed simultaneously.
using namespace asio::experimental::awaitable_operators;
auto [read_ok, write_ok] = co_await (rpc.read(response) && rpc.write(request));
int count{};
while (read_ok && write_ok && count < 10)
{
std::cout << "ClientRPC: Bidirectional streaming: " << response.integer() << '\n';
request.set_integer(response.integer());
++count;
std::tie(read_ok, write_ok) = co_await (rpc.read(response) && rpc.write(request));
}
// Finish will automatically signal that the client is done writing. Optionally call rpc.writes_done() to explicitly
// signal it earlier.
const grpc::Status status = co_await rpc.finish();
abort_if_not(status.ok());
}
// ---------------------------------------------------
//
// begin-snippet: client-side-run-with-deadline
// A unary request with a per-RPC step timeout. Using a unary RPC for demonstration purposes, the same mechanism can be
// applied to streaming RPCs, where it is arguably more useful.
// For unary RPCs, `grpc::ClientContext::set_deadline` should be preferred.
// end-snippet
asio::awaitable<void> make_and_cancel_unary_request(agrpc::GrpcContext& grpc_context, ExampleExtStub& stub)
{
using RPC = example::AwaitableClientRPC<&ExampleExtStub::PrepareAsyncSlowUnary>;
grpc::ClientContext client_context;
client_context.set_deadline(std::chrono::system_clock::now() + std::chrono::seconds(5));
RPC::Request request;
request.set_delay(2000); // tell server to delay response by 2000ms
RPC::Response response;
const auto not_to_exceed = std::chrono::steady_clock::now() + std::chrono::milliseconds(1900);
const auto result =
co_await asio::experimental::make_parallel_group(
RPC::request(grpc_context, stub, client_context, request, response, asio::deferred),
agrpc::Alarm(grpc_context)
.wait(std::chrono::system_clock::now() + std::chrono::milliseconds(100), asio::deferred))
.async_wait(asio::experimental::wait_for_one(), asio::use_awaitable);
// Alternative, slighlty less performant syntax:
//
// using namespace asio::experimental::awaitable_operators;
// co_await (RPC::request(grpc_context, stub, client_context, request, response) ||
// agrpc::Alarm(grpc_context).wait(deadline))
abort_if_not(grpc::StatusCode::CANCELLED == std::get<1>(result).error_code());
abort_if_not(std::chrono::steady_clock::now() < not_to_exceed);
}
// ---------------------------------------------------
//
// The Shutdown endpoint is used by unit tests.
asio::awaitable<void> make_shutdown_request(agrpc::GrpcContext& grpc_context, ExampleExtStub& stub)
{
using RPC = example::AwaitableClientRPC<&ExampleExtStub::PrepareAsyncShutdown>;
grpc::ClientContext client_context;
client_context.set_deadline(std::chrono::system_clock::now() + std::chrono::seconds(5));
google::protobuf::Empty response;
const grpc::Status status = co_await RPC::request(grpc_context, stub, client_context, {}, response);
if (status.ok())
{
std::cout << "ClientRPC: Successfully send shutdown request to server\n";
}
else
{
std::cout << "ClientRPC: Failed to send shutdown request to server: " << status.error_message() << '\n';
}
abort_if_not(status.ok());
}
// ---------------------------------------------------
//
int main(int argc, const char** argv)
{
const auto port = argc >= 2 ? argv[1] : "50051";
const auto host = std::string("localhost:") + port;
const auto channel = grpc::CreateChannel(host, grpc::InsecureChannelCredentials());
ExampleStub stub{channel};
ExampleExtStub stub_ext{channel};
agrpc::GrpcContext grpc_context;
asio::co_spawn(
grpc_context,
[&]() -> asio::awaitable<void>
{
co_await make_client_streaming_request(grpc_context, stub);
co_await make_server_streaming_request(grpc_context, stub);
co_await make_server_streaming_notify_when_done_request(grpc_context, stub);
co_await make_bidirectional_streaming_request(grpc_context, stub);
co_await make_and_cancel_unary_request(grpc_context, stub_ext);
co_await make_shutdown_request(grpc_context, stub_ext);
},
example::RethrowFirstArg{});
grpc_context.run();
}