From 8674c80f44cc8e593ef87630ef7cdcbf33963e1d Mon Sep 17 00:00:00 2001 From: Michael Landis Date: Thu, 29 Feb 2024 11:25:21 -0800 Subject: [PATCH 1/3] chore: add guard to only use socketshttphanlder if supported In some runtimes, even though `SocketsHttpHandler` is included in the runtime it is not supported. Thus we add guards to test for this and let the gRPC client fall back to a supported one. --- src/Momento.Sdk/Internal/ControlGrpcManager.cs | 15 ++++++++++----- src/Momento.Sdk/Internal/DataGrpcManager.cs | 11 +++++++---- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/Momento.Sdk/Internal/ControlGrpcManager.cs b/src/Momento.Sdk/Internal/ControlGrpcManager.cs index e357ba39..aac7c858 100644 --- a/src/Momento.Sdk/Internal/ControlGrpcManager.cs +++ b/src/Momento.Sdk/Internal/ControlGrpcManager.cs @@ -95,21 +95,26 @@ public ControlGrpcManager(IConfiguration config, string authToken, string endpoi endpoint = $"web.{endpoint}"; #endif var uri = $"https://{endpoint}"; - this.channel = GrpcChannel.ForAddress(uri, new GrpcChannelOptions() + + var channelOptions = new GrpcChannelOptions() { Credentials = ChannelCredentials.SecureSsl, MaxReceiveMessageSize = Internal.Utils.DEFAULT_MAX_MESSAGE_SIZE, MaxSendMessageSize = Internal.Utils.DEFAULT_MAX_MESSAGE_SIZE, + }; #if NET5_0_OR_GREATER - HttpHandler = new System.Net.Http.SocketsHttpHandler + if (SocketsHttpHandler.IsSupported) + { + channelOptions.HttpHandler = new SocketsHttpHandler { EnableMultipleHttp2Connections = config.TransportStrategy.GrpcConfig.SocketsHttpHandlerOptions.EnableMultipleHttp2Connections, PooledConnectionIdleTimeout = config.TransportStrategy.GrpcConfig.SocketsHttpHandlerOptions.PooledConnectionIdleTimeout - } + }; + } #elif USE_GRPC_WEB - HttpHandler = new GrpcWebHandler(new HttpClientHandler()) + channelOptions.HttpHandler = new GrpcWebHandler(new HttpClientHandler()); #endif - }); + this.channel = GrpcChannel.ForAddress(uri, channelOptions); List
headers = new List
{ new Header(name: Header.AuthorizationKey, value: authToken), new Header(name: Header.AgentKey, value: version), new Header(name: Header.RuntimeVersionKey, value: runtimeVersion) }; CallInvoker invoker = this.channel.CreateCallInvoker(); diff --git a/src/Momento.Sdk/Internal/DataGrpcManager.cs b/src/Momento.Sdk/Internal/DataGrpcManager.cs index 09960dc0..6d9b0a5f 100644 --- a/src/Momento.Sdk/Internal/DataGrpcManager.cs +++ b/src/Momento.Sdk/Internal/DataGrpcManager.cs @@ -285,11 +285,14 @@ internal DataGrpcManager(IConfiguration config, string authToken, string endpoin channelOptions.MaxSendMessageSize = Internal.Utils.DEFAULT_MAX_MESSAGE_SIZE; #if NET5_0_OR_GREATER - channelOptions.HttpHandler = new SocketsHttpHandler + if (SocketsHttpHandler.IsSupported) { - EnableMultipleHttp2Connections = config.TransportStrategy.GrpcConfig.SocketsHttpHandlerOptions.EnableMultipleHttp2Connections, - PooledConnectionIdleTimeout = config.TransportStrategy.GrpcConfig.SocketsHttpHandlerOptions.PooledConnectionIdleTimeout - }; + channelOptions.HttpHandler = new SocketsHttpHandler + { + EnableMultipleHttp2Connections = config.TransportStrategy.GrpcConfig.SocketsHttpHandlerOptions.EnableMultipleHttp2Connections, + PooledConnectionIdleTimeout = config.TransportStrategy.GrpcConfig.SocketsHttpHandlerOptions.PooledConnectionIdleTimeout + }; + } #elif USE_GRPC_WEB channelOptions.HttpHandler = new GrpcWebHandler(new HttpClientHandler()); #endif From 34bb870d74381333c56ae72d54d7267058b62334 Mon Sep 17 00:00:00 2001 From: Michael Landis Date: Thu, 29 Feb 2024 11:27:55 -0800 Subject: [PATCH 2/3] chore: document is supported guard --- src/Momento.Sdk/Internal/ControlGrpcManager.cs | 3 ++- src/Momento.Sdk/Internal/DataGrpcManager.cs | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Momento.Sdk/Internal/ControlGrpcManager.cs b/src/Momento.Sdk/Internal/ControlGrpcManager.cs index aac7c858..45ab435b 100644 --- a/src/Momento.Sdk/Internal/ControlGrpcManager.cs +++ b/src/Momento.Sdk/Internal/ControlGrpcManager.cs @@ -103,7 +103,8 @@ public ControlGrpcManager(IConfiguration config, string authToken, string endpoi MaxSendMessageSize = Internal.Utils.DEFAULT_MAX_MESSAGE_SIZE, }; #if NET5_0_OR_GREATER - if (SocketsHttpHandler.IsSupported) + + if (SocketsHttpHandler.IsSupported) // see: https://github.com/grpc/grpc-dotnet/blob/098dca892a3949ade411c3f2f66003f7b330dfd2/src/Shared/HttpHandlerFactory.cs#L28-L30 { channelOptions.HttpHandler = new SocketsHttpHandler { diff --git a/src/Momento.Sdk/Internal/DataGrpcManager.cs b/src/Momento.Sdk/Internal/DataGrpcManager.cs index 6d9b0a5f..17b308c5 100644 --- a/src/Momento.Sdk/Internal/DataGrpcManager.cs +++ b/src/Momento.Sdk/Internal/DataGrpcManager.cs @@ -285,7 +285,7 @@ internal DataGrpcManager(IConfiguration config, string authToken, string endpoin channelOptions.MaxSendMessageSize = Internal.Utils.DEFAULT_MAX_MESSAGE_SIZE; #if NET5_0_OR_GREATER - if (SocketsHttpHandler.IsSupported) + if (SocketsHttpHandler.IsSupported) // see: https://github.com/grpc/grpc-dotnet/blob/098dca892a3949ade411c3f2f66003f7b330dfd2/src/Shared/HttpHandlerFactory.cs#L28-L30 { channelOptions.HttpHandler = new SocketsHttpHandler { From afea9c8eb6b245f9aa810017deba9ac52148a84b Mon Sep 17 00:00:00 2001 From: Michael Landis Date: Thu, 29 Feb 2024 11:30:04 -0800 Subject: [PATCH 3/3] chore: style nit --- src/Momento.Sdk/Internal/ControlGrpcManager.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Momento.Sdk/Internal/ControlGrpcManager.cs b/src/Momento.Sdk/Internal/ControlGrpcManager.cs index 45ab435b..4284a9bc 100644 --- a/src/Momento.Sdk/Internal/ControlGrpcManager.cs +++ b/src/Momento.Sdk/Internal/ControlGrpcManager.cs @@ -96,7 +96,7 @@ public ControlGrpcManager(IConfiguration config, string authToken, string endpoi #endif var uri = $"https://{endpoint}"; - var channelOptions = new GrpcChannelOptions() + var channelOptions = new GrpcChannelOptions { Credentials = ChannelCredentials.SecureSsl, MaxReceiveMessageSize = Internal.Utils.DEFAULT_MAX_MESSAGE_SIZE,