Home

Awesome

asio-grpc

Reliability Rating Reliability Rating vcpkg conan hunter

An Executor, Networking TS and std::execution interface to grpc::CompletionQueue for writing asynchronous gRPC clients and servers using C++20 coroutines, Boost.Coroutines, Asio's stackless coroutines, callbacks, sender/receiver and more.

Features

Example

Hello world client using C++20 coroutines. Other Asio completion tokens are supported as well.

<!-- snippet: client-side-hello-world -->

<a id='snippet-client-side-hello-world'></a>

helloworld::Greeter::Stub stub(grpc::CreateChannel("localhost:50051", grpc::InsecureChannelCredentials()));
agrpc::GrpcContext grpc_context;
asio::co_spawn(
    grpc_context,
    [&]() -> asio::awaitable<void>
    {
        using RPC = agrpc::ClientRPC<&helloworld::Greeter::Stub::PrepareAsyncSayHello>;
        grpc::ClientContext client_context;
        helloworld::HelloRequest request;
        request.set_name("world");
        helloworld::HelloReply response;
        const grpc::Status status =
            co_await RPC::request(grpc_context, stub, client_context, request, response, asio::use_awaitable);
        assert(status.ok());
    },
    asio::detached);
grpc_context.run();

<sup><a href='/example/snippets/client.cpp#L83-L101' title='Snippet source file'>snippet source</a> | <a href='#snippet-client-side-hello-world' title='Start of snippet'>anchor</a></sup>

<!-- endSnippet -->

Requirements

Asio-grpc is a C++17, header-only library. To install it, CMake (3.14+) is all that is needed.

To use it, gRPC and either Boost.Asio (min. 1.74.0), standalone Asio (min. 1.17.0), libunifex or stdexec must be present and linked into your application.

Officially supported compilers are GCC 8+, Clang 10+, AppleClang 15+ and latest MSVC.

Usage

The library can be added to a CMake project using either add_subdirectory or find_package. Once set up, include the individual headers from the agrpc directory or the convenience header:

#include <agrpc/asio_grpc.hpp>
<details><summary><b>vcpkg</b></summary> <p>

Add asio-grpc to the dependencies inside your vcpkg.json:

{
    "name": "your_app",
    "version": "0.1.0",
    "dependencies": [
        "asio-grpc",
        // To use the Boost.Asio backend add
        // "boost-asio",
        // To use the standalone Asio backend add
        // "asio",
        // To use the libunifex backend add
        // "libunifex",
        // To use the stdexec backend add
        // "stdexec"
    ]
}

Find asio-grpc and link it to your target.

Using Boost.Asio:

find_package(asio-grpc CONFIG REQUIRED)
find_package(Boost REQUIRED)
target_link_libraries(your_app PUBLIC asio-grpc::asio-grpc Boost::headers)

Or using standalone Asio:

find_package(asio-grpc CONFIG REQUIRED)
find_package(asio CONFIG REQUIRED)
target_link_libraries(your_app PUBLIC asio-grpc::asio-grpc-standalone-asio asio::asio)

Or using libunifex:

find_package(asio-grpc CONFIG REQUIRED)
find_package(unifex CONFIG REQUIRED)
target_link_libraries(your_app PUBLIC asio-grpc::asio-grpc-unifex unifex::unifex)

Or using stdexec:

find_package(asio-grpc CONFIG REQUIRED)
find_package(stdexec CONFIG REQUIRED)
target_link_libraries(your_app PUBLIC asio-grpc::asio-grpc-stdexec STDEXEC::stdexec)
</p> </details> <details><summary><b>Hunter</b></summary> <p>

See asio-grpc's documentation on the Hunter website: https://hunter.readthedocs.io/en/latest/packages/pkg/asio-grpc.html.

</p> </details> <details><summary><b>conan</b></summary> <p>

