Skip to content

Commit

Permalink
#170: make the behavior of the ConnectionPool::get() methods consiste…
Browse files Browse the repository at this point in the history
…nt without throwing out an exception

When failed to get a connection from the pool, method get() should always return false instead of throwing out any exception. For method get(), returning a value of false means that the pool is at full capacity, yet all connections are currently in use.
  • Loading branch information
deminy committed Mar 1, 2024
1 parent 041533d commit 587127c
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 32 deletions.
1 change: 0 additions & 1 deletion src/__init__.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
'core/StringObject.php',
'core/MultibyteStringObject.php',
'core/Exception/ArrayKeyNotExists.php',
'core/Exception/TimeoutException.php',
'core/ArrayObject.php',
'core/ObjectProxy.php',
'core/Coroutine/WaitGroup.php',
Expand Down
3 changes: 1 addition & 2 deletions src/core/Database/PDOPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

use PDO;
use Swoole\ConnectionPool;
use Swoole\Exception\TimeoutException;

/**
* @method void put(PDO|PDOProxy $connection)
Expand Down Expand Up @@ -44,7 +43,7 @@ public function get(float $timeout = -1)
/* @var \Swoole\Database\PDOProxy|false $pdo */
$pdo = parent::get($timeout);
if ($pdo === false) {
throw new TimeoutException('Failed to get a PDO connection: The pool is at full capacity, yet all connections are currently in use.');
return false;
}

$pdo->reset();
Expand Down
16 changes: 0 additions & 16 deletions src/core/Exception/TimeoutException.php

This file was deleted.

17 changes: 4 additions & 13 deletions tests/unit/Database/PDOPoolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use PHPUnit\Framework\TestCase;
use Swoole\Coroutine;
use Swoole\Coroutine\WaitGroup;
use Swoole\Exception\TimeoutException;
use Swoole\Tests\HookFlagsTrait;

use function Swoole\Coroutine\go;
Expand Down Expand Up @@ -222,8 +221,7 @@ public function testTimeoutException(): void
{
self::saveHookFlags();
self::setHookFlags(SWOOLE_HOOK_ALL);
$failed = false;
run(function () use (&$failed) {
run(function () {
$config = (new PDOConfig())
->withHost(MYSQL_SERVER_HOST)
->withPort(MYSQL_SERVER_PORT)
Expand All @@ -241,22 +239,15 @@ public function testTimeoutException(): void
$waitGroup->done();
});

go(function () use ($pool, $waitGroup, &$failed) {
go(function () use ($pool, $waitGroup) {
Coroutine::sleep(0.1); // Sleep for 0.1 second to ensure the 1st connection is in use by the 1st coroutine.
try {
$pool->get(0.5); // Try to get a 2nd connection from the pool within 0.5 seconds.
} catch (TimeoutException) {
$failed = true;
} finally {
$waitGroup->done();
}
self::assertFalse($pool->get(0.5), 'Failed to get a 2nd connection from the pool within 0.5 seconds');
$waitGroup->done();
});

$waitGroup->wait();
$pool->close();
self::restoreHookFlags();

self::assertTrue($failed, 'Failed to get a 2nd connection from the pool within 0.5 seconds');
});
}
}

0 comments on commit 587127c

Please sign in to comment.