frostfs-sdk-csharp/src/FrostFS.SDK.ClientV2/Poll/InnerPool.cs
Pavel Gross ee20798379 [#24] Client: Implement pool part2
Signed-off-by: Pavel Gross <p.gross@yadro.com>
2024-11-01 10:30:28 +03:00

47 lines
1 KiB
C#

using FrostFS.SDK.ClientV2;
internal sealed class InnerPool
{
private readonly object _lock = new();
internal InnerPool(Sampler sampler, ClientWrapper[] clients)
{
Clients = clients;
Sampler = sampler;
}
internal Sampler Sampler { get; set; }
internal ClientWrapper[] Clients { get; }
internal ClientWrapper? Connection()
{
lock (_lock)
{
if (Clients.Length == 1)
{
var client = Clients[0];
if (client.IsHealthy())
{
return client;
}
}
else
{
var attempts = 3 * Clients.Length;
for (int i = 0; i < attempts; i++)
{
int index = Sampler.Next();
if (Clients[index].IsHealthy())
{
return Clients[index];
}
}
}
return null;
}
}
}