The recipe in conan-center is called asio-grpc.
If you are using conan's CMake generator then link with asio-grpc::asio-grpc independent of the backend that you choose:

find_package(asio-grpc)
target_link_libraries(your_app PUBLIC asio-grpc::asio-grpc)
</p> </details> <details><summary><b>CMake package</b></summary> <p>

Clone the repository and install it.

cmake -B build -DCMAKE_INSTALL_PREFIX=/desired/installation/directory .
cmake --build build --target install

Locate it and link it to your target.

Using Boost.Asio:

# Make sure CMAKE_PREFIX_PATH contains /desired/installation/directory
find_package(asio-grpc CONFIG REQUIRED)
find_package(Boost)
target_link_libraries(your_app PUBLIC asio-grpc::asio-grpc Boost::headers)

Or using standalone Asio:

# Make sure CMAKE_PREFIX_PATH contains /desired/installation/directory
find_package(asio-grpc CONFIG REQUIRED)
find_package(asio)
target_link_libraries(your_app PUBLIC asio-grpc::asio-grpc-standalone-asio asio::asio)

Or using libunifex:

# Make sure CMAKE_PREFIX_PATH contains /desired/installation/directory
find_package(asio-grpc CONFIG REQUIRED)
find_package(unifex)
target_link_libraries(your_app PUBLIC asio-grpc::asio-grpc-unifex unifex::unifex)

Or using stdexec:

# Make sure CMAKE_PREFIX_PATH contains /desired/installation/directory
find_package(asio-grpc CONFIG REQUIRED)
find_package(stdexec)
target_link_libraries(your_app PUBLIC asio-grpc::asio-grpc-stdexec STDEXEC::stdexec)
</p> </details> <details><summary><b>CMake subdirectory</b></summary> <p>

Clone the repository into a subdirectory of your CMake project. Then add it and link it to your target.

Independent of the backend you chose, find and link with gRPC:

find_package(gRPC)
target_link_libraries(your_app PUBLIC gRPC::grpc++)

Using Boost.Asio:

add_subdirectory(/path/to/asio-grpc)
find_package(Boost)
target_link_libraries(your_app PUBLIC asio-grpc::asio-grpc Boost::headers)

Or using standalone Asio:

add_subdirectory(/path/to/asio-grpc)
find_package(asio)
target_link_libraries(your_app PUBLIC asio-grpc::asio-grpc-standalone-asio asio::asio)

Or using libunifex:

add_subdirectory(/path/to/asio-grpc)
find_package(unifex)
target_link_libraries(your_app PUBLIC asio-grpc::asio-grpc-unifex unifex::unifex)

Or using stdexec:

add_subdirectory(/path/to/asio-grpc)
find_package(stdexec)
target_link_libraries(your_app PUBLIC asio-grpc::asio-grpc-stdexec STDEXEC::stdexec)
</p> </details> <details><summary><b>Raw source code</b></summary> <p>

This type of usage is unsupported. Future versions of asio-grpc might break it without notice.

Copy the contents of the src/ directory into your project and add it to your project's include directories. Depending on your desired backend: Boost.Asio, standalone Asio, libunifex or stdexec, set the preprocessor definitions AGRPC_BOOST_ASIO, AGRPC_STANDALONE_ASIO, AGRPC_UNIFEX or AGRPC_STDEXEC respectively. Also make sure that the backend's header files and libraries can be found correctly.

</p> </details>

CMake Options

ASIO_GRPC_DISABLE_AUTOLINK - Set before using find_package(asio-grpc) to prevent asio-grpcConfig.cmake from finding and setting up interface link libraries like gRPC::grpc++.

Performance

asio-grpc is part of grpc_bench. Head over there to compare its performance against other libraries and languages.

<details><summary><b>Results</b></summary> <p>

Below are the results from the helloworld unary RPC for:
Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
Linux, GCC 12.2.0, Boost 1.80.0, gRPC 1.52.1, asio-grpc v2.5.0, jemalloc 5.2.1
Request scenario: string_100B

