diff --git a/Client/src/Common/ArmoniK.DevelopmentKit.Client.Common.csproj b/Client/src/Common/ArmoniK.DevelopmentKit.Client.Common.csproj index 990f3414..be849b1d 100644 --- a/Client/src/Common/ArmoniK.DevelopmentKit.Client.Common.csproj +++ b/Client/src/Common/ArmoniK.DevelopmentKit.Client.Common.csproj @@ -8,8 +8,7 @@ - - + diff --git a/Client/src/Common/Submitter/ChannelPool.cs b/Client/src/Common/Submitter/ChannelPool.cs index 9981a83d..a7db51b0 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. -// +// // 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,13 +16,11 @@ using System; using System.Collections.Concurrent; +using System.Diagnostics.CodeAnalysis; -using Grpc.Core; +using Grpc.Net.Client; using Microsoft.Extensions.Logging; -#if NET5_0_OR_GREATER -using Grpc.Net.Client; -#endif namespace ArmoniK.DevelopmentKit.Client.Common.Submitter; @@ -31,30 +29,30 @@ namespace ArmoniK.DevelopmentKit.Client.Common.Submitter; /// public sealed class ChannelPool { - private readonly Func channelFactory_; + private readonly Func channelFactory_; 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, + 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)) { @@ -78,10 +76,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)) { @@ -101,47 +99,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) { @@ -162,7 +143,7 @@ public ChannelGuard GetChannel() /// Function to be called /// Type of the return type of f /// Value returned by f - public T WithChannel(Func f) + public T WithChannel(Func f) { using var channel = GetChannel(); return f(channel); @@ -176,7 +157,9 @@ public sealed class ChannelGuard : IDisposable /// /// Channel that is used by nobody else /// - private readonly ChannelBase channel_; + [SuppressMessage("Usage", + "CA2213:Disposable fields should be disposed")] + private readonly GrpcChannel channel_; private readonly ChannelPool pool_; @@ -198,8 +181,8 @@ public void Dispose() /// Implicit convert a ChannelGuard into a ChannelBase /// /// ChannelGuard - /// ChannelBase - public static implicit operator ChannelBase(ChannelGuard guard) + /// 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 bc5be60d..a5d19be6 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,23 +47,6 @@ public static ChannelPool ControlPlaneConnectionPool(Properties properties, OverrideTargetName = properties.TargetNameOverride, }; - 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 2e059028..3442ae11 100644 --- a/Tests/ArmoniK.EndToEndTests/ArmoniK.EndToEndTests.Common/ArmoniK.EndToEndTests.Common.csproj +++ b/Tests/ArmoniK.EndToEndTests/ArmoniK.EndToEndTests.Common/ArmoniK.EndToEndTests.Common.csproj @@ -12,7 +12,7 @@ - + diff --git a/Worker/src/Common/ArmoniK.DevelopmentKit.Worker.Common.csproj b/Worker/src/Common/ArmoniK.DevelopmentKit.Worker.Common.csproj index b02ba907..d23b8969 100644 --- a/Worker/src/Common/ArmoniK.DevelopmentKit.Worker.Common.csproj +++ b/Worker/src/Common/ArmoniK.DevelopmentKit.Worker.Common.csproj @@ -8,13 +8,13 @@ - + - + all runtime; build; native; contentfiles; analyzers; buildtransitive