23

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

leeor
  • 17,041
  • 6
  • 34
  • 60
julius
  • 815
  • 2
  • 7
  • 12

1 Answers1

31

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.)

Eric Anderson
  • 24,057
  • 5
  • 55
  • 76