'gRPC / C++- How to leave server streaming rpc on
Hello I have this code:
Status ListFeatures(ServerContext* context, const Rectangle* rectangle,
ServerWriter<Feature>* writer) override {
auto lo = rectangle->lo();
auto hi = rectangle->hi();
long left = std::min(lo.longitude(), hi.longitude());
long right = std::max(lo.longitude(), hi.longitude());
long top = std::max(lo.latitude(), hi.latitude());
long bottom = std::min(lo.latitude(), hi.latitude());
for (const Feature& f : feature_list_) {
if (f.location().longitude() >= left &&
f.location().longitude() <= right &&
f.location().latitude() >= bottom &&
f.location().latitude() <= top) {
writer->Write(f);
}
}
return Status::OK;
}
This is server streaming RPC on Client Unary call.
I would like to not close the stream. Once the Client initiates the unary call I would like to keep the server stream "forever" so I can send messages whenever I like.
As far as I understand at the moment this line is executed:
return Status::OK;
The stream is getting closed. Is there any way I can keep it open so later I can send more server streaming messages?
Solution 1:[1]
There's two major solutions here for you.
First, the API is designed to be threadsafe, and usable through threadpools. It's okay to never return, consuming a thread in the process, and continue writing endlessly. Obviously, this means keeping a thread up for sending out your responses continuously, which can be resource-intensive, depending on your situation. Also you'd need to properly set up your threadpool for this. Holding the response indefinitely by default will cause problems if you haven't tuned it beforehand.
But secondly, there's the Reactor mechanism, allowing you to return without damaging the state. Your callbacks have void
returns, and you'd need to explicitly call Finish(Status::OK)
to terminate the RPC. It's a different base API, so you'd need to convert your code to it. You can see an example here:
https://github.com/grpc/grpc/blob/master/examples/cpp/route_guide/route_guide_callback_server.cc
The Chatter call is an example of a streaming server API that hops threads to send its replies.
The details of the callback API can be found here: https://github.com/grpc/proposal/blob/master/L67-cpp-callback-api.md
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Nicolas Noble |