Skip to content

Commit

Permalink
fix: remove gRPC Core (#261)
Browse files Browse the repository at this point in the history
  • Loading branch information
ngruelaneo authored and lemaitre-aneo committed Oct 28, 2024
1 parent 646a267 commit 66eddc0
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 112 deletions.
4 changes: 2 additions & 2 deletions Client/src/Common/ArmoniK.DevelopmentKit.Client.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
</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.18.0" />
<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" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="8.0.0" />
Expand Down
46 changes: 35 additions & 11 deletions Client/src/Common/Properties.cs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -332,4 +341,19 @@ public string ConnectionString
/// Max backoff for retries
/// </summary>
public TimeSpan RetryMaxBackoff { get; } = TimeSpan.FromSeconds(30);

/// <summary>
/// Proxy URL
/// </summary>
public string Proxy { get; set; }

/// <summary>
/// Username for the proxy
/// </summary>
public string ProxyUsername { get; set; }

/// <summary>
/// Password for the proxy
/// </summary>
public string ProxyPassword { get; set; }
}
119 changes: 45 additions & 74 deletions Client/src/Common/Submitter/ChannelPool.cs
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;

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

[CanBeNull]
private readonly ILogger<ChannelPool> logger_;
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,
[CanBeNull] ILoggerFactory loggerFactory = null)
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 @@ -81,10 +77,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 @@ -104,47 +100,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 false;
#endif
}
catch (InvalidOperationException)
{
Expand All @@ -165,20 +144,10 @@ 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)
{
using var channel = GetChannel();
return f(channel.Channel);
}

/// <summary>
/// Call f with an acquired channel
/// </summary>
/// <param name="f">Function to be called</param>
public void WithChannel(Action<ChannelBase> f)
public T WithChannel<T>(Func<GrpcChannel, T> f)
{
using var channel = GetChannel();
f(channel.Channel);
return f(channel);
}

/// <summary>
Expand All @@ -189,7 +158,9 @@ public sealed class ChannelGuard : IDisposable
/// <summary>
/// Channel that is used by nobody else
/// </summary>
public readonly ChannelBase Channel;
[SuppressMessage("Usage",
"CA2213:Disposable fields should be disposed")]
private readonly GrpcChannel channel_;

private readonly ChannelPool pool_;

Expand All @@ -199,20 +170,20 @@ public sealed class ChannelGuard : IDisposable
/// <param name="channelPool"></param>
public ChannelGuard(ChannelPool channelPool)
{
pool_ = channelPool;
Channel = channelPool.AcquireChannel();
pool_ = channelPool;
channel_ = channelPool.AcquireChannel();
}

/// <inheritdoc />
public void Dispose()
=> pool_.ReleaseChannel(Channel);
=> pool_.ReleaseChannel(channel_);

/// <summary>
/// Implicit convert a ChannelGuard into a ChannelBase
/// </summary>
/// <param name="guard">ChannelGuard</param>
/// <returns>ChannelBase</returns>
public static implicit operator ChannelBase(ChannelGuard guard)
=> guard.Channel;
/// <returns>GrpcChannel</returns>
public static implicit operator GrpcChannel(ChannelGuard guard)
=> guard.channel_;
}
}
24 changes: 5 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,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))));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ArmoniK.Api.Common" Version="3.17.0" />
<PackageReference Include="ArmoniK.Api.Common" Version="3.18.0" />
<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
6 changes: 3 additions & 3 deletions Worker/src/Common/ArmoniK.DevelopmentKit.Worker.Common.csproj
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0</TargetFrameworks>
<TargetFrameworks>net6.0;net8.0</TargetFrameworks>
<OutputType>Library</OutputType>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<IsPackable>true</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ArmoniK.Api.Worker" Version="3.17.0" />
<PackageReference Include="ArmoniK.Api.Worker" Version="3.18.0" />
<PackageReference Include="AWSSDK.S3" Version="3.7.106.1" />
<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
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0</TargetFrameworks>
<TargetFrameworks>net6.0;net8.0</TargetFrameworks>
<OutputType>Library</OutputType>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<IsPackable>true</IsPackable>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0</TargetFrameworks>
<TargetFrameworks>net6.0;net8.0</TargetFrameworks>
<OutputType>Library</OutputType>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<IsPackable>true</IsPackable>
Expand Down

0 comments on commit 66eddc0

Please sign in to comment.