Skip to content

Commit

Permalink
more idiomatic way to override control clients keepalive settings
Browse files Browse the repository at this point in the history
  • Loading branch information
anitarua committed Mar 5, 2024
1 parent 8b6ecf3 commit 456c4fb
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,11 @@ public interface IVectorIndexTransportStrategy
/// <param name="clientTimeout"></param>
/// <returns>A new IVectorIndexTransportStrategy with the specified client timeout</returns>
public IVectorIndexTransportStrategy WithClientTimeout(TimeSpan clientTimeout);

/// <summary>
/// Copy constructor to update the SocketsHttpHandler's options
/// </summary>
/// <param name="options"></param>
/// <returns></returns>
public IVectorIndexTransportStrategy WithSocketsHttpHandlerOptions(SocketsHttpHandlerOptions options);
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public IVectorIndexTransportStrategy WithClientTimeout(TimeSpan clientTimeout)
return new StaticVectorIndexTransportStrategy(_loggerFactory, GrpcConfig.WithDeadline(clientTimeout));
}

public IVectorIndexTransportStrategy WithSocketsHttpHandlerOptions(SocketsHttpHandlerOptions options)

Check warning on line 40 in src/Momento.Sdk/Config/Transport/StaticVectorIndexTransportStrategy.cs

View workflow job for this annotation

GitHub Actions / build_csharp (ubuntu-latest, net6.0)

Missing XML comment for publicly visible type or member 'StaticVectorIndexTransportStrategy.WithSocketsHttpHandlerOptions(SocketsHttpHandlerOptions)'

Check warning on line 40 in src/Momento.Sdk/Config/Transport/StaticVectorIndexTransportStrategy.cs

View workflow job for this annotation

GitHub Actions / build_csharp (ubuntu-latest, net6.0)

Missing XML comment for publicly visible type or member 'StaticVectorIndexTransportStrategy.WithSocketsHttpHandlerOptions(SocketsHttpHandlerOptions)'

Check warning on line 40 in src/Momento.Sdk/Config/Transport/StaticVectorIndexTransportStrategy.cs

View workflow job for this annotation

GitHub Actions / build_csharp (windows-latest, net6.0)

Missing XML comment for publicly visible type or member 'StaticVectorIndexTransportStrategy.WithSocketsHttpHandlerOptions(SocketsHttpHandlerOptions)'

Check warning on line 40 in src/Momento.Sdk/Config/Transport/StaticVectorIndexTransportStrategy.cs

View workflow job for this annotation

GitHub Actions / build_csharp (windows-latest, net462)

Missing XML comment for publicly visible type or member 'StaticVectorIndexTransportStrategy.WithSocketsHttpHandlerOptions(SocketsHttpHandlerOptions)'
{
return new StaticVectorIndexTransportStrategy(_loggerFactory, GrpcConfig.WithSocketsHttpHandlerOptions(options));
}

