Skip to content

Commit

Permalink
fix: Add ReusePorts option (#524)
Browse files Browse the repository at this point in the history
  • Loading branch information
lemaitre-aneo authored Jun 24, 2024
2 parents ffe7550 + c18944f commit 083f1d5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
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;
}
}
}
}

0 comments on commit 083f1d5

Please sign in to comment.