Skip to content
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

fix: Add ReusePorts option #524

Merged
merged 2 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/csharp/ArmoniK.Api.Client/Options/GrpcClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,5 +154,10 @@ public bool HasClientCertificate
/// Password used for proxy authentication
/// </summary>
public string ProxyPassword { get; set; } = "";

/// <summary>
/// Enable the option SO_REUSE_UNICASTPORT upon socket opening to limit port exhaustion
/// </summary>
public bool ReusePorts { get; set; } = true;
}
}
32 changes: 32 additions & 0 deletions packages/csharp/ArmoniK.Api.Client/Submitter/GrpcChannelFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,11 @@ private static GrpcChannel CreateChannelInternal(GrpcClient optionsGrpcClient,
throw new InvalidOperationException($"{nameof(optionsGrpcClient.Endpoint)} should not be null or empty");
}

if (optionsGrpcClient.ReusePorts)
{
ServicePointManager.ReusePort = true;
}

var serviceConfig = new ServiceConfig
{
MethodConfigs =
Expand Down Expand Up @@ -389,6 +394,8 @@ private static GrpcChannel CreateChannelInternal(GrpcClient optionsGrpcClient,
proxyType,
handlerType,
logger);
httpHandler = new Handler(httpHandler,
logger);

// Warn that RequestTimeout is not supported.
// If required, it could be easily implemented with a DelegatingHandler and a cancellationToken delayed cancellation
Expand Down Expand Up @@ -625,5 +632,30 @@ private enum HandlerType
/// </summary>
Web,
}

private class Handler : DelegatingHandler
{
private readonly ILogger? logger_;

internal Handler(HttpMessageHandler inner,
ILogger? logger)
: base(inner)
=> logger_ = logger;

protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
CancellationToken cancellationToken)
{
var response = await base.SendAsync(request,
cancellationToken)
.ConfigureAwait(false);

if (response.Headers.ConnectionClose is true)
{
logger_?.LogInformation("Connection closing has been requested, performance degradation is expected");
}

return response;
}
}
}
}
Loading