Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

does grpc service must have exactly one input parameter and one return value

Tags:

let's say i have a proto file like this. can I define service like this

rpc SayHello () returns (Response) {} //service has no input rpc SayHello (Request1,Request2) returns (Response) {}//service has two inputs 

//.proto file

syntax = "proto3";  service Greeter{     rpc SayHello (Request) returns (Response) {} }   message Request{     string request = 1; }  message Response{     string response = 1; } 
like image 657
lcm Avatar asked Jul 26 '17 03:07

lcm


People also ask

Can gRPC server handle multiple clients?

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.

How does gRPC works internally?

The gRPC infrastructure decodes incoming requests, executes service methods, and encodes service responses. On the client side, the client has a local object known as stub (for some languages, the preferred term is client) that implements the same methods as the service.

Is gRPC full duplex?

gRPC supports duplex communications by means of streams. We can mark either a request or a response as a stream and can write/read from the stream until it is closed. As you can see, we've marked the response (the one inside the returns) as a stream, so we can expect continuous data for a single request.


1 Answers

gRPC service methods have exactly one input message and exactly one output message. Typically, these messages are used as input and output to only one method. This is on purpose, as it allows easily adding new parameters later (to the messages) while maintaining backward compatibility.

If you don't want any input or output parameters, you can use the well-known proto google.protobuf.Empty. However, this is discouraged as it prevents you from adding parameters to the method in the future. Instead, you would be encouraged to follow the normal practice of having a message for the request, but simply with no contents:

service Greeter {     rpc SayHello (SayHelloRequest) returns (SayHelloResponse) {} }  message SayHelloRequest {} // service has no input 

Similarly, if you want two request parameters, just include both in the request message:

message SayHelloRequest { // service has two inputs     string request = 1;     string anotherRequestParam = 2; } 
like image 141
Eric Anderson Avatar answered Sep 28 '22 02:09

Eric Anderson