grpc number of active clients/channels

5.7k views Asked by At

I am implementing a simple grpc service discovery. One of the thing I would like to do is to keep track of how many clients are currently to the service and the service will report that to the service registry. Does grpc server api provide such information? I have come across a somewhat similar question here. https://github.com/grpc/grpc-java/issues/779.

In point 1 of the response, it was mentioned to keep track of all incoming rpc via streaming api. How exactly do I do that?

Here's an example rpc I want to implement, RegisterInstance will act as a "pinger" and server will use etcd's ttl to check liveness of service. And the registry will use the connected_client to determine which service ip to send back to client when there are more than one of the same service type registered on the registry. My issue is how can I obtain connected_clients from the service side?

syntax = "proto3";

package registry_grpc;

service ServiceRegistry{
    rpc GetInstance(ServiceRequest) returns(ServiceInstance) {}
    rpc RegisterInstance(ServiceInstance) returns(ServiceInstance) {}
}


message ServiceRequest{
    string service_type = 1;
}

message ServiceInstance{
    int32 id = 1;
    int32 connected_clients = 2;
    string service_type = 3;
    string host_address = 4;
    int32 port = 5;
}

edit

I have found that it's possible to intercept every incoming rpc calls. This way I can report for example number of incoming connections in the last second. I guess it can serve as a proxy for workload.

1

There are 1 answers

2
Kun  Zhang On

ServerBuilder has a addTransportFilter(), which allows you to install a ServerTransportFilter, where you get notifications when a connection (i.e. transport) is created and terminated.