From eccb99aa9cb63b4227e122451aac1c2d8a9a4f7a Mon Sep 17 00:00:00 2001 From: Vitaly Date: Thu, 25 Feb 2021 12:12:11 -0800 Subject: [PATCH] Add keep alive to gRPC connection (#370) --- internal/grpc_dialer.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/internal/grpc_dialer.go b/internal/grpc_dialer.go index 2ea66d9ce..8b60ea53f 100644 --- a/internal/grpc_dialer.go +++ b/internal/grpc_dialer.go @@ -34,6 +34,7 @@ import ( "google.golang.org/grpc" "google.golang.org/grpc/backoff" "google.golang.org/grpc/credentials" + "google.golang.org/grpc/keepalive" "google.golang.org/grpc/metadata" "go.temporal.io/sdk/internal/common/metrics" @@ -79,11 +80,21 @@ func dial(params dialParameters) (*grpc.ClientConn, error) { cp.Backoff.BaseDelay = retryPollOperationInitialInterval cp.Backoff.MaxDelay = retryPollOperationMaxInterval + // gRPC utilizes keep alive mechanism to detect dead connections in case if server didn't close them + // gracefully. Client would ping the server periodically and expect replies withing the specified timeout. + // Learn more by reading https://github.com/grpc/grpc/blob/master/doc/keepalive.md + var kap = keepalive.ClientParameters{ + Time: 30 * time.Second, + Timeout: 15 * time.Second, + PermitWithoutStream: true, + } + return grpc.Dial(params.HostPort, grpcSecurityOptions, grpc.WithChainUnaryInterceptor(params.RequiredInterceptors...), grpc.WithDefaultServiceConfig(params.DefaultServiceConfig), grpc.WithConnectParams(cp), + grpc.WithKeepaliveParams(kap), ) }