I am taking a look at the tutorials of grpc https://grpc.io/docs/tutorials/basic/go.html
The grpc unary call looks something like this
conn, err := grpc.Dial(*serverAddr)
if err != nil {
...
}
defer conn.Close()
client := pb.NewRouteGuideClient(conn)
feature, err := client.GetFeature(context.Background(), &pb.Point{409146138, -746188906})
if err != nil {
...
}
I wanted to know if I call
client.GetFeature
from multiple threads, is it thread safe?
Multiple gRPC clients can be created from a channel, including different types of clients. A channel and clients created from the channel can safely be used by multiple threads. Clients created from the channel can make multiple simultaneous calls.
The gRPC streaming can be bi-directional and async. Is it thread safe? That depends on what the threads are doing - reads must be synchronized with respect to one another and writes must be synchronized with respect to one another, so an application cannot have multiple threads writing at the same time.
gRPC supports simultaneous requests on client-side and server-side, processing in different threads.
The generated class also contains stubs for use by gRPC clients to call methods defined by the service. Each stub wraps a Channel , supplied by the user of the generated code. The stub uses this channel to send RPCs to the service. gRPC Java generates code for three types of stubs: asynchronous, blocking, and future.
Looking into this issue you can learn that:
@rubenv asks:
Can I use a client from different threads in parallel?
@iamqizhao replies:
On client, if you want to perform multiple rpc in parallel, you should spawn multiple goroutines to do that since the rpc is synchronous/blocking
the answer is yes, however, a stream can't be shared (source).
@trevorgray, these kinds of concurrency topics are apparently still not documented, per #682.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With