Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question]: Slowly receiveing client in long-lived streaming #94

Open
Marwin34 opened this issue Jan 9, 2024 · 1 comment
Open

[Question]: Slowly receiveing client in long-lived streaming #94

Marwin34 opened this issue Jan 9, 2024 · 1 comment

Comments

@Marwin34
Copy link

Marwin34 commented Jan 9, 2024

Hi Tradias,

Is there a way to detect a slowly receiving client in a long-lived streaming RPC in asio-grpc (or grpc itself)? I am working on a server application that sends data to clients using streaming RPCS, and I would like to detect slowly receiving clients and then drop the connection with them since it can lead to queuing up data on sockets, potentially resulting in slowing down the server or ooming it. Could such a mechanism be implemented with asio-grpc?

Thanks in advance.

@Tradias
Copy link
Owner

Tradias commented Jan 9, 2024

Asio-grpc just acts as a small wrapper around gRPC's API. In the case of a client-streaming RPC, for example, that would be grpc::ServerAsyncWriter::Write. If that call really completes slowly for slow clients (I haven't tested it, also note this set_write_through option), then you can use it to automatically drop the connection like so:

const auto slow_threshold = std::chrono::seconds(5);
agrpc::Alarm alarm{grpc_context};
auto [completion_order, alarm_ok, write_ok] =
    co_await asio::experimental::make_parallel_group(
        alarm.wait(std::chrono::system_clock::now() + slow_threshold, asio::deferred),
        rpc.write(response, asio::deferred))
        .async_wait(asio::experimental::wait_for_one{}, asio::use_awaitable);
if (!write_ok)
{
    // client dropped connection or was slow (alarm finished and caused cancellation of rpc with invokes grpc::ServerContext::TryCancel)
    co_return;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants