From 3a6990d17f1a0cae99a0a7cca1fc8229772ed922 Mon Sep 17 00:00:00 2001 From: Alexander B Date: Mon, 21 Oct 2019 09:54:56 +0200 Subject: [PATCH] Added null-check for destructor and health-checker Added null-check for creator in validate Simlivied constructor using default values --- ManagedPool/Pool_T_.cs | 42 +++++++++--------------------------------- 1 file changed, 9 insertions(+), 33 deletions(-) diff --git a/ManagedPool/Pool_T_.cs b/ManagedPool/Pool_T_.cs index 7da4466..663a6fd 100644 --- a/ManagedPool/Pool_T_.cs +++ b/ManagedPool/Pool_T_.cs @@ -40,35 +40,6 @@ namespace eu.railduction.netcore.dll.ManagedPool /// 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; } - /// - /// Creates a managed pool - /// Will automatically spawn items - /// - /// 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 (needs to return the item-type) - /// Method to handle item-deleting/killing (needs to take the item-type as parameter) - public Pool(int min, int max, int batch, int increaseBuffer, int decreaseBuffer, Func creator, Action 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)); - } - /// /// Creates a managed pool /// Will automatically spawn items @@ -83,7 +54,7 @@ namespace eu.railduction.netcore.dll.ManagedPool /// 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) + public Pool(int min, int max, int batch, int increaseBuffer, int decreaseBuffer, Func creator, Action destructor = null, Func healthChecker = null) { this.min = min; 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 (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(creator == null) throw new InvalidOperationException($"Invalid parameter creator. Cannot be null!"); } /// @@ -133,7 +105,11 @@ namespace eu.railduction.netcore.dll.ManagedPool 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 } @@ -218,8 +194,8 @@ namespace eu.railduction.netcore.dll.ManagedPool { // decrease counter current--; - // Destruct it - destructor(item); + // Destruct it when we have a descructor (otherwise the garbage-collector will take care) + if(destructor != null) destructor(item); } ///