Class ScyllaGenericObjectPool<T>.Builder
Fluent builder for constructing ScyllaGenericObjectPool instances with complex configurations. Supports capacity, max size, creation function, callbacks (onGet, onRelease, onDestroy), collection checking, automatic creation, disposal on destroy, and synchronized wrapper creation.
Inherited Members
Namespace: Scylla.Core.Structures
Assembly: ScyllaCore.dll
Syntax
public sealed class ScyllaGenericObjectPool<T>.Builder
Remarks
The builder provides a discoverable API for configuring object pools without relying on positional parameters. Use Build() to obtain a configured pool (returns interface for synchronized wrappers). Use BuildConcrete() to obtain the concrete unsynchronized pool type (throws if synchronized).
// Basic usage with defaults
var pool = ScyllaGenericObjectPool<MyClass>.CreateBuilder().Build();
// Configure capacity and max size
var pool = ScyllaGenericObjectPool<MyClass>.CreateBuilder()
.WithCapacity(64)
.WithMaxSize(200)
.Build();
// Custom creation function and callbacks
var pool = ScyllaGenericObjectPool<MyClass>.CreateBuilder()
.WithCreateFunc(() => new MyClass())
.OnGet(obj => obj.Reset())
.OnRelease(obj => obj.Cleanup())
.Build();
// Create synchronized (thread-safe) wrapper
var pool = ScyllaGenericObjectPool<MyClass>.CreateBuilder()
.WithCapacity(128)
.Synchronized()
.Build();
Methods
AllowCreate(bool)
Enables automatic creation of new instances when the pool is empty. When enabled, Get() and TryGet(out T) create new instances when the pool is empty. When disabled, TryGet(out T) returns false when empty.
Declaration
public ScyllaGenericObjectPool<T>.Builder AllowCreate(bool value = true)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | value | True to enable automatic creation, false to disable (default: true) |
Returns
| Type | Description |
|---|---|
| ScyllaGenericObjectPool<T>.Builder | This builder instance for method chaining |
Build()
Builds and returns the configured ScyllaGenericObjectPool instance or synchronized wrapper. If Synchronized(object) was called, returns a ScyllaGenericObjectPool<T>.SynchronizedScyllaGenericObjectPool wrapper. Otherwise, returns the unsynchronized ScyllaGenericObjectPool<T>.
Declaration
public IScyllaCollection<T> Build()
Returns
| Type | Description |
|---|---|
| IScyllaCollection<T> | An IScyllaCollection<T> representing either the unsynchronized pool or synchronized wrapper |
BuildConcrete()
Builds and returns the concrete unsynchronized ScyllaGenericObjectPool<T> instance. This method provides access to the concrete type instead of the interface.
Declaration
public ScyllaGenericObjectPool<T> BuildConcrete()
Returns
| Type | Description |
|---|---|
| ScyllaGenericObjectPool<T> | The configured unsynchronized ScyllaGenericObjectPool instance |
Exceptions
| Type | Condition |
|---|---|
| InvalidOperationException | Thrown when Synchronized(object) was called. Use Build() to obtain the synchronized wrapper. |
CollectionCheck(bool)
Enables collection checking, which validates that instances are not released to the pool twice. When enabled, Release(T) checks whether the instance is already in the pool (O(n)). Useful for debugging; disable for maximum performance.
Declaration
public ScyllaGenericObjectPool<T>.Builder CollectionCheck(bool value = true)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | value | True to enable collection checking, false to disable (default: true) |
Returns
| Type | Description |
|---|---|
| ScyllaGenericObjectPool<T>.Builder | This builder instance for method chaining |
DisposeOnDestroy(bool)
Enables automatic disposal of instances implementing IDisposable when they are destroyed/forgotten. When enabled, Dispose() is called before instances are removed from the pool.
Declaration
public ScyllaGenericObjectPool<T>.Builder DisposeOnDestroy(bool value = true)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | value | True to enable disposal on destroy, false to disable (default: true) |
Returns
| Type | Description |
|---|---|
| ScyllaGenericObjectPool<T>.Builder | This builder instance for method chaining |
OnDestroy(Action<T>)
Sets a callback invoked before an instance is destroyed/forgotten. Useful for custom cleanup logic when instances are removed from the pool.
Declaration
public ScyllaGenericObjectPool<T>.Builder OnDestroy(Action<T> onDestroy)
Parameters
| Type | Name | Description |
|---|---|---|
| Action<T> | onDestroy | The callback to invoke, or null to disable |
Returns
| Type | Description |
|---|---|
| ScyllaGenericObjectPool<T>.Builder | This builder instance for method chaining |
OnGet(Action<T>)
Sets a callback invoked after an instance is retrieved from the pool. Useful for resetting instance state before use.
Declaration
public ScyllaGenericObjectPool<T>.Builder OnGet(Action<T> onGet)
Parameters
| Type | Name | Description |
|---|---|---|
| Action<T> | onGet | The callback to invoke, or null to disable |
Returns
| Type | Description |
|---|---|
| ScyllaGenericObjectPool<T>.Builder | This builder instance for method chaining |
OnRelease(Action<T>)
Sets a callback invoked before an instance is stored back into the pool. Useful for cleaning up instance state before returning to the pool.
Declaration
public ScyllaGenericObjectPool<T>.Builder OnRelease(Action<T> onRelease)
Parameters
| Type | Name | Description |
|---|---|---|
| Action<T> | onRelease | The callback to invoke, or null to disable |
Returns
| Type | Description |
|---|---|
| ScyllaGenericObjectPool<T>.Builder | This builder instance for method chaining |
Synchronized(object)
Configures the builder to return a synchronized (thread-safe) wrapper around the pool. All operations on the synchronized wrapper will be protected by a lock.
Declaration
public ScyllaGenericObjectPool<T>.Builder Synchronized(object syncRoot = null)
Parameters
| Type | Name | Description |
|---|---|---|
| object | syncRoot | Optional external lock object to use for synchronization. If null, the wrapper creates and owns a private lock. Provide a custom sync root to coordinate synchronization across multiple synchronized collections. |
Returns
| Type | Description |
|---|---|
| ScyllaGenericObjectPool<T>.Builder | This builder instance for method chaining |
WithCapacity(int)
Sets the initial capacity of the free-list buffer.
Declaration
public ScyllaGenericObjectPool<T>.Builder WithCapacity(int capacity)
Parameters
| Type | Name | Description |
|---|---|---|
| int | capacity | The initial capacity. Must be at least 1. If less than 1, will be clamped to 1 during construction. |
Returns
| Type | Description |
|---|---|
| ScyllaGenericObjectPool<T>.Builder | This builder instance for method chaining |
Exceptions
| Type | Condition |
|---|---|
| ArgumentOutOfRangeException | Thrown when capacity is less than 1 |
WithCreateFunc(Func<T>)
Sets a custom creation function used when the pool is empty and automatic creation is enabled. If not set and automatic creation is enabled, the pool will use CreateInstance<T>() (requires a public parameterless constructor for reference types).
Declaration
public ScyllaGenericObjectPool<T>.Builder WithCreateFunc(Func<T> createFunc)
Parameters
| Type | Name | Description |
|---|---|---|
| Func<T> | createFunc | The function to create new instances, or null to use the default activator. |
Returns
| Type | Description |
|---|---|
| ScyllaGenericObjectPool<T>.Builder | This builder instance for method chaining |
WithMaxSize(int)
Sets the maximum number of available instances that can be stored in the pool. When exceeded, released instances are destroyed/forgotten instead of being stored.
Declaration
public ScyllaGenericObjectPool<T>.Builder WithMaxSize(int maxSize)
Parameters
| Type | Name | Description |
|---|---|---|
| int | maxSize | The maximum size. Must be at least 0. If negative, will be clamped to 0 during construction. Default is MaxValue (unlimited). |
Returns
| Type | Description |
|---|---|
| ScyllaGenericObjectPool<T>.Builder | This builder instance for method chaining |
Exceptions
| Type | Condition |
|---|---|
| ArgumentOutOfRangeException | Thrown when maxSize is negative |