Skip to content

Commit

Permalink
Remove Grpc.Core
Browse files Browse the repository at this point in the history
  • Loading branch information
lemaitre-aneo committed May 23, 2024
1 parent 6812303 commit 7a56a1b
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ArmoniK.Api.Client" Version="3.17.0" />
<PackageReference Include="Grpc.Core" Version="2.46.6" />
<PackageReference Include="ArmoniK.Api.Client" Version="3.17.2-edge.18.1a440c0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.4" />
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="8.0.0" />
<PackageReference Include="System.Net.Http" Version="4.3.4" />
Expand Down
83 changes: 33 additions & 50 deletions Client/src/Common/Submitter/ChannelPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -31,30 +29,30 @@ namespace ArmoniK.DevelopmentKit.Client.Common.Submitter;
/// </summary>
public sealed class ChannelPool
{
private readonly Func<ChannelBase> channelFactory_;
private readonly Func<GrpcChannel> channelFactory_;

private readonly ILogger<ChannelPool>? logger_;

private readonly ConcurrentBag<ChannelBase> pool_;
private readonly ConcurrentBag<GrpcChannel> pool_;

/// <summary>
/// Constructs a new channelPool
/// </summary>
/// <param name="channelFactory">Function used to create new channels</param>
/// <param name="loggerFactory">loggerFactory used to instantiate a logger for the pool</param>
public ChannelPool(Func<ChannelBase> channelFactory,
public ChannelPool(Func<GrpcChannel> channelFactory,
ILoggerFactory? loggerFactory = null)
{
channelFactory_ = channelFactory;
pool_ = new ConcurrentBag<ChannelBase>();
pool_ = new ConcurrentBag<GrpcChannel>();
logger_ = loggerFactory?.CreateLogger<ChannelPool>();
}

/// <summary>
/// Get a channel from the pool. If the pool is empty, create a new channel
/// </summary>
/// <returns>A ChannelBase used by nobody else</returns>
private ChannelBase AcquireChannel()
/// <returns>A GrpcChannel used by nobody else</returns>
private GrpcChannel AcquireChannel()
{
if (pool_.TryTake(out var channel))
{
Expand All @@ -78,10 +76,10 @@ private ChannelBase AcquireChannel()
}

/// <summary>
/// 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
/// </summary>
/// <param name="channel">Channel to release</param>
private void ReleaseChannel(ChannelBase channel)
private void ReleaseChannel(GrpcChannel channel)
{
if (ShutdownOnFailure(channel))
{
Expand All @@ -101,47 +99,30 @@ private void ReleaseChannel(ChannelBase channel)
/// </summary>
/// <param name="channel">Channel to check the state</param>
/// <returns>True if the channel has been shut down</returns>
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 true;
#endif
}
catch (InvalidOperationException)
{
Expand All @@ -162,7 +143,7 @@ public ChannelGuard GetChannel()
/// <param name="f">Function to be called</param>
/// <typeparam name="T">Type of the return type of f</typeparam>
/// <returns>Value returned by f</returns>
public T WithChannel<T>(Func<ChannelBase, T> f)
public T WithChannel<T>(Func<GrpcChannel, T> f)
{
using var channel = GetChannel();
return f(channel);
Expand All @@ -176,7 +157,9 @@ public sealed class ChannelGuard : IDisposable
/// <summary>
/// Channel that is used by nobody else
/// </summary>
private readonly ChannelBase channel_;
[SuppressMessage("Usage",
"CA2213:Disposable fields should be disposed")]
private readonly GrpcChannel channel_;

private readonly ChannelPool pool_;

Expand All @@ -198,8 +181,8 @@ public void Dispose()
/// Implicit convert a ChannelGuard into a ChannelBase
/// </summary>
/// <param name="guard">ChannelGuard</param>
/// <returns>ChannelBase</returns>
public static implicit operator ChannelBase(ChannelGuard guard)
/// <returns>GrpcChannel</returns>
public static implicit operator GrpcChannel(ChannelGuard guard)
=> guard.channel_;
}
}
19 changes: 0 additions & 19 deletions Client/src/Common/Submitter/ClientServiceConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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))));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ArmoniK.Api.Common" Version="3.17.0" />
<PackageReference Include="ArmoniK.Api.Common" Version="3.17.2-edge.18.1a440c0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="8.0.0" />
Expand Down
4 changes: 2 additions & 2 deletions Worker/src/Common/ArmoniK.DevelopmentKit.Worker.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ArmoniK.Api.Worker" Version="3.17.0" />
<PackageReference Include="ArmoniK.Api.Worker" Version="3.17.2-edge.18.1a440c0" />
<PackageReference Include="AWSSDK.S3" Version="3.7.106.1" />
<!-- AWSSDK.SecurityToken is used by AWSSDK.S3 to automatically get credentials from pod secrets -->
<PackageReference Include="AWSSDK.SecurityToken" Version="3.7.103.16" />
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.3" PrivateAssets="All" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.18.1" />
<PackageReference Include="Grpc.Tools" Version="2.56.0">
<PackageReference Include="Grpc.Tools" Version="2.64.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down

0 comments on commit 7a56a1b

Please sign in to comment.