@ -40,35 +40,6 @@ namespace eu.railduction.netcore.dll.ManagedPool
/// <summary>If more items are in the pool that the buffer, it will delete/kill the overflowing items</summary>
/// <summary>If more items are in the pool that the buffer, it will delete/kill the overflowing items</summary>
public int DecreaseBuffer { get = > decreaseBuffer ; set = > decreaseBuffer = value ; }
public int DecreaseBuffer { get = > decreaseBuffer ; set = > decreaseBuffer = value ; }
/// <summary>
/// Creates a managed pool<para/>
/// Will automatically spawn <paramref name="min"/> items
/// </summary>
/// <param name="min">The minimum amount of items in the pool</param>
/// <param name="max">The maximum amount of items in the pool</param>
/// <param name="batch">Amount if items being spawned at a time if needed</param>
/// <param name="increaseBuffer">If less items are in the pool than the <paramref name="increaseBuffer"/>, it will spawn <paramref name="batch"/>-amount</param>
/// <param name="decreaseBuffer">If more items are in the pool than the <paramref name="decreaseBuffer"/>, it will delete/kill the overflowing items</param>
/// <param name="creator">Method to handle item-spawning (needs to return the item-type)</param>
/// <param name="destructor">Method to handle item-deleting/killing (needs to take the item-type as parameter)</param>
public Pool ( int min , int max , int batch , int increaseBuffer , int decreaseBuffer , Func < T > creator , Action < T > destructor )
{
this . min = min ;
this . max = max ;
this . batch = batch ;
this . increaseBuffer = increaseBuffer ;
this . decreaseBuffer = decreaseBuffer ;
// Validate objects state
validate ( ) ;
this . creator = creator ;
this . destructor = destructor ;
// Create the minimum amount of objects
Task . Run ( ( ) = > createNewObjects ( min ) ) ;
}
/// <summary>
/// <summary>
/// Creates a managed pool<para/>
/// Creates a managed pool<para/>
/// Will automatically spawn <paramref name="min"/> items<para/>
/// Will automatically spawn <paramref name="min"/> items<para/>
@ -83,7 +54,7 @@ namespace eu.railduction.netcore.dll.ManagedPool
/// <param name="destructor">Method to handle item-deleting/killing</param>
/// <param name="destructor">Method to handle item-deleting/killing</param>
/// <param name="healthChecker">Method to handle item-health-checking (useful for unstable items)
/// <param name="healthChecker">Method to handle item-health-checking (useful for unstable items)
/// <para/>While running the health-check, the Thread will be blocked!</param>
/// <para/>While running the health-check, the Thread will be blocked!</param>
public Pool ( int min , int max , int batch , int increaseBuffer , int decreaseBuffer , Func < T > creator , Action < T > destructor , Func < T , bool > healthChecker )
public Pool ( int min , int max , int batch , int increaseBuffer , int decreaseBuffer , Func < T > creator , Action < T > destructor = null , Func < T , bool > healthChecker = null )
{
{
this . min = min ;
this . min = min ;
this . max = max ;
this . max = max ;
@ -109,6 +80,7 @@ namespace eu.railduction.netcore.dll.ManagedPool
if ( batch < = 0 ) throw new InvalidOperationException ( $"Invalid parameter batch='{batch}'. Must be at least '1'!" ) ;
if ( batch < = 0 ) throw new InvalidOperationException ( $"Invalid parameter batch='{batch}'. Must be at least '1'!" ) ;
if ( increaseBuffer < = 0 ) throw new InvalidOperationException ( $"Invalid parameter increaseBuffer='{increaseBuffer}'. Must be at least '1'!" ) ;
if ( increaseBuffer < = 0 ) throw new InvalidOperationException ( $"Invalid parameter increaseBuffer='{increaseBuffer}'. Must be at least '1'!" ) ;
if ( decreaseBuffer < = 0 ) throw new InvalidOperationException ( $"Invalid parameter decreaseBuffer='{decreaseBuffer}'. Must be at least '1'!" ) ;
if ( decreaseBuffer < = 0 ) throw new InvalidOperationException ( $"Invalid parameter decreaseBuffer='{decreaseBuffer}'. Must be at least '1'!" ) ;
if ( creator = = null ) throw new InvalidOperationException ( $"Invalid parameter creator. Cannot be null!" ) ;
}
}
/// <summary>
/// <summary>
@ -133,7 +105,11 @@ namespace eu.railduction.netcore.dll.ManagedPool
if ( popped )
if ( popped )
{
{
if ( healthChecker ( item ) ) // Check for item-health
// Only run healthChecker if its set
bool healthy = true ;
if ( healthChecker ! = null ) healthy = healthChecker ( item ) ;
if ( healthy ) // Check for item-health
{
{
return item ; // Item is healthy, return it
return item ; // Item is healthy, return it
}
}
@ -218,8 +194,8 @@ namespace eu.railduction.netcore.dll.ManagedPool
{
{
// decrease counter
// decrease counter
current - - ;
current - - ;
// Destruct it
// Destruct it when we have a descructor (otherwise the garbage-collector will take care)
destructor ( item ) ;
if ( destructor ! = null ) destructor ( item ) ;
}
}
/// <summary>
/// <summary>