Skip to content

Commit

Permalink
doc: Shorten example README
Browse files Browse the repository at this point in the history
  • Loading branch information
Tradias committed Dec 10, 2024
1 parent 67f3545 commit 0f350e1
Show file tree
Hide file tree
Showing 20 changed files with 63 additions and 155 deletions.
68 changes: 5 additions & 63 deletions example/README.md

Large diffs are not rendered by default.

6 changes: 0 additions & 6 deletions example/async-generator-client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@ namespace asio = boost::asio;
using ExampleStub = example::v1::Example::Stub;
using ExampleExtStub = example::v1::ExampleExt::Stub;

// begin-snippet: client-side-server-streaming-async-generator
// ---------------------------------------------------
// (experimental) Client handling a server-streaming request using co_yield
// ---------------------------------------------------
// end-snippet

asio::awaitable<void> make_server_streaming_request(agrpc::GrpcContext& grpc_context, ExampleStub& stub)
{
using RPC = example::AwaitableClientRPC<&ExampleStub::PrepareAsyncServerStreaming>;
Expand Down
4 changes: 2 additions & 2 deletions example/async-generator-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ using ExampleService = example::v1::Example::AsyncService;
using ExampleExtService = example::v1::ExampleExt::AsyncService;

// begin-snippet: server-side-server-streaming-async-generator
// ---------------------------------------------------

// (experimental) Server handling a server-streaming request using co_yield
// ---------------------------------------------------

// end-snippet

template <class RPCHandler>
Expand Down
4 changes: 2 additions & 2 deletions example/file-transfer-client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ namespace asio = boost::asio;
inline constexpr asio::use_awaitable_t<agrpc::GrpcExecutor> USE_AWAITABLE{};

// begin-snippet: client-side-file-transfer
// ---------------------------------------------------

// Example showing how to transfer files over a streaming RPC. Stack buffers are used to customize memory allocation.
// ---------------------------------------------------

// end-snippet

// The use of `asio::awaitable<bool, agrpc::GrpcExecutor>` is not required but `agrpc::GrpcExecutor` is slightly smaller
Expand Down
4 changes: 2 additions & 2 deletions example/file-transfer-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@
namespace asio = boost::asio;

// begin-snippet: server-side-file-transfer
// ---------------------------------------------------

// Example showing how to transfer files over a streaming RPC. Stack buffers are used to customize memory allocation.
// ---------------------------------------------------

// end-snippet

using RPC = agrpc::ServerRPC<&example::v1::ExampleExt::AsyncService::RequestSendFile>;
Expand Down
8 changes: 4 additions & 4 deletions example/generic-client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ bool deserialize(grpc::ByteBuffer& buffer, Message& message)
}

// begin-snippet: client-side-generic-unary-request
// ---------------------------------------------------

// A simple generic unary with Boost.Coroutine.
// ---------------------------------------------------