1 CPU server

namereq/savg. latency90 % in95 % in99 % inavg. cpuavg. memory
rust_thruster_mt4879620.19 ms9.42 ms12.11 ms516.23 ms104.51%12.06 MiB
rust_tonic_mt4334322.86 ms10.42 ms11.29 ms662.73 ms102.29%14.39 MiB
go_grpc3854125.33 ms38.74 ms42.98 ms53.94 ms100.0%25.19 MiB
rust_grpcio3475728.65 ms30.18 ms30.60 ms31.68 ms101.91%18.59 MiB
cpp_grpc_mt3343329.77 ms31.56 ms32.07 ms33.58 ms102.22%5.69 MiB
cpp_asio_grpc_callback3252130.61 ms32.54 ms33.14 ms35.29 ms101.65%5.93 MiB
cpp_asio_grpc_unifex3250730.62 ms32.50 ms32.99 ms34.66 ms102.94%5.81 MiB
cpp_asio_grpc_coroutine2889334.47 ms36.78 ms37.37 ms38.88 ms102.52%5.56 MiB
cpp_asio_grpc_io_context_coro2807235.47 ms37.77 ms38.22 ms39.93 ms77.73%5.39 MiB
cpp_grpc_callback1024390.44 ms118.77 ms164.20 ms175.43 ms100.62%44.9 MiB

2 CPU server

namereq/savg. latency90 % in95 % in99 % inavg. cpuavg. memory
cpp_grpc_mt875509.66 ms15.11 ms18.23 ms27.03 ms204.66%26.15 MiB
cpp_asio_grpc_unifex865689.83 ms15.34 ms18.55 ms27.12 ms207.78%27.54 MiB
cpp_asio_grpc_callback8529210.03 ms15.38 ms18.51 ms26.62 ms206.63%24.73 MiB
cpp_asio_grpc_coroutine7964711.04 ms18.01 ms21.08 ms28.67 ms212.19%25.04 MiB
cpp_asio_grpc_io_context_coro7795311.24 ms18.32 ms21.61 ms29.20 ms161.24%28.4 MiB
rust_thruster_mt7579311.90 ms26.84 ms40.49 ms59.71 ms186.64%13.85 MiB
cpp_grpc_callback6820312.24 ms23.93 ms28.62 ms41.83 ms206.38%52.79 MiB
rust_tonic_mt6716213.85 ms34.05 ms46.31 ms69.58 ms206.13%17.24 MiB
rust_grpcio6077515.49 ms22.85 ms25.77 ms31.14 ms218.05%30.15 MiB
go_grpc5819215.87 ms24.31 ms27.10 ms32.43 ms197.71%25.06 MiB
</p> </details>

Documentation

Documentation | Examples

The main workhorses of this library are the agrpc::GrpcContext and its executor_type - agrpc::GrpcExecutor.

The agrpc::GrpcContext implements asio::execution_context and can be used as an argument to Asio functions that expect an ExecutionContext like asio::spawn.

Likewise, the agrpc::GrpcExecutor satisfies the Executor and Networking TS and Scheduler requirements and can therefore be used in places where Asio/libunifex expects an Executor or Scheduler.

The API for RPCs is modeled after the asynchronous, tag-based API of gRPC. As an example, the equivalent for grpc::ClientAsyncReader<helloworld::HelloReply>.Read(helloworld::HelloReply*, void*) would be agrpc::ClientRPC.read(helloworld::HelloReply&, CompletionToken).

Instead of the void* tag in the gRPC API the functions in this library expect a CompletionToken. Asio comes with several CompletionTokens already: C++20 coroutine, stackless coroutine, callback and Boost.Coroutine. There is also a special token called agrpc::use_sender that causes RPC functions to return a Sender.

If you are interested in learning more about the implementation details of this library then check out this blog article.

Examples of entire projects can be found in another repository.