EsoxSolutions.ObjectPool is a high-performance, thread-safe object pool for .NET 8+ and .NET 9. It supports automatic return of objects, async operations, health monitoring, performance metrics, and flexible configuration. Useful for pooling expensive resources like database connections, network clients, or reusable buffers.
- 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)
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);
}
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());
}
using (var model = await pool.GetObjectAsync())
{
Console.WriteLine(model.Unwrap());
}
if (pool.TryGetObject(out var model))
{
using (model)
{
Console.WriteLine(model.Unwrap());
}
}
var config = new PoolConfiguration {
MaxPoolSize = 5,
MaxActiveObjects = 3,
ValidateOnReturn = true,
ValidationFunction = obj => obj != null
};
var pool = new ObjectPool<int>(initialObjects, config);
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());
}
Creates objects on the fly using a factory method.
var pool = new DynamicObjectPool<int>(() => 42);
using (var model = pool.GetObject())
{
Console.WriteLine(model.Unwrap());
}
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}");
- 2.0.0: Async support, performance metrics, Try* methods, Prometheus metrics, 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
- Timeout/disposal for idle objects
- More advanced health checks
- Integration with dependency injection
For bug reports or suggestions, contact info@esoxsolutions.nl
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.