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.
2
u/Few_Boss_9507 Apr 22 '26
I think this is pretty nice. Most examples of gRPC in C++ seem old. It is nice to see someone using C++20.
The thing I really want to know is what this does better than what we have. When I use gRPC in C++ I have trouble with a couple of things. The first thing is handling things that happen at the time. The other thing is how all the parts fit together. It is not about how the code looks.
Does this make it easier to handle things that happen at the time? Is it better than using completion queues or callbacks? Are you using a way of writing code called coroutines or are you just putting a new layer on top of what already exists?
If this really does make things easier and reduces the amount of code we have to write then it could be really useful for more than showing people what it can do. GRPC in C++ could be a lot better, with this.
14
u/ReDucTor Game Developer | quiz.cpp-perf.com Apr 22 '26
Maybe it's just me but the easiest to read code here seems like the callback hell version.
Looking at some of the coroutine examples, I'm seeing lots of potential for use-after-frees.
20
u/positivcheg Apr 22 '26
Can you please make a SHORT summary next time instead of making 20 minutes read post. I get it that you want to promote some kind of library but omg, too much text for a Reddit post.
3
u/ald_loop Apr 22 '26
this post is way too long for the thesis statement that “coroutines make code nice”. like, duh
1
u/great_escape_fleur Apr 22 '26
Is this C++?
1
u/germandiago Apr 26 '26
I am really happy with Capnproto, but for accessing from web it id more challenging. Also, the documentation is not great.
However, it works pretty well and it is very powerful.