From 8b6ecf3a7ee846384c2888e56904260a61b666fe Mon Sep 17 00:00:00 2001 From: anitarua Date: Mon, 4 Mar 2024 16:08:30 -0800 Subject: [PATCH] fix: disable keepalive for control clients --- src/Momento.Sdk/Internal/GrpcManager.cs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/Momento.Sdk/Internal/GrpcManager.cs b/src/Momento.Sdk/Internal/GrpcManager.cs index da5ee315..c25e4aee 100644 --- a/src/Momento.Sdk/Internal/GrpcManager.cs +++ b/src/Momento.Sdk/Internal/GrpcManager.cs @@ -69,13 +69,24 @@ internal GrpcManager(IGrpcConfiguration grpcConfig, ILoggerFactory loggerFactory #if NET5_0_OR_GREATER if (SocketsHttpHandler.IsSupported) // see: https://github.com/grpc/grpc-dotnet/blob/098dca892a3949ade411c3f2f66003f7b330dfd2/src/Shared/HttpHandlerFactory.cs#L28-L30 { + var keepAliveWithoutCallsPolicy = grpcConfig.SocketsHttpHandlerOptions.KeepAlivePermitWithoutCalls == true ? System.Net.Http.HttpKeepAlivePingPolicy.Always : System.Net.Http.HttpKeepAlivePingPolicy.WithActiveRequests; + var keepAliveTimeout = grpcConfig.SocketsHttpHandlerOptions.KeepAlivePingTimeout; + var keepAlivePingDelay = grpcConfig.SocketsHttpHandlerOptions.KeepAlivePingDelay; + + // Disable keepalives for control clients and convert KeepAlivePermitWithoutCalls from bool to HttpKeepAlivePingPolicy + if (loggerName.Contains("Control")) { + keepAliveWithoutCallsPolicy = System.Net.Http.HttpKeepAlivePingPolicy.WithActiveRequests; + keepAliveTimeout = System.Threading.Timeout.InfiniteTimeSpan; + keepAlivePingDelay = System.Threading.Timeout.InfiniteTimeSpan; + } + channelOptions.HttpHandler = new SocketsHttpHandler { EnableMultipleHttp2Connections = grpcConfig.SocketsHttpHandlerOptions.EnableMultipleHttp2Connections, PooledConnectionIdleTimeout = grpcConfig.SocketsHttpHandlerOptions.PooledConnectionIdleTimeout, - KeepAlivePingTimeout = grpcConfig.SocketsHttpHandlerOptions.KeepAlivePingTimeout, - KeepAlivePingDelay = grpcConfig.SocketsHttpHandlerOptions.KeepAlivePingDelay, - KeepAlivePingPolicy = grpcConfig.SocketsHttpHandlerOptions.KeepAlivePermitWithoutCalls == true ? System.Net.Http.HttpKeepAlivePingPolicy.Always : System.Net.Http.HttpKeepAlivePingPolicy.WithActiveRequests, + KeepAlivePingTimeout = keepAliveTimeout, + KeepAlivePingDelay = keepAlivePingDelay, + KeepAlivePingPolicy = keepAliveWithoutCallsPolicy, }; } #elif USE_GRPC_WEB