frostfs-sdk-csharp/src/FrostFS.SDK.ClientV2/Tools/ClientEnvironment.cs
Pavel Gross 85506a997e
Some checks failed
DCO / DCO (pull_request) Failing after 44s
[#10] Netmap metrics and options
2024-06-24 12:12:53 +03:00

116 lines
3.2 KiB
C#

using System.Security.Cryptography;
using Grpc.Net.Client;
using FrostFS.SDK.ModelsV2;
using System;
using System.Threading.Tasks;
using Grpc.Core;
using System.Diagnostics;
namespace FrostFS.SDK.ClientV2;
public class ClientEnvironment(ECDsa key, OwnerId owner, GrpcChannel channel, ModelsV2.Version version) : IDisposable
{
internal OwnerId Owner { get; } = owner;
internal GrpcChannel Channel { get; private set; } = channel;
internal ECDsa Key { get; } = key;
internal ModelsV2.Version Version { get; } = version;
internal NetworkSettings? NetworkSettings { get; set; }
internal ContainerServiceProvider? ContainerService { get; set; }
internal NetmapServiceProvider? NetmapService { get; set; }
internal SessionServiceProvider? SessionService { get; set; }
internal ObjectServiceProvider? ObjectService { get; set; }
internal event EventHandler<GrpcCallInfo>? GrpcInvokedEvent;
internal bool IsEventRequested => GrpcInvokedEvent != null;
internal async Task<T> InvokeAsyncWithMetrics<T>(Func<Task<T>> func, string methodName)
{
if (!IsEventRequested)
return await func();
var watch = Stopwatch.StartNew();
bool exitWithException = false;
try
{
return await func();
}
catch
{
exitWithException = true;
throw;
}
finally
{
watch.Stop();
GrpcInvokedEvent?.Invoke(this, new GrpcCallInfo(methodName, watch.ElapsedTicks * 1_000_000 / Stopwatch.Frequency, exitWithException));
}
}
internal T InvokeWithMetrics<T>(Func<T> func, string methodName)
{
if (!IsEventRequested)
return func();
var watch = Stopwatch.StartNew();
bool exitWithException = false;
try
{
return func();
}
catch
{
exitWithException = true;
throw;
}
finally
{
watch.Stop();
GrpcInvokedEvent?.Invoke(this, new GrpcCallInfo(methodName, watch.ElapsedTicks * 1_000_000 / Stopwatch.Frequency, exitWithException));
}
}
internal async Task<T> InvokeAsyncUnaryWithMetrics<T>(Func<AsyncUnaryCall<T>> func, string methodName)
{
if (!IsEventRequested)
return await func();
var watch = Stopwatch.StartNew();
bool exitWithException = false;
try
{
return await func();
}
catch
{
exitWithException = true;
throw;
}
finally
{
watch.Stop();
GrpcInvokedEvent?.Invoke(this, new GrpcCallInfo(methodName, watch.ElapsedTicks * 1_000_000 / Stopwatch.Frequency, exitWithException));
}
}
internal bool Initialized =>
ContainerService != null
&& NetmapService != null
&& SessionService != null
&& ObjectService != null;
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
Channel.Dispose();
}
}
}