gRPC Server-side non-streaming request

Viewed 33

I have a gRPC service, and I would like to have a message initiated from the server to get order states from the client. I would like this server=>client request to be synchronous, and the client must initiate the service because of firewall constraints.

I do not see a way to accomplish this with gRPC messages, but I came up with two approaches that may work.

message OrderStates {
  repeated OrderState order_state = 1;
}

Option 1 - Non-streaming request + Streaming response

service < existing service > {
    rpc OrderStatuses(OrderStates) returns (stream google.protobuf.Empty);
}

With this approach, the client sends OrderStates when it starts up. Each time the server wants to get the current states from the client, it sends the streamed Empty response.

Option 2 - Streaming request + Streaming response

service < existing service > {
    rpc OrderStatuses(stream google.protobuf.Empty) returns (stream OrderStates);
}

This is the same as Option 1, but the client sends the initial request as a streaming request.

Any advice would be helpful.

1 Answers

Your approach is the way to accomplish this because you have a constraint that the server cannot act as a gRPC client and initiate a connection to the client acting as a gRPC server which would be the way to achieve this without your constraint.

Because of the constraint that the client must initiate the connection, the only solution is to hold the connection open (with a stream) so that the server may send messages to the client unbidden.

I would go with option #2 and the semantic of the RPC being "Hey server, ping me when you want OrderStates. You must use streaming on the client so that it can send updates.

An unstated optimization may be that, if the client remains alive but does not send an update in response to the server's ping within some timeframe, then the server assumes that there is no update.

Related