// end-snippet
void make_generic_unary_request(agrpc::GrpcContext& grpc_context, grpc::GenericStub& stub,
const asio::yield_context& yield)
Expand Down Expand Up @@ -84,10 +84,10 @@ void make_generic_unary_request(agrpc::GrpcContext& grpc_context, grpc::GenericS
//

// begin-snippet: client-side-generic-bidirectional-request
// ---------------------------------------------------

// A generic bidirectional-streaming request that simply sends the response from the server back to it using Asio's
// stackless coroutines.
// ---------------------------------------------------

// end-snippet
struct BidirectionalStreamingRequest
{
Expand Down
28 changes: 4 additions & 24 deletions example/generic-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,10 @@

namespace asio = boost::asio;

// Examples showing how to write generic servers for unary and bidirectional streaming RPCs.

// begin-snippet: server-side-generic-unary-request
// ---------------------------------------------------

// Handle a simple generic unary request with Boost.Coroutine.
// ---------------------------------------------------

// end-snippet
void process_request(grpc::ByteBuffer& buffer)
{
Expand Down Expand Up @@ -74,10 +72,10 @@ void handle_generic_unary_request(agrpc::GenericServerRPC& rpc, const asio::yiel
//

// begin-snippet: server-side-generic-bidirectional-request
// ---------------------------------------------------

// A bidirectional-streaming example that shows how to dispatch requests to a thread_pool and write responses
// back to the client.
// ---------------------------------------------------

// end-snippet
using Channel = asio::experimental::channel<agrpc::GrpcExecutor, void(boost::system::error_code, grpc::ByteBuffer)>;

Expand All @@ -100,19 +98,6 @@ void reader(agrpc::GenericServerRPC& rpc, Channel& channel, const asio::basic_yi
channel.close();
}

// When switching threads in a Boost.Coroutine calls to `std::this_thread::get_id` before and after the switch can
// produce unexpected results. Disabling optimizations seems to correct that.
auto
#if defined(__clang__)
__attribute__((optnone))
#elif defined(__GNUC__)
__attribute__((optimize("O0")))
#endif
get_thread_id()
{
return std::this_thread::get_id();
}

// The writer will pick up reads from the reader through the channel and switch
// to the thread_pool to compute their response.
template <class Handler>
Expand All @@ -128,14 +113,9 @@ bool writer(agrpc::GenericServerRPC& rpc, Channel& channel, asio::thread_pool& t
{
break;
}
auto main_thread = std::this_thread::get_id();

// In this example we switch to the thread_pool to compute the response.
asio::post(asio::bind_executor(thread_pool, yield));

auto thread_pool_thread = get_thread_id();
abort_if_not(main_thread != thread_pool_thread);

process_request(buffer);

// rpc.write() is thread-safe so we can interact with it from the thread_pool.
Expand Down
4 changes: 2 additions & 2 deletions example/hello-world-server-arena.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
namespace asio = boost::asio;

// begin-snippet: server-side-helloworld-arena
// ---------------------------------------------------

// Server-side hello world with google::protobuf::Arena allocation
// ---------------------------------------------------

// end-snippet

class ArenaRequestMessageFactory
Expand Down
4 changes: 2 additions & 2 deletions example/hello-world-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
namespace asio = boost::asio;

// begin-snippet: server-side-helloworld
// ---------------------------------------------------

// Server-side hello world which handles exactly one request from the client before shutting down.
// ---------------------------------------------------

// end-snippet
int main(int argc, const char** argv)
{
Expand Down
4 changes: 2 additions & 2 deletions example/main-io-context-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
namespace asio = boost::asio;

// begin-snippet: server-side-main-io-context
// ---------------------------------------------------

// Example showing how to use an io_context as the main context and a GrpcContext on a separate thread for gRPC servers.
// ---------------------------------------------------

// end-snippet

// A simple tcp request that will be handled by the io_context.
Expand Down
4 changes: 2 additions & 2 deletions example/multi-threaded-alternative-client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
namespace asio = boost::asio;

// begin-snippet: client-side-multi-threaded-alternative
// ---------------------------------------------------

// Multi-threaded client using single a GrpcContext
// ---------------------------------------------------

// end-snippet

asio::awaitable<void> make_request(agrpc::GrpcContext& grpc_context, helloworld::Greeter::Stub& stub)
Expand Down
4 changes: 2 additions & 2 deletions example/multi-threaded-alternative-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
namespace asio = boost::asio;

// begin-snippet: server-side-multi-threaded-alternative
// ---------------------------------------------------

// Multi-threaded server handling unary requests using callback API and single GrpcContext
// ---------------------------------------------------

// end-snippet

void register_request_handler(agrpc::GrpcContext& grpc_context, helloworld::Greeter::AsyncService& service,
Expand Down
4 changes: 2 additions & 2 deletions example/multi-threaded-client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
namespace asio = boost::asio;

// begin-snippet: client-side-multi-threaded
// ---------------------------------------------------

// Multi-threaded client using multiple GrpcContexts
// ---------------------------------------------------

// end-snippet

// A simple round robin strategy for picking the next GrpcContext to use for an RPC.
Expand Down
4 changes: 2 additions & 2 deletions example/multi-threaded-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
namespace asio = boost::asio;

// begin-snippet: server-side-multi-threaded
// ---------------------------------------------------

// Multi-threaded server handling unary requests using callback API and multiple GrpcContexts
// ---------------------------------------------------

// end-snippet

void register_request_handler(agrpc::GrpcContext& grpc_context, helloworld::Greeter::AsyncService& service,
Expand Down
4 changes: 2 additions & 2 deletions example/share-io-context-client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
namespace asio = boost::asio;

// begin-snippet: client-side-share-io-context
// ---------------------------------------------------

// Example showing how to run an io_context and a GrpcContext on the same thread for gRPC clients.
// ---------------------------------------------------

// end-snippet

// A simple tcp request that will be handled by the io_context
Expand Down
4 changes: 2 additions & 2 deletions example/share-io-context-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@
namespace asio = boost::asio;

// begin-snippet: server-side-share-io-context
// ---------------------------------------------------

// Example showing how to run an io_context and a GrpcContext on the same thread for gRPC servers.
// This can i.e. be useful when writing an HTTP server that occasionally reaches out to a gRPC server. In that case
// creating a separate thread for the GrpcContext might be undesirable due to added synchronization complexity.
// ---------------------------------------------------

// end-snippet

// A simple tcp request that will be handled by the io_context.
Expand Down
20 changes: 8 additions & 12 deletions example/streaming-client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ using ExampleExtStub = example::v1::ExampleExt::Stub;
// Example showing some of the features of the ClientRPC API of asio-grpc with Boost.Asio.

// begin-snippet: client-side-client-rpc-streaming
// ---------------------------------------------------

// A simple client-streaming request with coroutines.
// ---------------------------------------------------

// end-snippet
asio::awaitable<void> make_client_streaming_request(agrpc::GrpcContext& grpc_context, ExampleStub& stub)
{
Expand Down Expand Up @@ -73,9 +73,9 @@ asio::awaitable<void> make_client_streaming_request(agrpc::GrpcContext& grpc_con
//

// begin-snippet: client-rpc-server-streaming
// ---------------------------------------------------

// A simple server-streaming request with coroutines.
// ---------------------------------------------------

// end-snippet
asio::awaitable<void> make_server_streaming_request(agrpc::GrpcContext& grpc_context, ExampleStub& stub)
{
Expand Down Expand Up @@ -103,9 +103,7 @@ asio::awaitable<void> make_server_streaming_request(agrpc::GrpcContext& grpc_con
// ---------------------------------------------------
//

// ---------------------------------------------------
// A server-streaming request that is cancelled.
// ---------------------------------------------------
asio::awaitable<void> make_server_streaming_notify_when_done_request(agrpc::GrpcContext& grpc_context,
ExampleStub& stub)
{
Expand Down Expand Up @@ -133,9 +131,9 @@ asio::awaitable<void> make_server_streaming_notify_when_done_request(agrpc::Grpc
//

// begin-snippet: client-rpc-bidirectional-streaming
// ---------------------------------------------------

// A bidirectional-streaming request that simply sends the response from the server back to it.
// ---------------------------------------------------

// end-snippet
asio::awaitable<void> make_bidirectional_streaming_request(agrpc::GrpcContext& grpc_context, ExampleStub& stub)
{
Expand Down Expand Up @@ -177,11 +175,11 @@ asio::awaitable<void> make_bidirectional_streaming_request(agrpc::GrpcContext& g
//

// begin-snippet: client-side-run-with-deadline
// ---------------------------------------------------

// A unary request with a per-RPC step timeout. Using a unary RPC for demonstration purposes, the same mechanism can be
// applied to streaming RPCs, where it is arguably more useful.
// For unary RPCs, `grpc::ClientContext::set_deadline` should be preferred.
// ---------------------------------------------------

// end-snippet
asio::awaitable<void> make_and_cancel_unary_request(agrpc::GrpcContext& grpc_context, ExampleExtStub& stub)
{
Expand Down Expand Up @@ -215,9 +213,7 @@ asio::awaitable<void> make_and_cancel_unary_request(agrpc::GrpcContext& grpc_con
// ---------------------------------------------------
//

// ---------------------------------------------------
// The Shutdown endpoint is used by unit tests.
// ---------------------------------------------------
asio::awaitable<void> make_shutdown_request(agrpc::GrpcContext& grpc_context, ExampleExtStub& stub)
{
using RPC = example::AwaitableClientRPC<&ExampleExtStub::PrepareAsyncShutdown>;
Expand Down
18 changes: 8 additions & 10 deletions example/streaming-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ using ExampleExtService = example::v1::ExampleExt::AsyncService;
// Example showing some of the features of using asio-grpc with Boost.Asio.

// begin-snippet: server-side-client-streaming
// ---------------------------------------------------

// A simple client-streaming rpc handler using C++20 coroutines.
// ---------------------------------------------------

// end-snippet
using ClientStreamingRPC = example::AwaitableServerRPC<&ExampleService::RequestClientStreaming>;

Expand Down Expand Up @@ -74,9 +74,9 @@ asio::awaitable<void> handle_client_streaming_request(ClientStreamingRPC& rpc)
//

// begin-snippet: server-side-server-streaming
// ---------------------------------------------------

// A simple server-streaming rpc handler using C++20 coroutines.
// ---------------------------------------------------

// end-snippet
using ServerStreamingRPC = example::AwaitableServerRPC<&ExampleService::RequestServerStreaming>;

Expand All @@ -94,10 +94,10 @@ asio::awaitable<void> handle_server_streaming_request(ServerStreamingRPC& rpc, e
//

// begin-snippet: server-side-notify-when-done
// ---------------------------------------------------

// A server-streaming rpc handler that sends a message every 30s but completes immediately if the client cancels the
// rpc.
// ---------------------------------------------------

// end-snippet
using ServerStreamingNotifyWhenDoneRPC =
example::AwaitableNotifyWhenDoneServerRPC<&ExampleService::RequestServerStreaming>;
Expand Down Expand Up @@ -144,10 +144,10 @@ auto server_streaming_notify_when_done_request_handler(agrpc::GrpcContext& grpc_
//

// begin-snippet: server-side-bidirectional-streaming
// ---------------------------------------------------

// The following bidirectional-streaming example shows how to dispatch requests to a thread_pool and write responses
// back to the client.
// ---------------------------------------------------

// end-snippet
using BidiStreamingRPC = example::AwaitableServerRPC<&ExampleService::RequestBidirectionalStreaming>;

Expand Down Expand Up @@ -224,10 +224,8 @@ auto bidirectional_streaming_rpc_handler(asio::thread_pool& thread_pool)
// ---------------------------------------------------
//

// ---------------------------------------------------
// The SlowUnary endpoint is used by the client to demonstrate per-RPC step cancellation. See streaming-client.cpp.
// It also demonstrates how to use an awaitable with a different executor type.
// ---------------------------------------------------
using SlowUnaryRPC =
asio::use_awaitable_t<agrpc::GrpcExecutor>::as_default_on_t<agrpc::ServerRPC<&ExampleExtService::RequestSlowUnary>>;

Expand Down
Loading

0 comments on commit 0f350e1

Please sign in to comment.