Coder Social home page Coder Social logo

Comments (5)

eduardokussler avatar eduardokussler commented on May 17, 2024 1

This was indeed the problem. Thank you! Keep up the good work with the library!

from asio-grpc.

Tradias avatar Tradias commented on May 17, 2024

Hi, thank you for reporting the issue. I have trouble reproducing it. This email conversion in gRPC suggests that you might be doing two asynchronous operations in parallel that are not allowed to be in parallel. E.g. writing and finishing a writer at the same time.

I do not know what you mean by handle one request at a time, since you are using agrpc::repeatedly_request there will be one coroutine for each request from the client. In other words, the following code will co_spawn a coroutine for each client request, all those coroutines will be processed in parallel:

asio::awaitable<void> handle_request(grpc::ServerContext& server_context, auto& request, auto& writer, agrpc::GrpcContext& grpc_context)
{
  // This function will be called for each request from the client as soon as the request has been made, even while 
  // a previous request is still being processed by `handle_request`.
  // It is executing on a thread of the system_context.
  co_return;
}

void register_handler(asio::system_context& ctx, agrpc::GrpcContext& grpc_context, MarketDataAlert::AsyncService& service)
{
    agrpc::repeatedly_request(&MarketDataAlert::AsyncService::Requestsubscribe, service,
                              asio::bind_executor(grpc_context,
                                                  [&]<class T>(agrpc::RepeatedlyRequestContext<T>&& context)
                                                  {
                                                      
                                                      asio::co_spawn(
                                                              ctx,
                                                              [&, context = std::move(context)]()
                                                              {
                                                                  auto&& [server_context, request, responder] = context.args();
                                                                  return std::invoke(handle_request, server_context, request, responder, grpc_context);
                                                              },
                                                              asio::detached);
                                                  }));
}

from asio-grpc.

eduardokussler avatar eduardokussler commented on May 17, 2024

Thank you for the very quick response! I'll take a look on the link you have sent. What I meant by "handle one request at a time", is that the first request is processed and receives it's messages but the following requests all receive a unable to connect to server, which I assume is because it is occupied by the first request, which is on the while(request_ok) loop. In other words, i can only have one stream active at a time. It is also happening with the code you provided.
This is the error received/generated by the client application: Message: failed to connect to all addresses subscribe rpc failed.

In other words, the following code will co_spawn a coroutine for each client request, all those coroutines will be processed in parallel

I thought that this would happen too, but apparently is not. I can only start another rpc call when the previous one finishes. Could this be a performance issue? I mean by this is: could my system be too slow to co_spawn and process the new request while the other one is running?

from asio-grpc.

Tradias avatar Tradias commented on May 17, 2024

I suspect that something is blocking the GrpcContext. E.g. in your example:

while(request_ok) {
		// If `fill_response` performs blocking operations, e.g. holding a lock on a mutex or calling some long
		// CPU-intensive task then the entire GrpcContext will be blocked, because `agrpc::write` resumes
		// in the thread of the GrpcContext. If that is the case then explicitly switch back to the system_context
		// before doing such tasks.
		co_await asio::post(asio::bind_executor(ctx, asio::use_awaitable));

		co_await fill_response(server_context, request, response, instrumentId);
 
		// do some work
                request_ok = co_await agrpc::write(writer, response,
                                                              boost::asio::bind_executor(grpc_context, boost::asio::use_awaitable));
		
                    
    }
    //after done work
        bool finish_ok = co_await agrpc::finish(writer, grpc::Status::OK, boost::asio::bind_executor(grpc_context, boost::asio::use_awaitable));

I have just adjusted one of the unit test to deliberately verify that repeatedly_request can handle multiple requests at the same time which succeeds on all tested platforms/compilers.

from asio-grpc.

Tradias avatar Tradias commented on May 17, 2024

Great, I am glad we figured it out and thank you for the kind words.

from asio-grpc.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.