diff --git a/src/Momento.Sdk/Config/Transport/IGrpcConfiguration.cs b/src/Momento.Sdk/Config/Transport/IGrpcConfiguration.cs index 31e4bf93..148a5afd 100644 --- a/src/Momento.Sdk/Config/Transport/IGrpcConfiguration.cs +++ b/src/Momento.Sdk/Config/Transport/IGrpcConfiguration.cs @@ -32,7 +32,7 @@ public interface IGrpcConfiguration /// interface for the channel options, which allows modifying specific values but does /// not allow the caller to use arbitrary strings to set the channel options.) /// - public GrpcChannelOptions? GrpcChannelOptions { get; } + public GrpcChannelOptions GrpcChannelOptions { get; } /// /// Override the SocketsHttpHandler's options. diff --git a/src/Momento.Sdk/Config/Transport/StaticTransportStrategy.cs b/src/Momento.Sdk/Config/Transport/StaticTransportStrategy.cs index f6cf23e4..39167b5e 100644 --- a/src/Momento.Sdk/Config/Transport/StaticTransportStrategy.cs +++ b/src/Momento.Sdk/Config/Transport/StaticTransportStrategy.cs @@ -44,10 +44,23 @@ public StaticGrpcConfiguration( Utils.ArgumentStrictlyPositive(deadline, nameof(deadline)); this.Deadline = deadline; this.MinNumGrpcChannels = minNumGrpcChannels; - this.GrpcChannelOptions = grpcChannelOptions; + this.GrpcChannelOptions = grpcChannelOptions ?? DefaultGrpcChannelOptions(); this.SocketsHttpHandlerOptions = socketsHttpHandlerOptions ?? new SocketsHttpHandlerOptions(); } + /// + /// The grpc default value for max_send_message_length is 4mb. This function returns default grpc options that increase max message size to 5mb in order to support cases where users have requested a limit increase up to our maximum item size of 5mb. + /// + /// GrpcChannelOptions + public static GrpcChannelOptions DefaultGrpcChannelOptions() { + const int DEFAULT_MAX_MESSAGE_SIZE = 5_243_000; + return new GrpcChannelOptions + { + MaxReceiveMessageSize = DEFAULT_MAX_MESSAGE_SIZE, + MaxSendMessageSize = DEFAULT_MAX_MESSAGE_SIZE + }; + } + /// public IGrpcConfiguration WithDeadline(TimeSpan deadline) { diff --git a/src/Momento.Sdk/Internal/GrpcManager.cs b/src/Momento.Sdk/Internal/GrpcManager.cs index b646c15a..da5ee315 100644 --- a/src/Momento.Sdk/Internal/GrpcManager.cs +++ b/src/Momento.Sdk/Internal/GrpcManager.cs @@ -42,11 +42,6 @@ public class GrpcManager : IDisposable protected CallInvoker invoker; - /// - /// The default value for max_send_message_length is 4mb. We need to increase this to 5mb in order to support cases where users have requested a limit increase up to our maximum item size of 5mb. - /// - protected const int DEFAULT_MAX_MESSAGE_SIZE = 5_243_000; - /// /// Constructor for GrpcManager, establishes the GRPC connection and creates the logger and invoker. /// @@ -57,14 +52,6 @@ public class GrpcManager : IDisposable /// internal GrpcManager(IGrpcConfiguration grpcConfig, ILoggerFactory loggerFactory, string authToken, string endpoint, string loggerName) { -#if USE_GRPC_WEB - // Note: all web SDK requests are routed to a `web.` subdomain to allow us flexibility on the server - endpoint = $"web.{endpoint}"; -#endif - var uri = $"https://{endpoint}"; - var channelOptions = this.GrpcChannelOptionsFromGrpcConfig(grpcConfig, loggerFactory); - this.channel = GrpcChannel.ForAddress(uri, channelOptions); - this.invoker = this.channel.CreateCallInvoker(); this._logger = loggerFactory.CreateLogger(loggerName); this.headerTuples = new List> @@ -74,20 +61,11 @@ internal GrpcManager(IGrpcConfiguration grpcConfig, ILoggerFactory loggerFactory new(Header.RuntimeVersionKey, runtimeVersion) }; this.headers = headerTuples.Select(tuple => new Header(name: tuple.Item1, value: tuple.Item2)).ToList(); - } - /// - /// Create a GrpcChannelOptions object from an IGrpcConfiguration object. - /// - /// The IGrpcConfiguration object specifying underlying grpc options - /// - /// - public GrpcChannelOptions GrpcChannelOptionsFromGrpcConfig(IGrpcConfiguration grpcConfig, ILoggerFactory loggerFactory) { - var channelOptions = grpcConfig.GrpcChannelOptions ?? new GrpcChannelOptions(); + // Set all channel opens and create the grpc connection + var channelOptions = grpcConfig.GrpcChannelOptions; channelOptions.Credentials = ChannelCredentials.SecureSsl; channelOptions.LoggerFactory ??= loggerFactory; - channelOptions.MaxReceiveMessageSize = grpcConfig.GrpcChannelOptions?.MaxReceiveMessageSize ?? DEFAULT_MAX_MESSAGE_SIZE; - channelOptions.MaxSendMessageSize = grpcConfig.GrpcChannelOptions?.MaxSendMessageSize ?? DEFAULT_MAX_MESSAGE_SIZE; #if NET5_0_OR_GREATER if (SocketsHttpHandler.IsSupported) // see: https://github.com/grpc/grpc-dotnet/blob/098dca892a3949ade411c3f2f66003f7b330dfd2/src/Shared/HttpHandlerFactory.cs#L28-L30 { @@ -102,8 +80,12 @@ public GrpcChannelOptions GrpcChannelOptionsFromGrpcConfig(IGrpcConfiguration gr } #elif USE_GRPC_WEB channelOptions.HttpHandler = new GrpcWebHandler(new HttpClientHandler()); + // Note: all web SDK requests are routed to a `web.` subdomain to allow us flexibility on the server + endpoint = $"web.{endpoint}"; #endif - return channelOptions; + var uri = $"https://{endpoint}"; + this.channel = GrpcChannel.ForAddress(uri, channelOptions); + this.invoker = this.channel.CreateCallInvoker(); } ///