Stream RPC - Stub Thread safe

428 views
Skip to first unread message

Rama Rao

unread,
Nov 12, 2017, 10:38:21 PM11/12/17
to grpc.io
All,
If service uses Streaming calls (client, server or both) - can we create single stub (and channel) and reuse it across multiple calls that happen in multiple threads? Java doc says "StreamObserver" is not thread safe. But just want to confirm what scenarios are not thread safe?

Thanks,
Rama

spenc...@google.com

unread,
Nov 14, 2017, 7:37:33 PM11/14/17
to grpc.io
The stub and channel are thread safe meaning many threads can use these objects to begin RPCs safely.

For streaming RPCs, you'll have to pass in a StreamObserver and get back a StreamObserver. They are not thread safe because they contain unsynchronized code and data. For example, there is a flag that is set true if "cancel" is called, and will throw an error if you ever send a message after calling "cancel".

As long as the StreamObservers do not interact across threads you are fine. If you do, you must add synchronization at the application level.
Reply all
Reply to author
Forward
0 new messages