Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are the grpc generated stubs thread safe?

Tags:

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?

like image 351
rajan sthapit Avatar asked Apr 05 '18 01:04

rajan sthapit


People also ask

Are gRPC calls 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.

Is gRPC stream thread safe?

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.

Is gRPC server multi threaded?

gRPC supports simultaneous requests on client-side and server-side, processing in different threads.

What are gRPC stubs?

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.


1 Answers

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.

like image 145
vardius Avatar answered Sep 22 '22 19:09

vardius