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

chore: parametrize keep alive options #590

Merged
merged 11 commits into from
Feb 7, 2025
10 changes: 10 additions & 0 deletions packages/csharp/ArmoniK.Api.Common/Options/ComputePlane.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ public class ComputePlane
/// </summary>
public const string SettingSection = nameof(ComputePlane);

/// <summary>
/// Path to the section containing the worker channel values in configuration object
/// </summary>
public const string WorkerChannelSection = "WorkerChannel";

/// <summary>
/// Path to the section containing the agent channel values in configuration object
/// </summary>
public const string AgentChannelSection = "AgentChannel";
jfonseca-aneo marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Channel used by the Agent to send tasks to the Worker
/// </summary>
Expand Down
12 changes: 12 additions & 0 deletions packages/csharp/ArmoniK.Api.Common/Options/GrpcChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System;

using JetBrains.Annotations;

namespace ArmoniK.Api.Common.Options;
Expand All @@ -40,4 +42,14 @@ public class GrpcChannel
/// Type of gRPC Socket used
/// </summary>
public GrpcSocketType SocketType { get; set; } = GrpcSocketType.UnixDomainSocket;

/// <summary>
/// Keep-alive ping timeout for http2 connections
/// </summary>
public TimeSpan KeepAlivePingTimeOut { get; set; }

/// <summary>
/// Keep-alive timeout
/// </summary>
public TimeSpan KeepAliveTimeOut { get; set; }
}
37 changes: 35 additions & 2 deletions packages/csharp/ArmoniK.Api.Worker/Utils/WorkerServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ public static WebApplication Create<T>(Action<IServiceCollection, IConfiguration
throw new Exception($"{nameof(computePlaneOptions.WorkerChannel)} options should not be null");
}

builder.WebHost.ConfigureKestrel(options =>
builder.WebHost.ConfigureKestrel((context, options) =>
{
var address = computePlaneOptions.WorkerChannel.Address;
var address = computePlaneOptions.WorkerChannel.Address;
switch (computePlaneOptions.WorkerChannel.SocketType)
{
case GrpcSocketType.UnixDomainSocket:
Expand All @@ -133,10 +133,43 @@ public static WebApplication Create<T>(Action<IServiceCollection, IConfiguration
listenOptions => listenOptions.Protocols = HttpProtocols.Http2);
break;
case GrpcSocketType.Tcp:

var channelOptions = context.Configuration.GetRequiredSection(ComputePlane.SettingSection).
GetSection(ComputePlane.WorkerChannelSection);
options.Limits.Http2.KeepAlivePingTimeout = ParseTimeSpan(channelOptions["KeepAlivePingTimeout"],
TimeSpan.FromSeconds(20));
options.Limits.KeepAliveTimeout = ParseTimeSpan(channelOptions["KeepAliveTimeOut"],
TimeSpan.FromSeconds(130));
var uri = new Uri(address);
options.ListenAnyIP(uri.Port,
listenOptions => listenOptions.Protocols = HttpProtocols.Http2);
break;

/* Local function to parse the KeepAlive options from the configuration section.
If the section to parse does not exist returns the given defaultValue
If a valid Time.Span string has been provided it parses and returns it
Provide support to handle the case where the option has been set to "MaxValue". */
TimeSpan ParseTimeSpan(string? toParse,
TimeSpan defaultValue)
{
if (string.IsNullOrEmpty(toParse))
{
return defaultValue;
}

if (toParse.Equals("MaxValue"))
{
return TimeSpan.MaxValue;
}

if (TimeSpan.TryParse(toParse,
out var result))
{
return result;
}

throw new FormatException($"Provide a valid TimeSpan format: {toParse} or 'MaxValue'");
}
jfonseca-aneo marked this conversation as resolved.
Show resolved Hide resolved
default:
throw new InvalidOperationException("Socket type unknown");
}
Expand Down
Loading