I am a relative newbie to using gRPC. I am creating a microservice that need to run a long computation and return the results to the client via gRPC. I am trying to work out how I run the gRPC server in a thread or threads and the computation in another thread.
Usually you would want to have the server instance running and when a request comes in do some data retrieval or some computation and then formulate the results and return them to the requester. In many cases the operation performed in non-trivial and might take time to process. Ideally you don't want the server thread to block waiting for the operation to complete. The C++ code examples I have found are rather trivial and was looking for more guidance on how to correctly implement this scenario.
The controller would look something like:
void dummyFunction() {
while(true) {
// do something
}
}
void start() {
thread main_thread = thread{dummyFunction};
main_thread.join();
...
mainGRPCServer->start(target_str);
}
In the MainServer implementation I have used a Synchronous server as in the greeter example
void RunServer(string &server_address) {
NServiceImpl n_service;
SServiceImpl s_service;
grpc::EnableDefaultHealthCheckService(true);
grpc::reflection::InitProtoReflectionServerBuilderPlugin();
ServerBuilder builder;
// Listen on the given address without any authentication mechanism.
builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
// Register "service" as the instance through which we'll communicate with
// clients. In this case it corresponds to an *synchronous* service.
builder.RegisterService(&n_service);
builder.RegisterService(&s_service);
// Finally assemble the server.
std::unique_ptr<Server> server(builder.BuildAndStart());
// Wait for the server to shutdown. Note that some other thread must be
// responsible for shutting down the server for this call to ever return.
server->Wait();
}
void MainServerImpl::start(string & target_str) {
worker_ = thread( RunServer, ref(target_str));
worker_.join();
}
Obviously this implementation is not going to work as I understand the grpc Server itself has it's own threading model. I have looked at using an async server implementation. Can anyone guide me on how to structure this ?
UPDATE: I found this on the GoogleGroups:
*The C++ server has two threading models available: sync and async. Most users will want to use the sync model: the server will have an (internal) threadpool that manages multiplexing requests onto some number of threads (reusing threads between requests). The async model allows you to bring your own threading model, but is a little trickier to use - in that mode you request new calls when your server is ready for them, and block in completion queues while there is no work to do. By arranging when you block on the completion queues, and on which completion queues you make requests, you can arrange a wide variety of threading models.*
But I still can't seem to find any good examples of implementation
Best intro found so far is https://grpc.io/docs/languages/cpp/async/