From 5237de7eb97195d5521320f7e872b769a767aa0e Mon Sep 17 00:00:00 2001 From: Vitaly Date: Tue, 23 Feb 2021 22:34:26 -0800 Subject: [PATCH] Add keep alive to gRPC connection --- internal/grpc_dialer.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/internal/grpc_dialer.go b/internal/grpc_dialer.go index bf23eb7d5..1674d2250 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" "go.temporal.io/sdk/internal/common/metrics" ) @@ -78,11 +79,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), ) }