Skip to content

Commit

Permalink
doc: Replace asio::detached with RethrowFirstArg in examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Tradias committed Dec 10, 2023
1 parent 50b73c5 commit d12d185
Show file tree
Hide file tree
Showing 14 changed files with 74 additions and 47 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ jobs:
run: cmake --build ${{ github.workspace }}/build-12 --config Release --parallel $(nproc)

- name: Clang 12 Configure CMake
run: cmake --preset default -B ${{ github.workspace }}/build-12 -DCMAKE_CXX_COMPILER=$(which clang++-12) -DDOXYGEN_EXECUTABLE=${{ runner.workspace }}/doxygen/bin/doxygen -DVCPKG_INSTALLED_DIR=${{ runner.workspace }}/vcpkg_installed -DCMAKE_DISABLE_PRECOMPILE_HEADERS=on -DASIO_GRPC_ENABLE_STDEXEC_TESTS=on ${{ env.CMAKE_EXTRA_ARGS }} ${{ env.CMAKE_ARGS }}
run: cmake --preset default -B ${{ github.workspace }}/build-12 -DCMAKE_CXX_COMPILER=$(which clang++-12) -DDOXYGEN_EXECUTABLE=${{ runner.workspace }}/doxygen/bin/doxygen -DVCPKG_INSTALLED_DIR=${{ runner.workspace }}/vcpkg_installed -DCMAKE_DISABLE_PRECOMPILE_HEADERS=on ${{ env.CMAKE_EXTRA_ARGS }} ${{ env.CMAKE_ARGS }}

- name: Clang 12 Build
run: cmake --build ${{ github.workspace }}/build-12 --config Release --parallel $(nproc) --target asio-grpc-check-header-syntax all
Expand Down
9 changes: 2 additions & 7 deletions example/file-transfer-client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "buffer.hpp"
#include "example/v1/example_ext.grpc.pb.h"
#include "helper.hpp"
#include "rethrow_first_arg.hpp"
#include "scope_guard.hpp"

#include <agrpc/asio_grpc.hpp>
Expand Down Expand Up @@ -176,13 +177,7 @@ int main(int argc, const char** argv)
abort_if_not(
co_await make_double_buffered_send_file_request(grpc_context, io_context, stub_ext, file_path));
},
[](auto&& ep)
{
if (ep)
{
std::rethrow_exception(ep);
}
});
example::RethrowFirstArg{});

std::thread io_context_thread{&run_io_context, std::ref(io_context)};
example::ScopeGuard on_exit{[&]
Expand Down
11 changes: 2 additions & 9 deletions example/file-transfer-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "example/v1/example.grpc.pb.h"
#include "example/v1/example_ext.grpc.pb.h"
#include "helper.hpp"
#include "rethrow_first_arg.hpp"
#include "scope_guard.hpp"
#include "server_shutdown_asio.hpp"

Expand Down Expand Up @@ -118,14 +119,6 @@ asio::awaitable<bool, agrpc::GrpcExecutor> handle_send_file_request(asio::io_con
co_return co_await rpc.finish({}, grpc::Status::OK, buffer1.bind_allocator(USE_AWAITABLE));
}

void rethrow_first_arg(const std::exception_ptr& ep)
{
if (ep)
{
std::rethrow_exception(ep);
}
}

void run_io_context(asio::io_context& io_context)
{
try
Expand Down Expand Up @@ -172,7 +165,7 @@ int main(int argc, const char** argv)
abort_if_not(co_await handle_send_file_request(io_context, rpc, file_path));
shutdown.shutdown();
},
&rethrow_first_arg);
example::RethrowFirstArg{});

std::thread io_context_thread{&run_io_context, std::ref(io_context)};
example::ScopeGuard on_exit{[&]
Expand Down
4 changes: 2 additions & 2 deletions example/generic-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,11 @@ int main(int argc, const char** argv)
server_shutdown.shutdown();
}
},
asio::detached);
example::RethrowFirstArg{});

asio::thread_pool thread_pool{1};
agrpc::register_yield_rpc_handler<agrpc::GenericServerRPC>(
grpc_context, service, GenericRequestHandler{grpc_context, thread_pool}, asio::detached);
grpc_context, service, GenericRequestHandler{grpc_context, thread_pool}, example::RethrowFirstArg{});

grpc_context.run();
}
3 changes: 2 additions & 1 deletion example/hello-world-client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "awaitable_client_rpc.hpp"
#include "helloworld/helloworld.grpc.pb.h"
#include "helper.hpp"
#include "rethrow_first_arg.hpp"

