How to poll a CompletionQueue when implementing a C++ grpc async client?

72 views
Skip to first unread message

stvanhe...@gmail.com

unread,
Apr 3, 2020, 6:48:00 AM4/3/20
to grpc.io

I am working on a model that sends multiple client rpc requests to multiple nodes. The thread that launches these requests runs on a loop of an asynchronous framework (libuv) that is also doing other things and as such cannot block to wait for the result.


To do so I use the asynchronous C++ client model to send multiple requests and bind them to a CompletionQueue. What is then needed is to check the CompletionQueue for any completions without ever blocking.


I know the CompletionQueue::AsyncNext method can be used to specify a deadline but what happens when an instant/past deadline is given is not documented. Would it effectively allow you to poll the CompletionQueue or would it instantly fail and return since the deadline was exceeded? If it's the second case, is there any other known method to poll the completion of requests?

m...@stackrox.com

unread,
Apr 3, 2020, 6:00:00 PM4/3/20
to grpc.io
Have you tried whether specifying a past deadline works? I also could not find anything in the documentation, but I read this part of the completion queue test as a very strong indication that `AsyncNext` with an "infinite past" deadline will indeed reliably return a timeout if and only if no event is pending and the queue is not shutdown.
Message has been deleted

stvanhe...@gmail.com

unread,
Apr 6, 2020, 11:37:20 AM4/6/20
to grpc.io
Hello, I did try out specifying a past deadline and it indeed allows you to check for completed events and returning instantly if there are none.

Christopher Warrington - MSFT

unread,
Apr 6, 2020, 5:17:03 PM4/6/20
to grpc.io
On Monday, April 6, 2020 at 8:37:20 AM UTC-7, stvanhe...@gmail.com wrote:

> I did try out specifying a past deadline and it indeed allows you
> to check for completed events and returning instantly if there are none.

One thing to be aware of: the completionq queue "borrows" the threads that
are calling Next()/AsyncNext() to perform gRPC's internal work (e.g.,
serializing, parsing HTTP, queuing network I/O). If you only every call
AsyncNext() with a short timeout, then gRPC won't be able to make forward
progress reliably, AFAIU.

If you need to interface with some other event loop, the current recommended
model is to have a few dedicate gRPC threads polling the completion queues
and then injecting the relevant results into your application's other event
loop.

For more details, see the similar thread "Injecting Events in Grpc
Completion Queue" [1].

[1]: https://groups.google.com/forum/#!topic/grpc-io/G7L-sFErbMc

--
Christopher Warrington
Microsoft Corp

Reply all
Reply to author
Forward
0 new messages