-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: adds customization options for SocketsHttpHandler #539
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/Momento.Sdk/Config/Transport/SocketsHttpHandlerOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#pragma warning disable 1591 | ||
using System; | ||
using Momento.Sdk.Internal; | ||
namespace Momento.Sdk.Config.Transport; | ||
|
||
public class SocketsHttpHandlerOptions | ||
{ | ||
public static TimeSpan DefaultPooledConnectionIdleTimeout { get; } = TimeSpan.FromMinutes(1); | ||
public TimeSpan PooledConnectionIdleTimeout { get; } = DefaultPooledConnectionIdleTimeout; | ||
public bool EnableMultipleHttp2Connections { get; } = true; | ||
|
||
public SocketsHttpHandlerOptions() { } | ||
public SocketsHttpHandlerOptions(TimeSpan pooledConnectionIdleTimeout) : this(pooledConnectionIdleTimeout, true) { } | ||
public SocketsHttpHandlerOptions(bool enableMultipleHttp2Connections) : this(DefaultPooledConnectionIdleTimeout, enableMultipleHttp2Connections) { } | ||
|
||
public SocketsHttpHandlerOptions(TimeSpan pooledConnectionIdleTimeout, bool enableMultipleHttp2Connections) | ||
{ | ||
Utils.ArgumentStrictlyPositive(pooledConnectionIdleTimeout, nameof(pooledConnectionIdleTimeout)); | ||
PooledConnectionIdleTimeout = pooledConnectionIdleTimeout; | ||
EnableMultipleHttp2Connections = enableMultipleHttp2Connections; | ||
} | ||
|
||
public SocketsHttpHandlerOptions WithPooledConnectionIdleTimeout(TimeSpan pooledConnectionIdleTimeout) | ||
{ | ||
return new SocketsHttpHandlerOptions(pooledConnectionIdleTimeout, EnableMultipleHttp2Connections); | ||
} | ||
|
||
public SocketsHttpHandlerOptions WithEnableMultipleHttp2Connections(bool enableMultipleHttp2Connections) | ||
{ | ||
return new SocketsHttpHandlerOptions(PooledConnectionIdleTimeout, enableMultipleHttp2Connections); | ||
} | ||
|
||
public static SocketsHttpHandlerOptions Of(TimeSpan pooledConnectionIdleTimeout) | ||
{ | ||
return new SocketsHttpHandlerOptions(pooledConnectionIdleTimeout); | ||
} | ||
|
||
public static SocketsHttpHandlerOptions Of(bool enableMultipleHttp2Connections) | ||
{ | ||
return new SocketsHttpHandlerOptions(enableMultipleHttp2Connections); | ||
} | ||
|
||
public static SocketsHttpHandlerOptions Of(TimeSpan pooledConnectionIdleTimeout, bool enableMultipleHttp2Connections) | ||
{ | ||
return new SocketsHttpHandlerOptions(pooledConnectionIdleTimeout, enableMultipleHttp2Connections); | ||
} | ||
|
||
public override bool Equals(object obj) | ||
{ | ||
if (obj == null || GetType() != obj.GetType()) | ||
{ | ||
return false; | ||
} | ||
|
||
var other = (SocketsHttpHandlerOptions)obj; | ||
return PooledConnectionIdleTimeout.Equals(other.PooledConnectionIdleTimeout) && | ||
EnableMultipleHttp2Connections.Equals(other.EnableMultipleHttp2Connections); | ||
} | ||
|
||
public override int GetHashCode() | ||
{ | ||
return PooledConnectionIdleTimeout.GetHashCode() * 17 + EnableMultipleHttp2Connections.GetHashCode(); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,12 +4,13 @@ | |
using System.Threading.Tasks; | ||
using Grpc.Core; | ||
using Grpc.Net.Client; | ||
#if USE_GRPC_WEB | ||
using System.Net.Http; | ||
#if USE_GRPC_WEB | ||
using Grpc.Net.Client.Web; | ||
#endif | ||
using Microsoft.Extensions.Logging; | ||
using Momento.Protos.ControlClient; | ||
using Momento.Sdk.Config; | ||
using Momento.Sdk.Config.Middleware; | ||
using Momento.Sdk.Internal.Middleware; | ||
using static System.Reflection.Assembly; | ||
|
@@ -86,8 +87,9 @@ internal sealed class ControlGrpcManager : IDisposable | |
private readonly string runtimeVersion = $"{moniker}:{System.Environment.Version}"; | ||
private readonly ILogger _logger; | ||
|
||
public ControlGrpcManager(ILoggerFactory loggerFactory, string authToken, string endpoint) | ||
public ControlGrpcManager(IConfiguration config, string authToken, string endpoint) | ||
{ | ||
this._logger = config.LoggerFactory.CreateLogger<ControlGrpcManager>(); | ||
#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}"; | ||
|
@@ -98,20 +100,24 @@ public ControlGrpcManager(ILoggerFactory loggerFactory, string authToken, string | |
Credentials = ChannelCredentials.SecureSsl, | ||
MaxReceiveMessageSize = Internal.Utils.DEFAULT_MAX_MESSAGE_SIZE, | ||
MaxSendMessageSize = Internal.Utils.DEFAULT_MAX_MESSAGE_SIZE, | ||
#if USE_GRPC_WEB | ||
HttpHandler = new GrpcWebHandler(new HttpClientHandler()), | ||
#if NET5_0_OR_GREATER | ||
HttpHandler = new System.Net.Http.SocketsHttpHandler | ||
{ | ||
EnableMultipleHttp2Connections = config.TransportStrategy.GrpcConfig.SocketsHttpHandlerOptions.EnableMultipleHttp2Connections, | ||
PooledConnectionIdleTimeout = config.TransportStrategy.GrpcConfig.SocketsHttpHandlerOptions.PooledConnectionIdleTimeout | ||
} | ||
Comment on lines
+103
to
+108
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here is where we set the SocketsHttpHandler settings for the control client |
||
#elif USE_GRPC_WEB | ||
HttpHandler = new GrpcWebHandler(new HttpClientHandler()) | ||
#endif | ||
}); | ||
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(); | ||
|
||
var middlewares = new List<IMiddleware> { | ||
new HeaderMiddleware(loggerFactory, headers) | ||
new HeaderMiddleware(config.LoggerFactory, headers) | ||
}; | ||
|
||
Client = new ControlClientWithMiddleware(new ScsControl.ScsControlClient(invoker), middlewares); | ||
|
||
this._logger = loggerFactory.CreateLogger<ControlGrpcManager>(); | ||
} | ||
|
||
public void Dispose() | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Curious if we needed in this PR and if
true
is default (should be but confirming). I am having a hard time connect this with gRPC as ideally the http2 control should be in the gRPC libraries.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is to align with the gRPC .NET client defaults. We can revisit later: https://github.com/grpc/grpc-dotnet/blob/098dca892a3949ade411c3f2f66003f7b330dfd2/src/Shared/HttpHandlerFactory.cs#L27-L51