Skip to content

Commit

Permalink
create default grpc options object if none provided to transport stra…
Browse files Browse the repository at this point in the history
…tegy
  • Loading branch information
anitarua committed Mar 2, 2024
1 parent 485fe7c commit 2d6d921
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/Momento.Sdk/Config/Transport/IGrpcConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.)
/// </summary>
public GrpcChannelOptions? GrpcChannelOptions { get; }
public GrpcChannelOptions GrpcChannelOptions { get; }

/// <summary>
/// Override the SocketsHttpHandler's options.
Expand Down
15 changes: 14 additions & 1 deletion src/Momento.Sdk/Config/Transport/StaticTransportStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/// <summary>
/// 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.
/// </summary>
/// <returns>GrpcChannelOptions</returns>
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
};
}

/// <inheritdoc/>
public IGrpcConfiguration WithDeadline(TimeSpan deadline)
{
Expand Down
32 changes: 7 additions & 25 deletions src/Momento.Sdk/Internal/GrpcManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,6 @@ public class GrpcManager : IDisposable

protected CallInvoker invoker;

/// <summary>
/// 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.
/// </summary>
protected const int DEFAULT_MAX_MESSAGE_SIZE = 5_243_000;

/// <summary>
/// Constructor for GrpcManager, establishes the GRPC connection and creates the logger and invoker.
/// </summary>
Expand All @@ -57,14 +52,6 @@ public class GrpcManager : IDisposable
/// <param name="loggerName"></param>
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<Tuple<string, string>>
Expand All @@ -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();
}

/// <summary>
/// Create a GrpcChannelOptions object from an IGrpcConfiguration object.
/// </summary>
/// <param name="grpcConfig">The IGrpcConfiguration object specifying underlying grpc options</param>
/// <param name="loggerFactory"></param>
/// <returns></returns>
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
{
Expand All @@ -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();
}

/// <summary>
Expand Down

0 comments on commit 2d6d921

Please sign in to comment.