Skip to content

Commit

Permalink
feat: Remove asio::use_awaitable as default completion token from Cli…
Browse files Browse the repository at this point in the history
…entRPC
  • Loading branch information
Tradias committed Jul 25, 2023
1 parent dc7fcf9 commit c84d002
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 15 deletions.
11 changes: 6 additions & 5 deletions example/client-rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include "client_rpc.hpp"
#include "example/v1/example.grpc.pb.h"
#include "example/v1/example_ext.grpc.pb.h"
#include "helper.hpp"
Expand Down Expand Up @@ -40,7 +41,7 @@ namespace asio = boost::asio;
// end-snippet
asio::awaitable<void> make_client_streaming_request(agrpc::GrpcContext& grpc_context, example::v1::Example::Stub& stub)
{
using RPC = agrpc::ClientRPC<&example::v1::Example::Stub::PrepareAsyncClientStreaming>;
using RPC = example::AwaitableClientRPC<&example::v1::Example::Stub::PrepareAsyncClientStreaming>;

RPC rpc{grpc_context};
rpc.context().set_deadline(std::chrono::system_clock::now() + std::chrono::seconds(5));
Expand Down Expand Up @@ -74,7 +75,7 @@ asio::awaitable<void> make_client_streaming_request(agrpc::GrpcContext& grpc_con
// end-snippet
asio::awaitable<void> make_server_streaming_request(agrpc::GrpcContext& grpc_context, example::v1::Example::Stub& stub)
{
using RPC = agrpc::ClientRPC<&example::v1::Example::Stub::PrepareAsyncServerStreaming>;
using RPC = example::AwaitableClientRPC<&example::v1::Example::Stub::PrepareAsyncServerStreaming>;

RPC rpc{grpc_context};
rpc.context().set_deadline(std::chrono::system_clock::now() + std::chrono::seconds(5));
Expand Down Expand Up @@ -106,7 +107,7 @@ asio::awaitable<void> make_server_streaming_request(agrpc::GrpcContext& grpc_con
asio::awaitable<void> make_bidirectional_streaming_request(agrpc::GrpcContext& grpc_context,
example::v1::Example::Stub& stub)
{
using RPC = agrpc::ClientRPC<&example::v1::Example::Stub::PrepareAsyncBidirectionalStreaming>;
using RPC = example::AwaitableClientRPC<&example::v1::Example::Stub::PrepareAsyncBidirectionalStreaming>;

RPC rpc{grpc_context};
rpc.context().set_deadline(std::chrono::system_clock::now() + std::chrono::seconds(5));
Expand Down Expand Up @@ -153,7 +154,7 @@ asio::awaitable<void> make_bidirectional_streaming_request(agrpc::GrpcContext& g
asio::awaitable<void> make_and_cancel_unary_request(agrpc::GrpcContext& grpc_context,
example::v1::ExampleExt::Stub& stub)
{
using RPC = agrpc::ClientRPC<&example::v1::ExampleExt::Stub::PrepareAsyncSlowUnary>;
using RPC = example::AwaitableClientRPC<&example::v1::ExampleExt::Stub::PrepareAsyncSlowUnary>;

grpc::ClientContext client_context;
client_context.set_deadline(std::chrono::system_clock::now() + std::chrono::seconds(5));
Expand Down Expand Up @@ -193,7 +194,7 @@ asio::awaitable<void> make_shutdown_request(agrpc::GrpcContext& grpc_context, ex

google::protobuf::Empty response;
const grpc::Status status =
co_await agrpc::ClientRPC<&example::v1::ExampleExt::Stub::PrepareAsyncShutdown>::request(
co_await example::AwaitableClientRPC<&example::v1::ExampleExt::Stub::PrepareAsyncShutdown>::request(
grpc_context, stub, client_context, {}, response);

if (status.ok())
Expand Down
27 changes: 27 additions & 0 deletions example/helper/client_rpc.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2022 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_CLIENT_RPC_HPP
#define AGRPC_HELPER_CLIENT_RPC_HPP

#include <agrpc/client_rpc.hpp>
#include <boost/asio/use_awaitable.hpp>

namespace example
{
template <auto PrepareAsync>
using AwaitableClientRPC = boost::asio::use_awaitable_t<>::as_default_on_t<agrpc::ClientRPC<PrepareAsync>>;
}

#endif // AGRPC_HELPER_CLIENT_RPC_HPP
3 changes: 2 additions & 1 deletion example/multi-threaded-client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ asio::awaitable<void> make_request(agrpc::GrpcContext& grpc_context, helloworld:
RPC::Request request;
request.set_name("world");
RPC::Response response;
const auto status = co_await RPC::request(grpc_context, stub, client_context, request, response);
const auto status =
co_await RPC::request(grpc_context, stub, client_context, request, response, asio::use_awaitable);

abort_if_not(status.ok());
}
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 @@ -56,7 +56,8 @@ asio::awaitable<void> make_grpc_request(agrpc::GrpcContext& grpc_context, exampl
RPC::Request request;
request.set_integer(42);
RPC::Response response;
const auto status = co_await RPC::request(grpc_context, stub, client_context, request, response);
const auto status =
co_await RPC::request(grpc_context, stub, client_context, request, response, asio::use_awaitable);

abort_if_not(status.ok());
abort_if_not(42 == response.integer());
Expand Down
3 changes: 2 additions & 1 deletion example/snippets/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,8 @@ void client_main()
request.set_integer(42);
example::v1::Response response;
using RPC = agrpc::ClientRPC<&example::v1::Example::Stub::PrepareAsyncUnary>;
grpc::Status status = co_await RPC::request(grpc_context, stub, client_context, request, response);
grpc::Status status =
co_await RPC::request(grpc_context, stub, client_context, request, response, asio::use_awaitable);
assert(status.ok());
},
asio::detached);
Expand Down
8 changes: 4 additions & 4 deletions src/agrpc/alarm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ class BasicAlarm
* @param token A completion token like `asio::yield_context` or the one created by `agrpc::use_sender`. The
* completion signature is `void(bool)`. `true` if it expired, `false` if it was canceled.
*/
template <class Deadline, class CompletionToken = detail::DefaultCompletionTokenT<Executor>>
auto wait(const Deadline& deadline, CompletionToken token = detail::DefaultCompletionTokenT<Executor>{}) &
template <class Deadline, class CompletionToken = detail::LegacyDefaultCompletionTokenT<Executor>>
auto wait(const Deadline& deadline, CompletionToken token = detail::LegacyDefaultCompletionTokenT<Executor>{}) &
{
return detail::async_initiate_sender_implementation<
detail::GrpcSenderImplementation<detail::AlarmInitFunction<Deadline>, detail::AlarmCancellationFunction>>(
Expand All @@ -99,8 +99,8 @@ class BasicAlarm
* @param token A completion token like `asio::yield_context` or the one created by `agrpc::use_sender`. The
* completion signature is `void(BasicAlarm, bool)`. `true` if it expired, `false` if it was canceled.
*/
template <class Deadline, class CompletionToken = detail::DefaultCompletionTokenT<Executor>>
auto wait(const Deadline& deadline, CompletionToken token = detail::DefaultCompletionTokenT<Executor>{}) &&
template <class Deadline, class CompletionToken = detail::LegacyDefaultCompletionTokenT<Executor>>
auto wait(const Deadline& deadline, CompletionToken token = detail::LegacyDefaultCompletionTokenT<Executor>{}) &&
{
return detail::async_initiate_sender_implementation<detail::MoveAlarmSenderImplementation<Deadline, Executor>>(
grpc_context(), deadline, {static_cast<BasicAlarm&&>(*this)}, token);
Expand Down
12 changes: 10 additions & 2 deletions src/agrpc/detail/default_completion_token.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,20 @@ using DefaultCompletionToken = agrpc::UseSender;

#if defined(AGRPC_STANDALONE_ASIO) || defined(AGRPC_BOOST_ASIO)
template <class Executor>
using DefaultCompletionTokenT =
using DefaultCompletionTokenT = asio::default_completion_token_t<Executor>;
#else
template <class>
using DefaultCompletionTokenT = detail::DefaultCompletionToken;
#endif

#if defined(AGRPC_STANDALONE_ASIO) || defined(AGRPC_BOOST_ASIO)
template <class Executor>
using LegacyDefaultCompletionTokenT =
detail::ConditionalT<std::is_same_v<void, asio::default_completion_token_t<Executor>>,
detail::DefaultCompletionToken, asio::default_completion_token_t<Executor>>;
#else
template <class>
using DefaultCompletionTokenT = detail::DefaultCompletionToken;
using LegacyDefaultCompletionTokenT = detail::DefaultCompletionToken;
#endif
}

Expand Down
2 changes: 1 addition & 1 deletion test/src/test_asio_grpc_20.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ TEST_CASE_TEMPLATE("awaitable bidirectional streaming", Stub, test::v1::Test::St
}

#ifdef AGRPC_ASIO_HAS_CANCELLATION_SLOT
using RPC = agrpc::ClientRPC<&test::v1::Test::Stub::PrepareAsyncUnary>;
using RPC = asio::use_awaitable_t<>::as_default_on_t<agrpc::ClientRPC<&test::v1::Test::Stub::PrepareAsyncUnary>>;

TEST_CASE_FIXTURE(test::GrpcClientServerTest, "awaitable run_with_deadline no cancel")
{
Expand Down

0 comments on commit c84d002

Please sign in to comment.