gRPC streaming service

157 views
Skip to first unread message

song...@hotmail.com

unread,
Jan 3, 2017, 1:27:41 PM1/3/17
to grpc.io
Hello,

I am very interested in the gRPC bi-directional streaming feature. I have a few questions on the server side. In the .proto file, I define a simple streaming RPC and message below

service MfMessaging {
  rpc MfRpc (stream MfStream) returns (stream MfStream) {}
}
message MfStream {
  bytes message=1;
}

My first question is: what is the thread model at server side? I mean how gRPC handles if there are multiple concurrent requests from different clients? Does gRPC spawn a thread for each client? If yes, is there any limit or gRPC has a thread pool underneath?

My second question is: In the service routine, I get a ServerReaderWriter stream (e.g. ServerReaderWriter<MfStream, MfStream>* stream). What is lifetime of this stream? Can I pass this stream to another thread which writes response back later?

Look forward to your reply. Thank you very much!

Carl Mastrangelo

unread,
Jan 3, 2017, 7:35:42 PM1/3/17
to grpc.io, song...@hotmail.com
Each stream does not require a separate thread; gRPC multiplexes these onto your threads.  It is slightly different for each language, but in general threading isn't that much of a problem.


As for your second question, can you mention what language you are using?  That will affect the answer.

song...@hotmail.com

unread,
Jan 3, 2017, 7:56:02 PM1/3/17
to grpc.io, song...@hotmail.com
Thanks for your reply.

I am using c++.

For my first question. Can you please explain a bit more? I tested with two concurrent client requests. I saw two threads running in my service routine. My server code does not spawn any thread. So I think gRPC spawns the thread for me. That is why I want to understand the thread model. Is it one thread per one client? or there is a thread pool for all clients?

Thank you!

Carl Mastrangelo於 2017年1月3日星期二 UTC-8下午4時35分42秒寫道:

Carl Mastrangelo

unread,
Jan 4, 2017, 2:17:58 PM1/4/17
to grpc.io, song...@hotmail.com, Vijay Pai
From my understanding of the C-core and C++ wrapper for gRPC, it does not create threads; it borrows.  You must provide your own threads to handle streaming messages.  You can wait on multiple actions within a single thread.

song...@hotmail.com

unread,
Jan 4, 2017, 3:09:29 PM1/4/17
to grpc.io, song...@hotmail.com, vp...@google.com
Interesting. What do you mean "it borrows"? gRPC protobuf compiler automatically generates the service routine/function for me. I just fill in my implementation/logic there. When a client request comes in this routine gets triggered. My code does not spawn any thread to call this service routine. It should be some gRPC low level IO receiver/read thread that calls into my service routine. Can you please check?

btw, can you please kindly answer my second question too?


Carl Mastrangelo於 2017年1月4日星期三 UTC-8上午11時17分58秒寫道:
Reply all
Reply to author
Forward
0 new messages