gogrpcgrpc-go

Increase gRPC timeout in Go


There are a few gRPC calls that require the connection to remain connected for more than 5 minutes, which will be idle till the operation is complete.
I tried keepalive settings and even context.WithTimeout() but the gRPC connection gets timed out ater 5 minutes (code = Unknown desc = stream timeout")

How can i increase the idle timeout for such gRPC calls?


Solution

  • Have you tried,

    clientDeadline := time.Now().Add(time.Duration(*deadlineMs) * time.Millisecond)
    ctx, cancel := context.WithDeadline(ctx, clientDeadline)
    

    like defined in official documentation of gRPC. Here is the https://grpc.io/blog/deadlines/ link.

    Edit:

    You can basically pass Enforcement Policy option to keep alive on the server-side as it is mentioned in the source code,

    // EnforcementPolicy is used to set keepalive enforcement policy on the
    // server-side. Server will close connection with a client that violates this
    // policy.
    type EnforcementPolicy struct {
        // MinTime is the minimum amount of time a client should wait before sending
        // a keepalive ping.
        MinTime time.Duration // The current default value is 5 minutes.
        // If true, server allows keepalive pings even when there are no active
        // streams(RPCs). If false, and client sends ping when there are no active
        // streams, server will send GOAWAY and close the connection.
        PermitWithoutStream bool // false by default.
    }