Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GRPC: Client ID or connection information?

Tags:

grpc

Is there a way to get the connection information on RPC calls from server side? Or maybe something like unique client ID?

like image 876
Kibeom Kim Avatar asked Jan 11 '17 05:01

Kibeom Kim


People also ask

How to get the information from the client request on gRPC?

There are generally two types of information we can get from the client request on grpc server side. Method Information : We know that rpc call is simple method call . To get the method name (ie : which method will be invoked in grpc server when client will request?). below code will work.

How do I make a gRPC call?

A gRPC call is initiated by calling a method on the client. The gRPC client will handle message serialization and addressing the gRPC call to the correct service. gRPC has different types of methods. How the client is used to make a gRPC call depends on the type of method called.

How does gRPC work on server side?

On the server side, a service and a gRPC server are implemented based on protocol buffers. The requesting client generates a matching stub. If the stub is available, the client application calls the corresponding function ("Search for items in inventory") in the stub. The request is then sent to the server over the network.

What is the difference between gRPC and gRPC?

Grpc.Core and gRPC-Web are viable alternatives that work today. A gRPC call is initiated by calling a method on the client. The gRPC client will handle message serialization and addressing the gRPC call to the correct service. gRPC has different types of methods. How the client is used to make a gRPC call depends on the type of method called.


1 Answers

There is no connecton information which may help distinguish clients. One reason of this is proxies: different clients can have same IP and port (as I understand)

One possible solution is handshake protocol in app level. You can add rpc method "Connect" and send clientId as response from server. Afterthat you can attach custom headers (metadata) to your rpc calls.

Client side java code:

String clientId = getIdfromServer();
Metadata.Key<String> CLIENT_ID = Metadata.Key.of("client_id", ASCII_STRING_MARSHALLER);
Metadata fixedHeaders = new Metadata();
fixedHeaders.put(CLIENT_ID, clientId);
blockingStub = MetadataUtils.attachHeaders(blockingStub, fixedHeaders);

This C++ server side code shows how to handle such header on server:

::grpc::Status YourRPC(::grpc::ServerContext* context, const Your* request, YourResponse* response)
{
    const auto clientMetadata = context->client_metadata();
    auto it = clientMetadata.find("client_id");
    auto clientId = std::string(it->second.begin(), it->second.end());
}

I noticed that metadata key is case insensitive. Grpc converts keys to lowercase.

like image 198
Radik Kurbanov Avatar answered Sep 22 '22 16:09

Radik Kurbanov