Daniel
Daniel

Reputation: 631

How to keep grpc-js client connection open (alive) during inactive times?

I have a grpc server streaming RPC that communicates with the client. The client throws an error when it does not receive communication from the server. The error is:

Error: 13 INTERNAL: Received RST_STREAM with code 2
    at Object.callErrorFromStatus (/app/node_modules/@grpc/grpc-js/build/src/call.js:30:26)
    at Object.onReceiveStatus (/app/node_modules/@grpc/grpc-js/build/src/client.js:328:49)
    at Object.onReceiveStatus (/app/node_modules/@grpc/grpc-js/build/src/client-interceptors.js:304:181)
    at Http2CallStream.outputStatus (/app/node_modules/@grpc/grpc-js/build/src/call-stream.js:116:74)
    at Http2CallStream.maybeOutputStatus (/app/node_modules/@grpc/grpc-js/build/src/call-stream.js:155:22)
    at Http2CallStream.endCall (/app/node_modules/@grpc/grpc-js/build/src/call-stream.js:141:18)
    at ClientHttp2Stream.stream.on (/app/node_modules/@grpc/grpc-js/build/src/call-stream.js:410:22)
    at ClientHttp2Stream.emit (events.js:198:13)
    at emitCloseNT (internal/streams/destroy.js:68:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
Emitted 'error' event at:
    at Object.onReceiveStatus (/app/node_modules/@grpc/grpc-js/build/src/client.js:328:28)
    at Object.onReceiveStatus (/app/node_modules/@grpc/grpc-js/build/src/client-interceptors.js:304:181)
    [... lines matching original stack trace ...]
    at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
    at process._tickCallback (internal/process/next_tick.js:63:19)

I have tried searching for solutions and the only "solution" that kept the connection open was to manually ping the client every 10 seconds using setInterval. I read that grpc is supposed to keep the connections open for you and even reconnect if it is lost in this article.

As mentioned above, KeepAlive provides a valuable benefit: periodically checking the health of the connection by sending an HTTP/2 PING to determine whether the connection is still alive.

The way I set up the client is below

var grpc = require('@grpc/grpc-js');
require('dotenv').config({path:'/app/.env'});
var responderProto = grpc.loadPackageDefinition(packageDefinition).responder;
var client = new responderProto.ResponderService(process.env.GRPC_HOST_AND_PORT,
        grpc.credentials.createInsecure(),
        {
            "grpc.http2.max_pings_without_data" : 0,
            "grpc.keepalive_time_ms": 10000,
            "grpc.keepalive_permit_without_calls" : 1

        });

My understanding was that the "grpc.keepalive_time_ms" is supposed to ping the server every x ms

Am I doing something wrong or missing or misunderstanding something essential?

Upvotes: 0

Views: 2348

Answers (0)

Related Questions