/// <summary>
/// Test equality by value.
/// </summary>
Expand Down
17 changes: 3 additions & 14 deletions src/Momento.Sdk/Internal/GrpcManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,24 +69,13 @@ internal GrpcManager(IGrpcConfiguration grpcConfig, ILoggerFactory loggerFactory
#if NET5_0_OR_GREATER
if (SocketsHttpHandler.IsSupported) // see: https://github.com/grpc/grpc-dotnet/blob/098dca892a3949ade411c3f2f66003f7b330dfd2/src/Shared/HttpHandlerFactory.cs#L28-L30
{
var keepAliveWithoutCallsPolicy = grpcConfig.SocketsHttpHandlerOptions.KeepAlivePermitWithoutCalls == true ? System.Net.Http.HttpKeepAlivePingPolicy.Always : System.Net.Http.HttpKeepAlivePingPolicy.WithActiveRequests;
var keepAliveTimeout = grpcConfig.SocketsHttpHandlerOptions.KeepAlivePingTimeout;
var keepAlivePingDelay = grpcConfig.SocketsHttpHandlerOptions.KeepAlivePingDelay;

// Disable keepalives for control clients and convert KeepAlivePermitWithoutCalls from bool to HttpKeepAlivePingPolicy
if (loggerName.Contains("Control")) {
keepAliveWithoutCallsPolicy = System.Net.Http.HttpKeepAlivePingPolicy.WithActiveRequests;
keepAliveTimeout = System.Threading.Timeout.InfiniteTimeSpan;
keepAlivePingDelay = System.Threading.Timeout.InfiniteTimeSpan;
}

channelOptions.HttpHandler = new SocketsHttpHandler
{
EnableMultipleHttp2Connections = grpcConfig.SocketsHttpHandlerOptions.EnableMultipleHttp2Connections,
PooledConnectionIdleTimeout = grpcConfig.SocketsHttpHandlerOptions.PooledConnectionIdleTimeout,
KeepAlivePingTimeout = keepAliveTimeout,
KeepAlivePingDelay = keepAlivePingDelay,
KeepAlivePingPolicy = keepAliveWithoutCallsPolicy,
KeepAlivePingTimeout = grpcConfig.SocketsHttpHandlerOptions.KeepAlivePingTimeout,
KeepAlivePingDelay = grpcConfig.SocketsHttpHandlerOptions.KeepAlivePingDelay,
KeepAlivePingPolicy = grpcConfig.SocketsHttpHandlerOptions.KeepAlivePermitWithoutCalls == true ? System.Net.Http.HttpKeepAlivePingPolicy.Always : System.Net.Http.HttpKeepAlivePingPolicy.WithActiveRequests,
};
}
#elif USE_GRPC_WEB
Expand Down
13 changes: 12 additions & 1 deletion src/Momento.Sdk/Internal/ScsControlClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Microsoft.Extensions.Logging;
using Momento.Protos.ControlClient;
using Momento.Sdk.Config;
using Momento.Sdk.Config.Transport;
using Momento.Sdk.Exceptions;
using Momento.Sdk.Responses;

Expand All @@ -20,7 +21,17 @@ internal sealed class ScsControlClient : IDisposable

public ScsControlClient(IConfiguration config, string authToken, string endpoint)
{
this.grpcManager = new ControlGrpcManager(config, authToken, endpoint);
// Override the sockets http handler options to disable keepalive
var overrideKeepalive = SocketsHttpHandlerOptions.Of(
pooledConnectionIdleTimeout: config.TransportStrategy.GrpcConfig.SocketsHttpHandlerOptions.PooledConnectionIdleTimeout,
enableMultipleHttp2Connections: config.TransportStrategy.GrpcConfig.SocketsHttpHandlerOptions.EnableMultipleHttp2Connections,
keepAlivePingTimeout: System.Threading.Timeout.InfiniteTimeSpan,
keepAlivePingDelay: System.Threading.Timeout.InfiniteTimeSpan,
keepAlivePermitWithoutCalls: false
);
var controlConfig = config.WithTransportStrategy(config.TransportStrategy.WithSocketsHttpHandlerOptions(overrideKeepalive));

this.grpcManager = new ControlGrpcManager(controlConfig, authToken, endpoint);
this.authToken = authToken;
this._logger = config.LoggerFactory.CreateLogger<ScsControlClient>();
this._exceptionMapper = new CacheExceptionMapper(config.LoggerFactory);
Expand Down
13 changes: 12 additions & 1 deletion src/Momento.Sdk/Internal/VectorIndexControlClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Momento.Sdk.Requests.Vector;
using Momento.Sdk.Responses.Vector;
using Momento.Sdk.Config;
using Momento.Sdk.Config.Transport;

namespace Momento.Sdk.Internal;

Expand All @@ -22,7 +23,17 @@ internal sealed class VectorIndexControlClient : IDisposable

public VectorIndexControlClient(IVectorIndexConfiguration config, string authToken, string endpoint)
{
grpcManager = new VectorIndexControlGrpcManager(config, authToken, endpoint);
// Override the sockets http handler options to disable keepalive
var overrideKeepalive = SocketsHttpHandlerOptions.Of(
pooledConnectionIdleTimeout: config.TransportStrategy.GrpcConfig.SocketsHttpHandlerOptions.PooledConnectionIdleTimeout,
enableMultipleHttp2Connections: config.TransportStrategy.GrpcConfig.SocketsHttpHandlerOptions.EnableMultipleHttp2Connections,
keepAlivePingTimeout: System.Threading.Timeout.InfiniteTimeSpan,
keepAlivePingDelay: System.Threading.Timeout.InfiniteTimeSpan,
keepAlivePermitWithoutCalls: false
);
var controlConfig = config.WithTransportStrategy(config.TransportStrategy.WithSocketsHttpHandlerOptions(overrideKeepalive));

grpcManager = new VectorIndexControlGrpcManager(controlConfig, authToken, endpoint);
_logger = config.LoggerFactory.CreateLogger<VectorIndexControlClient>();
_exceptionMapper = new CacheExceptionMapper(config.LoggerFactory);
}
Expand Down

0 comments on commit 456c4fb

Please sign in to comment.