Writing gRPC Clients and Servers with C++20 Coroutines
Full source code
gRPC's asynchronous interface is quite complex, especially for servers that need to support concurrency. So how can we combine it with coroutines to write simple and easy-to-understand code?
Please read the blog for implementation details; here we are only showing the final code.
Protocol:
syntax = "proto3";
package sample;
service SampleService {
// Unary RPC: simplest request-response pattern
rpc Echo(EchoRequest) returns (EchoResponse);
// Server streaming: server continuously pushes data to the client
rpc GetNumbers(GetNumbersRequest) returns (stream Number);
// Client streaming: client continuously sends data, server returns one result
rpc Sum(stream Number) returns (SumResponse);
// Bidirectional streaming: both sides can send and receive continuously
rpc Chat(stream ChatMessage) returns (stream ChatMessage);
}
message EchoRequest { string message = 1; }
message EchoResponse { string message = 1; int64 timestamp = 2; }
message GetNumbersRequest { int32 value = 1; int32 count = 2; }
message Number { int32 value = 1; }
message SumResponse { int32 total = 1; int32 count = 2; }
message ChatMessage { string user = 1; string content = 2; int64 timestamp = 3; }
Client:
class Client final : public GenericClient<sample::SampleService> {
public:
using GenericClient::GenericClient;
static Client make(const std::string &address) {
return Client{sample::SampleService::NewStub(grpc::CreateChannel(address, grpc::InsecureChannelCredentials()))};
}
asyncio::task::Task<sample::EchoResponse>
echo(
sample::EchoRequest request,
std::unique_ptr<grpc::ClientContext> context = std::make_unique<grpc::ClientContext>()
) {
co_return co_await call(&sample::SampleService::Stub::async::Echo, std::move(context), std::move(request));
}
asyncio::task::Task<void>
getNumbers(
sample::GetNumbersRequest request,
asyncio::Sender<sample::Number> sender,
std::unique_ptr<grpc::ClientContext> context = std::make_unique<grpc::ClientContext>()
) {
co_await call(
&sample::SampleService::Stub::async::GetNumbers,
std::move(context),
std::move(request),
std::move(sender)
);
}
asyncio::task::Task<sample::SumResponse> sum(
asyncio::Receiver<sample::Number> receiver,
std::unique_ptr<grpc::ClientContext> context = std::make_unique<grpc::ClientContext>()
) {
co_return co_await call(&sample::SampleService::Stub::async::Sum, std::move(context), std::move(receiver));
}
asyncio::task::Task<void>
chat(
asyncio::Receiver<sample::ChatMessage> receiver,
asyncio::Sender<sample::ChatMessage> sender,
std::unique_ptr<grpc::ClientContext> context = std::make_unique<grpc::ClientContext>()
) {
co_return co_await call(
&sample::SampleService::Stub::async::Chat,
std::move(context),
std::move(receiver),
std::move(sender)
);
}
};
asyncio::task::Task<void> asyncMain(const int argc, char *argv[]) {
auto client = Client::make("localhost:50051");
co_await all(
asyncio::task::spawn([&]() -> asyncio::task::Task<void> {
sample::EchoRequest req;
req.set_message("Hello gRPC!");
const auto resp = co_await client.echo(req);
fmt::print("Echo: {}\n", resp.message());
}),
asyncio::task::spawn([&]() -> asyncio::task::Task<void> {
sample::GetNumbersRequest req;
req.set_value(1);
req.set_count(5);
auto [sender, receiver] = asyncio::channel<sample::Number>();
const auto result = co_await all(
client.getNumbers(req, std::move(sender)),
client.sum(std::move(receiver))
);
const auto &resp = std::get<sample::SumResponse>(result);
fmt::print("Sum: {}, count: {}\n", resp.total(), resp.count());
}),
asyncio::task::spawn([&]() -> asyncio::task::Task<void> {
auto [inSender, inReceiver] = asyncio::channel<sample::ChatMessage>();
auto [outSender, outReceiver] = asyncio::channel<sample::ChatMessage>();
co_await all(
client.chat(std::move(outReceiver), std::move(inSender)),
asyncio::task::spawn([&]() -> asyncio::task::Task<void> {
sample::ChatMessage msg;
msg.set_content("Hello server!");
co_await asyncio::error::guard(outSender.send(std::move(msg)));
outSender.close();
}),
asyncio::task::spawn([&]() -> asyncio::task::Task<void> {
const auto msg = co_await asyncio::error::guard(inReceiver.receive());
fmt::print("Chat reply: {}\n", msg.content());
})
);
})
);
}
Server:
class Server final : public GenericServer<sample::SampleService> {
public:
using GenericServer::GenericServer;
static Server make(const std::string &address) {
auto service = std::make_unique<sample::SampleService::AsyncService>();
grpc::ServerBuilder builder;
builder.AddListeningPort(address, grpc::InsecureServerCredentials());
builder.RegisterService(service.get());
auto completionQueue = builder.AddCompletionQueue();
auto server = builder.BuildAndStart();
return {std::move(server), std::move(service), std::move(completionQueue)};
}
private:
static asyncio::task::Task<sample::EchoResponse> echo(sample::EchoRequest request) {
sample::EchoResponse response;
response.set_message(request.message());
response.set_timestamp(std::time(nullptr));
co_return response;
}
static asyncio::task::Task<void>
getNumbers(sample::GetNumbersRequest request, Writer<sample::Number> writer) {
for (int i = 0; i < request.count(); ++i) {
sample::Number number;
number.set_value(request.value() + i);
co_await writer.write(number);
}
}
static asyncio::task::Task<sample::SumResponse> sum(Reader<sample::Number> reader) {
int total{0}, count{0};
while (const auto number = co_await reader.read()) {
total += number->value();
++count;
}
sample::SumResponse response;
response.set_total(total);
response.set_count(count);
co_return response;
}
static asyncio::task::Task<void> chat(Stream<sample::ChatMessage, sample::ChatMessage> stream) {
while (const auto message = co_await stream.read()) {
sample::ChatMessage response;
response.set_user("Server");
response.set_timestamp(std::time(nullptr));
response.set_content(fmt::format("Echo: {}", message->content()));
co_await stream.write(response);
}
}
asyncio::task::Task<void> dispatch() override {
co_await all(
handle(&sample::SampleService::AsyncService::RequestEcho, echo),
handle(&sample::SampleService::AsyncService::RequestGetNumbers, getNumbers),
handle(&sample::SampleService::AsyncService::RequestSum, sum),
handle(&sample::SampleService::AsyncService::RequestChat, chat)
);
}
};
asyncio::task::Task<void> asyncMain(const int argc, char *argv[]) {
auto server = Server::make("0.0.0.0:50051");
auto signal = asyncio::Signal::make();
co_await race(
asyncio::task::spawn([&]() -> asyncio::task::Task<void> {
asyncio::sync::Event event;
co_await asyncio::task::Cancellable{
all(
server.run(),
asyncio::task::spawn([&]() -> asyncio::task::Task<void> {
co_await asyncio::error::guard(event.wait());
co_await server.shutdown(); })
),
[&]() -> std::expected<void, std::error_code> {
event.set(); return {};
}
};
}),
asyncio::task::spawn([&]() -> asyncio::task::Task<void> {
co_await asyncio::error::guard(signal.on(SIGINT));
})
);
}
Because some people complained that it was too long, I reposted it on my own blog.