Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ gRPC thread number configuration

Tags:

c++

grpc

Does gRPC server/client have any concept of thread pool for connections? That it is possible to reuse threads, pre-allocate threads, queue request on thread limit reached, etc.

If no, how does it work, is it just allocating/destroying a thread when it need, without any limitation and/or reuse? If yes, is it possible to configure it?

like image 973
Yuki Avatar asked Sep 12 '18 15:09

Yuki


People also ask

How many connections can gRPC handle?

Connection concurrency By default, most servers set this limit to 100 concurrent streams. A gRPC channel uses a single HTTP/2 connection, and concurrent calls are multiplexed on that connection. When the number of active calls reaches the connection stream limit, additional calls are queued in the client.

Is gRPC multithreaded?

gRPC Python wraps gRPC core, which uses multithreading for performance, and hence doesn't support fork() .

How can I make my gRPC faster?

There are two possible solutions: Create a separate channel for each area of high load in the application. Use a pool of gRPC channels to distribute RPCs over multiple connections (channels must have different channel args to prevent re-use so define a use-specific channel arg such as channel number).

Are gRPC stubs thread safe?

Channels provided by gRPC Python are thread-safe and that stubs provided by gRPC Python are thread-safe when used with thread-safe grpc. Channels #9320.


1 Answers

It depends whether you are using sync or async API. For a sync client, your RPC calls blocks the calling thread, so it is not really relevant. For a sync server, there is an internal threadpool handling all the incoming requests, you can use a grpc::ResourceQuota on a ServerBuilder to limit the max number of threads used by the threadpool.

For async client and server, gRPC uses CompletionQueue as a way for users to define their own threading model. A common way of building clients and servers is to use a user-provided threadpool to run CompletionQueue::Next in each thread. Then once it gets some tag from the Next call, you can cast it to a user-defined type and run some methods to proceed the state. In this case, the user has full control of threads being used.

Note gRPC does create some internal threads, but they should not be used for the majority of the rpc work.

like image 143
Yang Avatar answered Oct 14 '22 18:10

Yang