diff --git a/ManagedPool/Pool_T_.cs b/ManagedPool/Pool_T_.cs index 7da4466..fb4e65d 100644 --- a/ManagedPool/Pool_T_.cs +++ b/ManagedPool/Pool_T_.cs @@ -7,22 +7,24 @@ using System.Threading.Tasks; namespace eu.railduction.netcore.dll.ManagedPool { public class Pool where T : class - { + { + private ConcurrentStack pool = new ConcurrentStack(); + private int current = 0, max = 100, min = 10, batch = 5, increaseBuffer = 5, decreaseBuffer = 10; + private Func creator; + private Action destructor; + private Func healthChecker = (T) => { return true; }; + + #region Attribute declaration /// Method to handle item-spawning (needs to return the item-type) public Func Creator { get => creator; set => creator = value; } - private Func healthChecker = (T) => { return true; }; - /// Method to handle item-health-checking (useful for unstable items) - /// While running the health-check, the Thread will be blocked! - public Func HealthChecker { get => healthChecker; set => healthChecker = value; } - - private Action destructor; /// Method to handle item-deleting/killing (needs to take the item-type as parameter) public Action Destructor { get => destructor; set => destructor = value; } - private ConcurrentStack pool = new ConcurrentStack(); - private int current = 0, max = 100, min = 10, batch = 5, increaseBuffer = 5, decreaseBuffer = 10; + /// Method to handle item-health-checking (useful for unstable items) + /// While running the health-check, the Thread will be blocked! + public Func HealthChecker { get => healthChecker; set => healthChecker = value; } /// The current amount of items in the pool public int Current { get => min; } @@ -39,6 +41,7 @@ namespace eu.railduction.netcore.dll.ManagedPool public int IncreaseBuffer { get => increaseBuffer; set => increaseBuffer = value; } /// If more items are in the pool that the buffer, it will delete/kill the overflowing items public int DecreaseBuffer { get => decreaseBuffer; set => decreaseBuffer = value; } + #endregion /// /// Creates a managed pool @@ -69,37 +72,12 @@ namespace eu.railduction.netcore.dll.ManagedPool Task.Run(() => createNewObjects(min)); } - /// - /// Creates a managed pool - /// Will automatically spawn items - /// The health-check is run before an item is used for execution - /// - /// The minimum amount of items in the pool - /// The maximum amount of items in the pool - /// Amount if items being spawned at a time if needed - /// If less items are in the pool than the , it will spawn -amount - /// If more items are in the pool than the , it will delete/kill the overflowing items - /// Method to handle item-spawning - /// Method to handle item-deleting/killing /// Method to handle item-health-checking (useful for unstable items) /// While running the health-check, the Thread will be blocked! public Pool(int min, int max, int batch, int increaseBuffer, int decreaseBuffer, Func creator, Action destructor, Func 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; - - // Create the minimum amount of objects - Task.Run(() => createNewObjects(min)); } 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'!"); } - /// - /// 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 - /// - /// The item - public T getItem() + private T getItem() { do{ 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) { // create 'size'-amount of items, as long we dont hit our maximum