Last active
February 11, 2022 13:12
-
-
Save ptupitsyn/343f1439982898c776df2acba60c5d50 to your computer and use it in GitHub Desktop.
Apache Ignite.NET Thin Client Pool
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Apache.Ignite.Core; | |
using Apache.Ignite.Core.Client; | |
using Microsoft.Extensions.ObjectPool; | |
namespace IgniteThinClientConnectionPool; | |
public class IgniteClientPool | |
{ | |
private const int ReconnectRetryLimit = 3; | |
private const int NewInstanceRetryLimit = 10; | |
private readonly ObjectPool<IIgniteClient> _pool = new DefaultObjectPool<IIgniteClient>(new Policy(), 10); // Adjust pool size as needed. | |
public IIgniteClient Rent() | |
{ | |
var errors = new List<Exception>(); | |
for (int i = 0; i < NewInstanceRetryLimit; i++) | |
{ | |
var client = _pool.Get(); | |
for (int j = 0; j < ReconnectRetryLimit; j++) | |
{ | |
try | |
{ | |
// Perform trivial operation to check connection and trigger reconnect. | |
client.GetCluster().IsActive(); | |
// Connection is alive. | |
return client; | |
} | |
catch (Exception e) | |
{ | |
// Retry. | |
errors.Add(e); | |
} | |
} | |
// Current instance is beyond repair - try next. | |
} | |
throw new AggregateException("Cluster connection failed.", errors); | |
} | |
public void Return(IIgniteClient client) | |
{ | |
_pool.Return(client); | |
} | |
private class Policy : IPooledObjectPolicy<IIgniteClient> | |
{ | |
public IIgniteClient Create() | |
{ | |
return Ignition.StartClient(new IgniteClientConfiguration("127.0.0.1")); | |
} | |
public bool Return(IIgniteClient obj) | |
{ | |
return true; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Project Sdk="Microsoft.NET.Sdk"> | |
<PropertyGroup> | |
<OutputType>Exe</OutputType> | |
<TargetFramework>net6.0</TargetFramework> | |
<ImplicitUsings>enable</ImplicitUsings> | |
<Nullable>enable</Nullable> | |
</PropertyGroup> | |
<ItemGroup> | |
<PackageReference Include="Apache.Ignite" Version="2.12.0" /> | |
<PackageReference Include="Microsoft.Extensions.ObjectPool" Version="6.0.1" /> | |
</ItemGroup> | |
</Project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment