From f788ba63ae963a66af4b9265331fc3d9c205aabd Mon Sep 17 00:00:00 2001 From: Dave Smith-Hayes Date: Wed, 9 Oct 2024 21:20:52 -0400 Subject: [PATCH] Add a connection pool class and start utilizing it. --- app/src/Bootstrap.php | 20 +++++++++++-------- .../Domain/Repository/User/UserRepository.php | 10 +++++++--- .../Database/ConnectionPool.php | 6 +++++- app/src/Routes.php | 4 +++- 4 files changed, 27 insertions(+), 13 deletions(-) diff --git a/app/src/Bootstrap.php b/app/src/Bootstrap.php index f9f1625..8c8138f 100644 --- a/app/src/Bootstrap.php +++ b/app/src/Bootstrap.php @@ -26,7 +26,10 @@ use Slovocast\Configuration\SessionSchema; use Slovocast\Configuration\SiteInformationSchema; use Slovocast\Domain\Repository\User\UserRepository; use Slovocast\Domain\Repository\User\UserRepositoryInterface; +use Slovocast\Infrastructure\Api\Database\ConnectionPoolInterface; use Slovocast\Infrastructure\Api\User\UserAuthorizationInterface; +use Slovocast\Infrastructure\Database\ConnectionPoolConfig; +use Slovocast\Infrastructure\Database\ConnectionPool; use Slovocast\Infrastructure\User\BasicUserAuthorization; use Twig\Error\LoaderError; @@ -125,16 +128,17 @@ class Bootstrap /** * Database Connections */ - MysqlClient::class => function (ContainerInterface $container) { + ConnectionPoolInterface::class => function (ContainerInterface $container) { $config = $container->get('config')->get('database'); - $connectionString = sprintf( - "%s:%s@%s/%s", - rawurlencode($config['username']), - rawurlencode($config['password']), + + $pool = new ConnectionPool(new ConnectionPoolConfig( + $config['username'], + $config['password'], $config['host'], $config['database'] - ); - return new MysqlClient($connectionString); + )); + + return $pool; }, /** @@ -149,7 +153,7 @@ class Bootstrap */ UserRepositoryInterface::class => function (ContainerInterface $container) { return new UserRepository( - $container->get(MysqlClient::class), + $container->get(ConnectionPoolInterface::class), $container->get(UserAuthorizationInterface::class) ); } diff --git a/app/src/Domain/Repository/User/UserRepository.php b/app/src/Domain/Repository/User/UserRepository.php index e394740..c3c858b 100644 --- a/app/src/Domain/Repository/User/UserRepository.php +++ b/app/src/Domain/Repository/User/UserRepository.php @@ -2,16 +2,16 @@ namespace Slovocast\Domain\Repository\User; -use React\Mysql\MysqlClient; use Slovocast\Domain\Entity\User; use Slovocast\Exception\EntityNotFoundException; +use Slovocast\Infrastructure\Api\Database\ConnectionPoolInterface; use Slovocast\Infrastructure\Api\User\UserAuthorizationInterface; use function React\Async\await; class UserRepository implements UserRepositoryInterface { public function __construct( - private MysqlClient $db, + private ConnectionPoolInterface $connectionPool, private UserAuthorizationInterface $userAuth ) {} @@ -38,7 +38,11 @@ class UserRepository implements UserRepositoryInterface public function get(int $id): User { $query = "SELECT * FROM users WHERE id = ? LIMIT 1"; - $results = await($this->db->query($query, [ $id ])); + + /** @var $conn ConnectionPool */ + $conn = $this->connectionPool->getConnection(); + $results = await($conn->query($query, [ $id ])); + return $this->userFromQueryResults($results->resultRows[0]); } diff --git a/app/src/Infrastructure/Database/ConnectionPool.php b/app/src/Infrastructure/Database/ConnectionPool.php index efe83eb..1f8f197 100644 --- a/app/src/Infrastructure/Database/ConnectionPool.php +++ b/app/src/Infrastructure/Database/ConnectionPool.php @@ -66,11 +66,15 @@ class ConnectionPool implements ConnectionPoolInterface { if (!$this->hasIdleConnection()) { \React\Async\delay((float) $this->getWaitTimeout()); - return this->getConnection(); + return $this->getConnection(); } + // Remove from the idle pool $conn = $this->idleConnections->current(); $this->idleConnections->detach($conn); + + // Attach to the pool to the connectin, add it to the active pool + $conn->setConnectionPool($this); $this->activeConnections->attach($conn); return $conn; } diff --git a/app/src/Routes.php b/app/src/Routes.php index c7c0c71..543cd54 100644 --- a/app/src/Routes.php +++ b/app/src/Routes.php @@ -30,7 +30,6 @@ class Routes /** * @param App $app Instantiated Application - * @return void */ protected static function users(App $app): void { @@ -47,6 +46,9 @@ class Routes ->setName('user-login-action'); } + /** + * @param App $app Instance of the application + */ protected static function dashboard(App $app): void { $app->get('/dashboard', DashboardPage::class)