Skip to content

Commit

Permalink
chore: add is supported guard for SocketsHttpHandler (#541)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
malandis authored Feb 29, 2024
1 parent 9bf812c commit 7c9f16d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
16 changes: 11 additions & 5 deletions src/Momento.Sdk/Internal/ControlGrpcManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,21 +95,27 @@ 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) // see: https://github.com/grpc/grpc-dotnet/blob/098dca892a3949ade411c3f2f66003f7b330dfd2/src/Shared/HttpHandlerFactory.cs#L28-L30
{
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<Header> headers = new List<Header> { 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();

Expand Down
11 changes: 7 additions & 4 deletions src/Momento.Sdk/Internal/DataGrpcManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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) // see: https://github.com/grpc/grpc-dotnet/blob/098dca892a3949ade411c3f2f66003f7b330dfd2/src/Shared/HttpHandlerFactory.cs#L28-L30
{
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
Expand Down

0 comments on commit 7c9f16d

Please sign in to comment.