Hi,
The current node.js module doesn't allow one to pass a Channel object to build a client, but it does allow one to access a channel via grpc.getClientChannel(…) and .$channel on a client object.
In lieu of an exposed interface to pass a connection or channel, I tried out the following hack to allow two clients to share the same channel.
function combineChannels(aClient, bClient) {
var aChan = grpc.getClientChannel(aClient);
var bChan = grpc.getClientChannel(bClient);
if (aChan.getTarget() != bChan.getTarget()) {
throw "different targets: " + " (a) " + aChan.getTarget() + "; (b) " + bChan.getTarget();
}
if (aChan.getConnectivityState() != 0) {
throw "bad connectivity state (a): " + aChan.getConnectivityState();
}
if (bChan.getConnectivityState() != 0) {
throw "bad connectivity state (b): " + bChan.getConnectivityState();
}
bChan.close();
bClient.$channel = aChan;
}
Observing with nettop (macOS) shows only 1 connection when hack is in use, and 1 per client otherwise. It seems to work as expected, but is any danger in doing so? Is there a better way to accomplish this in node.js?
FWIW my goal is to have a healthcheck client keep the TCP socket alive in case a LB or FW wants to close idle-long-running connections.
Thanks in advance,
– Franco