Skip to content

Commit

Permalink
fix: disable keepalive for control clients (#542)
Browse files Browse the repository at this point in the history
* fix: disable keepalive for control clients

* more idiomatic way to override control clients keepalive settings

* remove redundant ==true
  • Loading branch information
anitarua authored Mar 5, 2024
1 parent 4db09f8 commit bc15ddf
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 3 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, 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
2 changes: 1 addition & 1 deletion src/Momento.Sdk/Internal/GrpcManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ internal GrpcManager(IGrpcConfiguration grpcConfig, ILoggerFactory loggerFactory
PooledConnectionIdleTimeout = grpcConfig.SocketsHttpHandlerOptions.PooledConnectionIdleTimeout,
KeepAlivePingTimeout = grpcConfig.SocketsHttpHandlerOptions.KeepAlivePingTimeout,
KeepAlivePingDelay = grpcConfig.SocketsHttpHandlerOptions.KeepAlivePingDelay,
KeepAlivePingPolicy = grpcConfig.SocketsHttpHandlerOptions.KeepAlivePermitWithoutCalls == true ? System.Net.Http.HttpKeepAlivePingPolicy.Always : System.Net.Http.HttpKeepAlivePingPolicy.WithActiveRequests,
KeepAlivePingPolicy = grpcConfig.SocketsHttpHandlerOptions.KeepAlivePermitWithoutCalls ? 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 bc15ddf

Please sign in to comment.