Are channel/stubs in gRPC thread-safe
Asked Answered
G

1

26

When using gRPC from Java, can I cache stubs (clients) and call them in a multi-threaded environment or are the channels thread-safe and can be safely cached?

If there is a network outage, should I recreate the channel or it is smart enough to reconnect? I couldn't find relevant info on http://www.grpc.io/docs/

Thanks

Ganister answered 18/10, 2015 at 11:56 Comment(0)
A
35

Answer to first question:

Channels are thread safe; io.grpc.Channel is marked with @ThreadSafe annotation. Stubs are also thread-safe, which is why reconfiguration creates a new stub.

Answer to second question:

If there is a network outage, you don't need to recreate the channel. The channel will reconnect with exponential backoff, roughly as described by the connection backoff doc. Java does not 100% conform to that algorithm, because it doesn't increase connection timeouts in later retries. (Not to be confused with the exponential backoff, which is implemented.)

Astrea answered 19/10, 2015 at 16:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.