#include <agrpc/asio_grpc.hpp>
#include <boost/asio/co_spawn.hpp>
Expand Down Expand Up @@ -46,7 +47,7 @@ int main(int argc, const char** argv)
status = co_await RPC::request(grpc_context, stub, client_context, request, response);
std::cout << status.ok() << " response: " << response.message() << std::endl;
},
asio::detached);
example::RethrowFirstArg{});

grpc_context.run();

Expand Down
3 changes: 2 additions & 1 deletion example/hello-world-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include "awaitable_server_rpc.hpp"
#include "helloworld/helloworld.grpc.pb.h"
#include "rethrow_first_arg.hpp"

#include <agrpc/asio_grpc.hpp>
#include <boost/asio/co_spawn.hpp>
Expand Down Expand Up @@ -53,7 +54,7 @@ int main(int argc, const char** argv)
co_await rpc.finish(response, grpc::Status::OK);
server->Shutdown();
},
asio::detached);
example::RethrowFirstArg{});

grpc_context.run();
}
42 changes: 42 additions & 0 deletions example/helper/rethrow_first_arg.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2023 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.

#ifndef AGRPC_HELPER_RETHROW_FIRST_ARG_HPP
#define AGRPC_HELPER_RETHROW_FIRST_ARG_HPP

#include <exception>

namespace example
{
// Using this as the completion token to functions like asio::co_spawn ensures that exceptions thrown by the coroutine
// are rethrown from grpc_context.run().
struct RethrowFirstArg
{
template <class... T>
void operator()(std::exception_ptr ep, T&&...)
{
if (ep)
{
std::rethrow_exception(ep);
}
}

template <class... T>
void operator()(T&&...)
{
}
};
} // namespace example

#endif // AGRPC_HELPER_RETHROW_FIRST_ARG_HPP
19 changes: 4 additions & 15 deletions example/helper/yield_helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#ifndef AGRPC_HELPER_YIELD_HELPER_HPP
#define AGRPC_HELPER_YIELD_HELPER_HPP

#include "rethrow_first_arg.hpp"

#include <agrpc/grpc_context.hpp>
#include <boost/asio/bind_executor.hpp>
#include <boost/asio/compose.hpp>
Expand All @@ -30,7 +32,7 @@ template <class Executor, class Function>
void spawn(Executor&& executor, Function&& function)
{
#if (BOOST_VERSION >= 108000)
boost::asio::spawn(std::forward<Executor>(executor), std::forward<Function>(function), boost::asio::detached);
boost::asio::spawn(std::forward<Executor>(executor), std::forward<Function>(function), example::RethrowFirstArg{});
#else
boost::asio::spawn(std::forward<Executor>(executor), std::forward<Function>(function));
#endif
Expand All @@ -54,19 +56,6 @@ auto initiate_spawn(Executor&& executor, Function&& function, CompletionToken&&
#endif
}

inline void rethrow_exception_ptr(std::exception_ptr ep)
{
if (ep)
{
std::rethrow_exception(ep);
}
}

template <class T>
void rethrow_exception_ptr(T&&)
{
}

template <class... Function>
struct SpawnAllVoid
{
Expand Down Expand Up @@ -104,7 +93,7 @@ struct SpawnAllVoid
template <class Self, class... T>
void operator()(Self& self, T&&... t)
{
(example::rethrow_exception_ptr(t), ...);
(example::RethrowFirstArg{}(t), ...);
self.complete();
}
};
Expand Down
3 changes: 2 additions & 1 deletion example/multi-threaded-client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "awaitable_client_rpc.hpp"
#include "helloworld/helloworld.grpc.pb.h"
#include "helper.hpp"
#include "rethrow_first_arg.hpp"

#include <agrpc/asio_grpc.hpp>
#include <boost/asio/co_spawn.hpp>
Expand Down Expand Up @@ -106,7 +107,7 @@ int main(int argc, const char** argv)
for (size_t i{}; i < 20; ++i)
{
auto& grpc_context = round_robin_grpc_contexts.next()->context;
boost::asio::co_spawn(grpc_context, make_request(grpc_context, stub), boost::asio::detached);
asio::co_spawn(grpc_context, make_request(grpc_context, stub), example::RethrowFirstArg{});
}

for (auto& grpc_context : grpc_contexts)
Expand Down
3 changes: 2 additions & 1 deletion example/multi-threaded-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include "grpc/health/v1/health.grpc.pb.h"
#include "helloworld/helloworld.grpc.pb.h"
#include "rethrow_first_arg.hpp"
#include "server_shutdown_asio.hpp"

#include <agrpc/asio_grpc.hpp>
Expand Down Expand Up @@ -56,7 +57,7 @@ void register_request_handler(agrpc::GrpcContext& grpc_context, helloworld::Gree
}
});
},
asio::detached);
example::RethrowFirstArg{});
}

