zazf
zazf

Reputation: 1

While a gRPC server reads from a stream, would it wait and let the server handle request from other clients?

From the offcial gRPC c++ examples: https://github.com/grpc/grpc/tree/v1.33.1/examples/cpp/route_guide

route_guide_server.cc:

  ...

  Status RouteChat(ServerContext* context,
                   ServerReaderWriter<RouteNote, RouteNote>* stream) override {
    RouteNote note;
    while (stream->Read(&note)) {
      std::unique_lock<std::mutex> lock(mu_);
      for (const RouteNote& n : received_notes_) {
        if (n.location().latitude() == note.location().latitude() &&
            n.location().longitude() == note.location().longitude()) {
          stream->Write(n);
        }
      }
      received_notes_.push_back(note);
    }

    return Status::OK;
  }

  ...

The server reads from the stream using a while loop. So, if the stream is still open and the server is waiting for messages from the client, would it switches to handle request from other clients since my server aims to communicate with multiple clients at the same time?

If not, what could be a better way to let the server wait for messages from a stream while being able to interact with other clients during the waiting?

Upvotes: 0

Views: 634

Answers (1)

Esun Kim
Esun Kim

Reputation: 219

In this case, you can use async gRPC server. doc

https://github.com/grpc/grpc/blob/master/test/cpp/qps/server_async.cc might be an example to see how to use it but please keep in mind that this is not comprehensible.

Upvotes: 0

Related Questions