Changed constructor to create from itself.
Added region for attribute declaration. Removed todo. Set getItem() to private.
This commit is contained in:
parent
98c732f6ee
commit
b242795104
@ -7,22 +7,24 @@ using System.Threading.Tasks;
|
|||||||
namespace eu.railduction.netcore.dll.ManagedPool
|
namespace eu.railduction.netcore.dll.ManagedPool
|
||||||
{
|
{
|
||||||
public class Pool<T> where T : class
|
public class Pool<T> where T : class
|
||||||
{
|
{
|
||||||
|
private ConcurrentStack<T> pool = new ConcurrentStack<T>();
|
||||||
|
private int current = 0, max = 100, min = 10, batch = 5, increaseBuffer = 5, decreaseBuffer = 10;
|
||||||
|
|
||||||
private Func<T> creator;
|
private Func<T> creator;
|
||||||
|
private Action<T> destructor;
|
||||||
|
private Func<T, bool> healthChecker = (T) => { return true; };
|
||||||
|
|
||||||
|
#region Attribute declaration
|
||||||
/// <summary>Method to handle item-spawning (needs to return the item-type)</summary>
|
/// <summary>Method to handle item-spawning (needs to return the item-type)</summary>
|
||||||
public Func<T> Creator { get => creator; set => creator = value; }
|
public Func<T> Creator { get => creator; set => creator = value; }
|
||||||
|
|
||||||
private Func<T, bool> healthChecker = (T) => { return true; };
|
|
||||||
/// <summary>Method to handle item-health-checking (useful for unstable items)
|
|
||||||
/// <para/>While running the health-check, the Thread will be blocked!</summary>
|
|
||||||
public Func<T, bool> HealthChecker { get => healthChecker; set => healthChecker = value; }
|
|
||||||
|
|
||||||
private Action<T> destructor;
|
|
||||||
/// <summary>Method to handle item-deleting/killing (needs to take the item-type as parameter)</summary>
|
/// <summary>Method to handle item-deleting/killing (needs to take the item-type as parameter)</summary>
|
||||||
public Action<T> Destructor { get => destructor; set => destructor = value; }
|
public Action<T> Destructor { get => destructor; set => destructor = value; }
|
||||||
|
|
||||||
private ConcurrentStack<T> pool = new ConcurrentStack<T>();
|
/// <summary>Method to handle item-health-checking (useful for unstable items)
|
||||||
private int current = 0, max = 100, min = 10, batch = 5, increaseBuffer = 5, decreaseBuffer = 10;
|
/// <para/>While running the health-check, the Thread will be blocked!</summary>
|
||||||
|
public Func<T, bool> HealthChecker { get => healthChecker; set => healthChecker = value; }
|
||||||
|
|
||||||
/// <summary>The current amount of items in the pool</summary>
|
/// <summary>The current amount of items in the pool</summary>
|
||||||
public int Current { get => min; }
|
public int Current { get => min; }
|
||||||
@ -39,6 +41,7 @@ namespace eu.railduction.netcore.dll.ManagedPool
|
|||||||
public int IncreaseBuffer { get => increaseBuffer; set => increaseBuffer = value; }
|
public int IncreaseBuffer { get => increaseBuffer; set => increaseBuffer = value; }
|
||||||
/// <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; }
|
||||||
|
#endregion
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a managed pool<para/>
|
/// Creates a managed pool<para/>
|
||||||
@ -69,37 +72,12 @@ namespace eu.railduction.netcore.dll.ManagedPool
|
|||||||
Task.Run(() => createNewObjects(min));
|
Task.Run(() => createNewObjects(min));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Creates a managed pool<para/>
|
|
||||||
/// Will automatically spawn <paramref name="min"/> items<para/>
|
|
||||||
/// The health-check is run before an item is used for execution
|
|
||||||
/// </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</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, Func<T, bool> healthChecker)
|
||||||
|
:this(min, max, batch, increaseBuffer, decreaseBuffer, creator, 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;
|
|
||||||
this.healthChecker = healthChecker;
|
this.healthChecker = healthChecker;
|
||||||
|
|
||||||
// Create the minimum amount of objects
|
|
||||||
Task.Run(() => createNewObjects(min));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void validate()
|
private void validate()
|
||||||
@ -111,12 +89,7 @@ namespace eu.railduction.netcore.dll.ManagedPool
|
|||||||
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'!");
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
private T getItem()
|
||||||
/// Will try to get an avaible item out of the pool
|
|
||||||
/// If there is no item avaible, this function will block the thread and retry
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>The item</returns>
|
|
||||||
public T getItem()
|
|
||||||
{
|
{
|
||||||
do{
|
do{
|
||||||
T item;
|
T item;
|
||||||
@ -156,9 +129,6 @@ namespace eu.railduction.netcore.dll.ManagedPool
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Todo [HIGHER]: Add function to put item back into the pool
|
|
||||||
// Check if item was created by pool, reject otherwise
|
|
||||||
|
|
||||||
private void createNewObjects(int size)
|
private void createNewObjects(int size)
|
||||||
{
|
{
|
||||||
// create 'size'-amount of items, as long we dont hit our maximum
|
// create 'size'-amount of items, as long we dont hit our maximum
|
||||||
|
Loading…
x
Reference in New Issue
Block a user