From 309da998f23294047c44506cbf5b92530cce895c Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Tue, 18 Mar 2025 14:33:33 +1300 Subject: [PATCH 1/2] Revert connection health --- composer.lock | 4 +-- src/Pools/Connection.php | 25 ++------------- src/Pools/Pool.php | 13 ++------ tests/Pools/PoolTest.php | 67 ---------------------------------------- 4 files changed, 6 insertions(+), 103 deletions(-) diff --git a/composer.lock b/composer.lock index e5678dd..bf1125a 100644 --- a/composer.lock +++ b/composer.lock @@ -3646,13 +3646,13 @@ ], "aliases": [], "minimum-stability": "stable", - "stability-flags": [], + "stability-flags": {}, "prefer-stable": false, "prefer-lowest": false, "platform": { "php": ">=8.3" }, - "platform-dev": [], + "platform-dev": {}, "platform-overrides": { "php": "8.3" }, diff --git a/src/Pools/Connection.php b/src/Pools/Connection.php index f121366..eaeec6f 100644 --- a/src/Pools/Connection.php +++ b/src/Pools/Connection.php @@ -10,14 +10,11 @@ class Connection protected ?Pool $pool = null; - protected bool $healthy = true; - /** * @param mixed $resource */ - public function __construct( - protected mixed $resource, - ) { + public function __construct(protected mixed $resource) + { } /** @@ -97,22 +94,4 @@ public function destroy(): Pool return $this->pool->destroy($this); } - - /** - * @param bool $healthy - * @return self - */ - public function setHealthy(bool $healthy): self - { - $this->healthy = $healthy; - return $this; - } - - /** - * @return bool - */ - public function isHealthy(): bool - { - return $this->healthy; - } } diff --git a/src/Pools/Pool.php b/src/Pools/Pool.php index 8336998..a04f990 100644 --- a/src/Pools/Pool.php +++ b/src/Pools/Pool.php @@ -305,21 +305,12 @@ public function count(): int public function reclaim(Connection $connection = null): self { if ($connection !== null) { - if ($connection->isHealthy()) { - $this->push($connection); - } else { - $this->destroy($connection); - } + $this->push($connection); return $this; } foreach ($this->active as $connection) { - if ($connection->isHealthy()) { - $this->push($connection); - continue; - } - - $this->destroy($connection); + $this->push($connection); } return $this; diff --git a/tests/Pools/PoolTest.php b/tests/Pools/PoolTest.php index c43b842..4a44891 100644 --- a/tests/Pools/PoolTest.php +++ b/tests/Pools/PoolTest.php @@ -258,71 +258,4 @@ public function testDestroy(): void $this->assertEquals('y', $connection1->getResource()); $this->assertEquals('y', $connection2->getResource()); } - - public function testUnhealthyConnectionsPurged(): void - { - $object = new Pool('testDestroy', 2, function () { - return 'x'; - }); - - $this->assertEquals(2, $object->count()); - - $connection1 = $object->pop(); - $connection2 = $object->pop(); - - $connection1Id = $connection1->getId(); - $connection2Id = $connection2->getId(); - - $this->assertEquals(0, $object->count()); - - $this->assertEquals('x', $connection1->getResource()); - $this->assertEquals('x', $connection2->getResource()); - - $connection1->setHealthy(false); - - $object->reclaim(); - - $this->assertEquals(2, $object->count()); - - $connection2 = $object->pop(); - $connection1 = $object->pop(); - - $this->assertNotEquals($connection1Id, $connection1->getId()); - $this->assertEquals($connection2Id, $connection2->getId()); - } - - public function testTelemetry(): void - { - $telemetry = new TestTelemetry(); - $this->object->setTelemetry($telemetry); - - $allocate = function (int $amount, callable $assertion) { - $connections = []; - for ($i = 0; $i < $amount; $i++) { - $connections[] = $this->object->pop(); - } - - $assertion(); - - foreach ($connections as $connection) { - $this->object->reclaim($connection); - } - }; - - $this->assertEquals(5, $this->object->count()); - - $allocate(3, function () use ($telemetry) { - $this->assertEquals([1, 2, 3], $telemetry->gauges['pool.connection.open.count']->values); - $this->assertEquals([1, 2, 3], $telemetry->gauges['pool.connection.active.count']->values); - $this->assertEquals([0, 0, 0], $telemetry->gauges['pool.connection.idle.count']->values); - }); - - $this->assertEquals(5, $this->object->count()); - - $allocate(1, function () use ($telemetry) { - $this->assertEquals([1, 2, 3, 3, 3, 3, 3], $telemetry->gauges['pool.connection.open.count']->values); - $this->assertEquals([1, 2, 3, 2, 1, 0, 1], $telemetry->gauges['pool.connection.active.count']->values); - $this->assertEquals([0, 0, 0, 1, 2, 3, 2], $telemetry->gauges['pool.connection.idle.count']->values); - }); - } } From c77456dd916f0f3ff81959844497f76ff84093ee Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Tue, 18 Mar 2025 14:39:23 +1300 Subject: [PATCH 2/2] Add back test --- tests/Pools/PoolTest.php | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/tests/Pools/PoolTest.php b/tests/Pools/PoolTest.php index 4a44891..d840ea4 100644 --- a/tests/Pools/PoolTest.php +++ b/tests/Pools/PoolTest.php @@ -258,4 +258,39 @@ public function testDestroy(): void $this->assertEquals('y', $connection1->getResource()); $this->assertEquals('y', $connection2->getResource()); } + + public function testTelemetry(): void + { + $telemetry = new TestTelemetry(); + $this->object->setTelemetry($telemetry); + + $allocate = function (int $amount, callable $assertion) { + $connections = []; + for ($i = 0; $i < $amount; $i++) { + $connections[] = $this->object->pop(); + } + + $assertion(); + + foreach ($connections as $connection) { + $this->object->reclaim($connection); + } + }; + + $this->assertEquals(5, $this->object->count()); + + $allocate(3, function () use ($telemetry) { + $this->assertEquals([1, 2, 3], $telemetry->gauges['pool.connection.open.count']->values); + $this->assertEquals([1, 2, 3], $telemetry->gauges['pool.connection.active.count']->values); + $this->assertEquals([0, 0, 0], $telemetry->gauges['pool.connection.idle.count']->values); + }); + + $this->assertEquals(5, $this->object->count()); + + $allocate(1, function () use ($telemetry) { + $this->assertEquals([1, 2, 3, 3, 3, 3, 3], $telemetry->gauges['pool.connection.open.count']->values); + $this->assertEquals([1, 2, 3, 2, 1, 0, 1], $telemetry->gauges['pool.connection.active.count']->values); + $this->assertEquals([0, 0, 0, 1, 2, 3, 2], $telemetry->gauges['pool.connection.idle.count']->values); + }); + } }