diff --git a/Client/src/Common/ArmoniK.DevelopmentKit.Client.Common.csproj b/Client/src/Common/ArmoniK.DevelopmentKit.Client.Common.csproj index 7ebead0b2..938ac59a1 100644 --- a/Client/src/Common/ArmoniK.DevelopmentKit.Client.Common.csproj +++ b/Client/src/Common/ArmoniK.DevelopmentKit.Client.Common.csproj @@ -8,8 +8,8 @@ - - + + diff --git a/Client/src/Common/Properties.cs b/Client/src/Common/Properties.cs index 5c95c9c58..3b5fa0fae 100644 --- a/Client/src/Common/Properties.cs +++ b/Client/src/Common/Properties.cs @@ -1,13 +1,13 @@ // This file is part of the ArmoniK project -// -// Copyright (C) ANEO, 2021-2023. All rights reserved. -// +// +// Copyright (C) ANEO, 2021-2024. All rights reserved. +// // Licensed under the Apache License, Version 2.0 (the "License") // you may not use this file except in compliance with the License. // You may obtain a copy of the License at -// +// // http://www.apache.org/licenses/LICENSE-2.0 -// +// // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -46,6 +46,9 @@ public class Properties private const string SectionClientKey = "ClientKey"; private const string SectionClientCertP12 = "ClientP12"; private const string SectionTargetNameOverride = "EndpointNameOverride"; + private const string SectionProxy = "Proxy"; + private const string SectionProxyUsername = "ProxyUsername"; + private const string SectionProxyPassword = "ProxyPassword"; private const string SectionRetryInitialBackoff = "RetryInitialBackoff"; private const string SectionRetryBackoffMultiplier = "RetryBackoffMultiplier"; @@ -129,7 +132,10 @@ public Properties(IConfiguration configuration, bool? sslValidation = null, TimeSpan retryInitialBackoff = new(), double retryBackoffMultiplier = 0, - TimeSpan retryMaxBackoff = new()) + TimeSpan retryMaxBackoff = new(), + string? proxy = null, + string? proxyUsername = null, + string? proxyPassword = null) { TaskOptions = options; Configuration = configuration; @@ -157,11 +163,14 @@ public Properties(IConfiguration configuration, Protocol = protocol ?? Protocol; ConfSSLValidation = sslValidation ?? sectionGrpc?[SectionSSlValidation] != "disable"; - TargetNameOverride = sectionGrpc?[SectionTargetNameOverride]; - CaCertFilePem = caCertPem ?? sectionGrpc?[SectionCaCert]; - ClientCertFilePem = clientCertFilePem ?? sectionGrpc?[SectionClientCert]; - ClientKeyFilePem = clientKeyFilePem ?? sectionGrpc?[SectionClientKey]; - ClientP12File = clientP12 ?? sectionGrpc?[SectionClientCertP12]; + TargetNameOverride = sectionGrpc[SectionTargetNameOverride] ?? string.Empty; + CaCertFilePem = caCertPem ?? sectionGrpc[SectionCaCert] ?? string.Empty; + ClientCertFilePem = clientCertFilePem ?? sectionGrpc[SectionClientCert] ?? string.Empty; + ClientKeyFilePem = clientKeyFilePem ?? sectionGrpc[SectionClientKey] ?? string.Empty; + ClientP12File = clientP12 ?? sectionGrpc[SectionClientCertP12] ?? string.Empty; + Proxy = proxy ?? sectionGrpc[SectionProxy] ?? string.Empty; + ProxyUsername = proxyUsername ?? sectionGrpc[SectionProxyUsername] ?? string.Empty; + ProxyPassword = proxyPassword ?? sectionGrpc[SectionProxyPassword] ?? string.Empty; if (retryInitialBackoff != TimeSpan.Zero) { @@ -332,4 +341,19 @@ public string ConnectionString /// Max backoff for retries /// public TimeSpan RetryMaxBackoff { get; } = TimeSpan.FromSeconds(30); + + /// + /// Proxy URL + /// + public string Proxy { get; set; } + + /// + /// Username for the proxy + /// + public string ProxyUsername { get; set; } + + /// + /// Password for the proxy + /// + public string ProxyPassword { get; set; } } diff --git a/Client/src/Common/Submitter/ChannelPool.cs b/Client/src/Common/Submitter/ChannelPool.cs index ea8f07a80..4abf9ca7f 100644 --- a/Client/src/Common/Submitter/ChannelPool.cs +++ b/Client/src/Common/Submitter/ChannelPool.cs @@ -1,13 +1,13 @@ // This file is part of the ArmoniK project -// -// Copyright (C) ANEO, 2021-2023. All rights reserved. -// +// +// Copyright (C) ANEO, 2021-2024. All rights reserved. +// // Licensed under the Apache License, Version 2.0 (the "License") // you may not use this file except in compliance with the License. // You may obtain a copy of the License at -// +// // http://www.apache.org/licenses/LICENSE-2.0 -// +// // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -16,15 +16,12 @@ using System; using System.Collections.Concurrent; +using System.Diagnostics.CodeAnalysis; using Grpc.Core; - -using JetBrains.Annotations; +using Grpc.Net.Client; using Microsoft.Extensions.Logging; -#if NET5_0_OR_GREATER -using Grpc.Net.Client; -#endif namespace ArmoniK.DevelopmentKit.Client.Common.Submitter; @@ -33,31 +30,30 @@ namespace ArmoniK.DevelopmentKit.Client.Common.Submitter; /// public sealed class ChannelPool { - private readonly Func channelFactory_; + private readonly Func channelFactory_; - [CanBeNull] - private readonly ILogger logger_; + private readonly ILogger? logger_; - private readonly ConcurrentBag pool_; + private readonly ConcurrentBag pool_; /// /// Constructs a new channelPool /// /// Function used to create new channels /// loggerFactory used to instantiate a logger for the pool - public ChannelPool(Func channelFactory, - [CanBeNull] ILoggerFactory loggerFactory = null) + public ChannelPool(Func channelFactory, + ILoggerFactory? loggerFactory = null) { channelFactory_ = channelFactory; - pool_ = new ConcurrentBag(); + pool_ = new ConcurrentBag(); logger_ = loggerFactory?.CreateLogger(); } /// /// Get a channel from the pool. If the pool is empty, create a new channel /// - /// A ChannelBase used by nobody else - private ChannelBase AcquireChannel() + /// A GrpcChannel used by nobody else + private GrpcChannel AcquireChannel() { if (pool_.TryTake(out var channel)) { @@ -81,10 +77,10 @@ private ChannelBase AcquireChannel() } /// - /// Release a ChannelBase to the pool that could be reused later by someone else + /// Release a GrpcChannel to the pool that could be reused later by someone else /// /// Channel to release - private void ReleaseChannel(ChannelBase channel) + private void ReleaseChannel(GrpcChannel channel) { if (ShutdownOnFailure(channel)) { @@ -104,47 +100,30 @@ private void ReleaseChannel(ChannelBase channel) /// /// Channel to check the state /// True if the channel has been shut down - private static bool ShutdownOnFailure(ChannelBase channel) + private static bool ShutdownOnFailure(GrpcChannel channel) { try { - switch (channel) - { - case Channel chan: - switch (chan.State) - { - case ChannelState.TransientFailure: - chan.ShutdownAsync() - .Wait(); - return true; - case ChannelState.Shutdown: - return true; - case ChannelState.Idle: - case ChannelState.Connecting: - case ChannelState.Ready: - default: - return false; - } #if NET5_0_OR_GREATER - case GrpcChannel chan: - switch (chan.State) - { - case ConnectivityState.TransientFailure: - chan.ShutdownAsync() - .Wait(); - return true; - case ConnectivityState.Shutdown: - return true; - case ConnectivityState.Idle: - case ConnectivityState.Connecting: - case ConnectivityState.Ready: - default: - return false; - } -#endif + switch (channel.State) + { + case ConnectivityState.TransientFailure: + channel.ShutdownAsync() + .Wait(); + channel.Dispose(); + return true; + case ConnectivityState.Shutdown: + return true; + case ConnectivityState.Idle: + case ConnectivityState.Connecting: + case ConnectivityState.Ready: default: return false; } +#else + _ = channel; + return false; +#endif } catch (InvalidOperationException) { @@ -165,20 +144,10 @@ public ChannelGuard GetChannel() /// Function to be called /// Type of the return type of f /// Value returned by f - public T WithChannel(Func f) - { - using var channel = GetChannel(); - return f(channel.Channel); - } - - /// - /// Call f with an acquired channel - /// - /// Function to be called - public void WithChannel(Action f) + public T WithChannel(Func f) { using var channel = GetChannel(); - f(channel.Channel); + return f(channel); } /// @@ -189,7 +158,9 @@ public sealed class ChannelGuard : IDisposable /// /// Channel that is used by nobody else /// - public readonly ChannelBase Channel; + [SuppressMessage("Usage", + "CA2213:Disposable fields should be disposed")] + private readonly GrpcChannel channel_; private readonly ChannelPool pool_; @@ -199,20 +170,20 @@ public sealed class ChannelGuard : IDisposable /// public ChannelGuard(ChannelPool channelPool) { - pool_ = channelPool; - Channel = channelPool.AcquireChannel(); + pool_ = channelPool; + channel_ = channelPool.AcquireChannel(); } /// public void Dispose() - => pool_.ReleaseChannel(Channel); + => pool_.ReleaseChannel(channel_); /// /// Implicit convert a ChannelGuard into a ChannelBase /// /// ChannelGuard - /// ChannelBase - public static implicit operator ChannelBase(ChannelGuard guard) - => guard.Channel; + /// GrpcChannel + public static implicit operator GrpcChannel(ChannelGuard guard) + => guard.channel_; } } diff --git a/Client/src/Common/Submitter/ClientServiceConnector.cs b/Client/src/Common/Submitter/ClientServiceConnector.cs index c266c0b94..bf90c4aab 100644 --- a/Client/src/Common/Submitter/ClientServiceConnector.cs +++ b/Client/src/Common/Submitter/ClientServiceConnector.cs @@ -14,8 +14,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -using System; - using ArmoniK.Api.Client.Options; using ArmoniK.Api.Client.Submitter; @@ -49,25 +47,13 @@ public static ChannelPool ControlPlaneConnectionPool(Properties KeyPem = properties.ClientKeyFilePem, Endpoint = properties.ControlPlaneUri.ToString(), OverrideTargetName = properties.TargetNameOverride, + BackoffMultiplier = properties.RetryBackoffMultiplier, + InitialBackOff = properties.RetryInitialBackoff, + Proxy = properties.Proxy, + ProxyUsername = properties.ProxyUsername, + ProxyPassword = properties.ProxyPassword, }; - if (properties.ControlPlaneUri.Scheme == Uri.UriSchemeHttps && options.AllowUnsafeConnection && string.IsNullOrEmpty(options.OverrideTargetName)) - { -#if NET5_0_OR_GREATER - var doOverride = !string.IsNullOrEmpty(options.CaCert); -#else - var doOverride = true; -#endif - if (doOverride) - { - // Doing it here once to improve performance - options.OverrideTargetName = GrpcChannelFactory.GetOverrideTargetName(options, - GrpcChannelFactory.GetServerCertificate(properties.ControlPlaneUri, - options)) ?? ""; - } - } - - return new ChannelPool(() => GrpcChannelFactory.CreateChannel(options, loggerFactory?.CreateLogger(typeof(ClientServiceConnector)))); } diff --git a/Tests/ArmoniK.EndToEndTests/ArmoniK.EndToEndTests.Common/ArmoniK.EndToEndTests.Common.csproj b/Tests/ArmoniK.EndToEndTests/ArmoniK.EndToEndTests.Common/ArmoniK.EndToEndTests.Common.csproj index 93bd859ec..d2f216529 100644 --- a/Tests/ArmoniK.EndToEndTests/ArmoniK.EndToEndTests.Common/ArmoniK.EndToEndTests.Common.csproj +++ b/Tests/ArmoniK.EndToEndTests/ArmoniK.EndToEndTests.Common/ArmoniK.EndToEndTests.Common.csproj @@ -13,7 +13,7 @@ - + diff --git a/Worker/src/Common/ArmoniK.DevelopmentKit.Worker.Common.csproj b/Worker/src/Common/ArmoniK.DevelopmentKit.Worker.Common.csproj index da1afc169..24c65b95d 100644 --- a/Worker/src/Common/ArmoniK.DevelopmentKit.Worker.Common.csproj +++ b/Worker/src/Common/ArmoniK.DevelopmentKit.Worker.Common.csproj @@ -1,19 +1,19 @@  - net6.0 + net6.0;net8.0 Library True true - + - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/Worker/src/Symphony/ArmoniK.DevelopmentKit.Worker.Symphony.csproj b/Worker/src/Symphony/ArmoniK.DevelopmentKit.Worker.Symphony.csproj index 76af55201..614d18b9e 100644 --- a/Worker/src/Symphony/ArmoniK.DevelopmentKit.Worker.Symphony.csproj +++ b/Worker/src/Symphony/ArmoniK.DevelopmentKit.Worker.Symphony.csproj @@ -1,7 +1,7 @@ - net6.0 + net6.0;net8.0 Library True true diff --git a/Worker/src/Unified/ArmoniK.DevelopmentKit.Worker.Unified.csproj b/Worker/src/Unified/ArmoniK.DevelopmentKit.Worker.Unified.csproj index 1010f8561..641b8bc21 100644 --- a/Worker/src/Unified/ArmoniK.DevelopmentKit.Worker.Unified.csproj +++ b/Worker/src/Unified/ArmoniK.DevelopmentKit.Worker.Unified.csproj @@ -1,7 +1,7 @@ - net6.0 + net6.0;net8.0 Library True true