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

fix: remove gRPC Core #261

Merged
merged 5 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.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" />
Expand Down
28 changes: 26 additions & 2 deletions Client/src/Common/Properties.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// 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.
Expand Down Expand Up @@ -50,6 +50,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 @@ -137,7 +140,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 @@ -186,6 +192,9 @@ public Properties(IConfiguration configuration,
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 @@ -369,4 +378,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; }
}
85 changes: 34 additions & 51 deletions Client/src/Common/Submitter/ChannelPool.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// 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.
Expand All @@ -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 false;
#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_;
}
}
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 @@ -47,25 +45,13 @@ public static ChannelPool ControlPlaneConnectionPool(Properties 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 @@ -12,7 +12,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,20 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net8.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" />
<!-- 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
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net8.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>net8.0</TargetFrameworks>
<TargetFrameworks>net6.0;net8.0</TargetFrameworks>
<OutputType>Library</OutputType>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<IsPackable>true</IsPackable>
Expand Down
Loading