EsoxSolutions.ObjectPool
2.1.0
dotnet add package EsoxSolutions.ObjectPool --version 2.1.0
NuGet\Install-Package EsoxSolutions.ObjectPool -Version 2.1.0
<PackageReference Include="EsoxSolutions.ObjectPool" Version="2.1.0" />
<PackageVersion Include="EsoxSolutions.ObjectPool" Version="2.1.0" />
<PackageReference Include="EsoxSolutions.ObjectPool" />
paket add EsoxSolutions.ObjectPool --version 2.1.0
#r "nuget: EsoxSolutions.ObjectPool, 2.1.0"
#:package EsoxSolutions.ObjectPool@2.1.0
#addin nuget:?package=EsoxSolutions.ObjectPool&version=2.1.0
#tool nuget:?package=EsoxSolutions.ObjectPool&version=2.1.0
EsoxSolutions.ObjectPool
Overview
EsoxSolutions.ObjectPool is a high-performance, thread-safe object pool for .NET 8+ and .NET 9. It supports automatic return of objects, async operations, performance metrics, and flexible configuration. Useful for pooling expensive resources like database connections, network clients, or reusable buffers.
Features
- Thread-safe object pooling
- Automatic return of objects (via IDisposable)
- Async support (
GetObjectAsync
,TryGetObjectAsync
) - Queryable and dynamic pools
- Health monitoring and status
- Performance metrics (exportable, Prometheus format)
- Pool configuration (max size, validation, etc.)
- Try* methods for safe retrieval
- Multithreaded and high-performance (Stack/HashSet)
Usage
PoolModel
A generic wrapper for pooled objects. Use Unwrap()
to access the value.
var pool = new ObjectPool<int>(new List<int> { 1, 2, 3 });
using (var model = pool.GetObject())
{
var value = model.Unwrap();
Console.WriteLine(value);
}
ObjectPool
Administers a fixed set of objects. Throws if pool is empty.
var initialObjects = new List<int> { 1, 2, 3 };
var pool = new ObjectPool<int>(initialObjects);
using (var model = pool.GetObject())
{
Console.WriteLine(model.Unwrap());
}
Async Usage
using (var model = await pool.GetObjectAsync())
{
Console.WriteLine(model.Unwrap());
}
Try Methods
if (pool.TryGetObject(out var model))
{
using (model)
{
Console.WriteLine(model.Unwrap());
}
}
Pool Configuration
var config = new PoolConfiguration {
MaxPoolSize = 5,
MaxActiveObjects = 3,
ValidateOnReturn = true,
ValidationFunction = obj => obj != null
};
var pool = new ObjectPool<int>(initialObjects, config);
QueryableObjectPool
Query for objects matching a predicate.
var pool = new QueryableObjectPool<int>(new List<int> { 1, 2, 3 });
using (var model = pool.GetObject(x => x == 2))
{
Console.WriteLine(model.Unwrap());
}
DynamicObjectPool
Creates objects on the fly using a factory method.
var pool = new DynamicObjectPool<int>(() => 42);
using (var model = pool.GetObject())
{
Console.WriteLine(model.Unwrap());
}
Health Monitoring & Metrics
var health = pool.GetHealthStatus();
Console.WriteLine($"Healthy: {health.IsHealthy}, Warnings: {health.WarningCount}");
var metrics = pool.ExportMetrics();
foreach (var kv in metrics)
Console.WriteLine($"{kv.Key}: {kv.Value}");
Version history:
- 2.1.0: Added PoolConfiguration, improved health checks, fixed async disposal for the queryable pool. Improved thread-safety for all pools.
- 2.0.0: Async support, performance metrics, Try* methods, improved performance
- 1.1.5: Improved thread-safety, dynamic pool throws if no match
- 1.1.3: Added DynamicObjectPool
- 1.1.2: Improved threadsafety
- 1.1.1: Added QueryableObjectPool
Future work
- Timeout/disposal for idle objects
- More advanced health checks
- Integration with dependency injection
For bug reports or suggestions, contact info@esoxsolutions.nl
Disclaimer
Use of this software is at your own risk. The author(s) of EsoxSolutions.ObjectPool are not liable for any damages, losses, or other consequences resulting from the use, misuse, or inability to use this software.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.1)
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.1)
- System.Diagnostics.DiagnosticSource (>= 8.0.1)
-
net9.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.1)
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.1)
- System.Diagnostics.DiagnosticSource (>= 8.0.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
version 2.0: Added async support, performance metrics, Try* methods, improved performance with Stack/HashSet