int main(int argc, const char** argv)
Expand Down
3 changes: 2 additions & 1 deletion example/share-io-context-client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "awaitable_client_rpc.hpp"
#include "example/v1/example.grpc.pb.h"
#include "helper.hpp"
#include "rethrow_first_arg.hpp"

#include <agrpc/asio_grpc.hpp>
#include <boost/asio/bind_executor.hpp>
Expand Down Expand Up @@ -85,7 +86,7 @@ int main(int argc, const char** argv)
co_await (make_grpc_request(grpc_context, stub) && make_tcp_request(tcp_port));
grpc_context_work_guard.reset();
},
asio::detached);
example::RethrowFirstArg{});
/* [co_spawn_io_context_and_grpc_context] */

/* [agrpc_run_io_context_shared_work_tracking] */
Expand Down
5 changes: 3 additions & 2 deletions example/share-io-context-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "awaitable_server_rpc.hpp"
#include "example/v1/example.grpc.pb.h"
#include "helper.hpp"
#include "rethrow_first_arg.hpp"
#include "server_shutdown_asio.hpp"

#include <agrpc/asio_grpc.hpp>
Expand Down Expand Up @@ -83,9 +84,9 @@ int main(int argc, const char** argv)
co_await rpc.finish(response, grpc::Status::OK);
server_shutdown.shutdown();
},
asio::detached);
example::RethrowFirstArg{});

asio::co_spawn(io_context, handle_tcp_request(tcp_port), asio::detached);
asio::co_spawn(io_context, handle_tcp_request(tcp_port), example::RethrowFirstArg{});

// First, initiate the io_context's thread_local variables by posting on it. The io_context uses them to optimize
// dynamic memory allocations. This is an optional step but it can improve performance.
Expand Down
3 changes: 2 additions & 1 deletion example/streaming-client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#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>
Expand Down Expand Up @@ -231,7 +232,7 @@ int main(int argc, const char** argv)
co_await make_and_cancel_unary_request(grpc_context, stub_ext);
co_await make_shutdown_request(grpc_context, stub_ext);
},
asio::detached);
example::RethrowFirstArg{});

grpc_context.run();
}
11 changes: 6 additions & 5 deletions example/streaming-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "example/v1/example.grpc.pb.h"
#include "example/v1/example_ext.grpc.pb.h"
#include "helper.hpp"
#include "rethrow_first_arg.hpp"
#include "server_shutdown_asio.hpp"

#include <agrpc/asio_grpc.hpp>
Expand Down Expand Up @@ -214,16 +215,16 @@ int main(int argc, const char** argv)
asio::thread_pool thread_pool{1};

agrpc::register_awaitable_rpc_handler<ClientStreamingRPC>(grpc_context, service, &handle_client_streaming_request,
asio::detached);
example::RethrowFirstArg{});

agrpc::register_awaitable_rpc_handler<ServerStreamingRPC>(grpc_context, service, &handle_server_streaming_request,
asio::detached);
example::RethrowFirstArg{});

agrpc::register_awaitable_rpc_handler<BidiStreamingRPC>(
grpc_context, service, bidirectional_streaming_rpc_handler(thread_pool), asio::detached);
grpc_context, service, bidirectional_streaming_rpc_handler(thread_pool), example::RethrowFirstArg{});

agrpc::register_awaitable_rpc_handler<SlowUnaryRPC>(grpc_context, service_ext, &handle_slow_unary_request,
asio::detached);
example::RethrowFirstArg{});

agrpc::register_awaitable_rpc_handler<ShutdownRPC>(
grpc_context, service_ext,
Expand All @@ -235,7 +236,7 @@ int main(int argc, const char** argv)
server_shutdown.shutdown();
}
},
asio::detached);
example::RethrowFirstArg{});

grpc_context.run();
std::cout << "Shutdown completed\n";
Expand Down

0 comments on commit d12d185

Please sign in to comment.