From 4a66af0f2beee33d6a4271cf21733a76c545ffb6 Mon Sep 17 00:00:00 2001 From: Dave Smith-Hayes Date: Mon, 23 Sep 2024 19:41:50 -0400 Subject: [PATCH] Document the connection pool interface a bit, start writing a connection pool. --- .../Api/Database/ConnectionPoolInterface.php | 39 +++++++++++-- ...face.php => PooledConnectionInterface.php} | 0 .../Database/ConnectionPool.php | 56 +++++++++++++++++++ .../Database/ConnectionPoolConfig.php | 24 ++++++++ 4 files changed, 114 insertions(+), 5 deletions(-) rename app/src/Infrastructure/Api/Database/{ConnectionInterface.php => PooledConnectionInterface.php} (100%) create mode 100644 app/src/Infrastructure/Database/ConnectionPool.php create mode 100644 app/src/Infrastructure/Database/ConnectionPoolConfig.php diff --git a/app/src/Infrastructure/Api/Database/ConnectionPoolInterface.php b/app/src/Infrastructure/Api/Database/ConnectionPoolInterface.php index 18754e7..4b614d8 100644 --- a/app/src/Infrastructure/Api/Database/ConnectionPoolInterface.php +++ b/app/src/Infrastructure/Api/Database/ConnectionPoolInterface.php @@ -2,11 +2,40 @@ namespace Slovocast\Infrastructure\Api\Database; +use Exception; + interface ConnectionPoolInterface { - public function getFreeConnectionsCount(): int; - public function getActiveConnectionsCount(): int; - public function hasFreeConnection(): bool; - public function getConnection(): ConnectionInterface; - public function releaseConnection(ConnectionInterface $connection): void; + public function getTotalIdleConnections(): int; + public function getTotalActiveConnections(): int; + + /** + * If there is at least one idle connection waiting to be used. + * + * @return bool + */ + public function hasIdleConnection(): bool; + public function setWaitTimeout(int $seconds): void; + public function getWaitTimeout(): int; + public function getConnectionLimit(): int; + + /** + * This method will grab a connection from the Idle list, push it into the Active list and returns that same + * connection to the caller. If there is no active connections, the method will wait based on the `waitTimeout` + * value (in seconds) and throw an exception when the timeout has been reached. + * + * @throws Exception When the Wait Timeout is surpassed waiting for an idle connection + * @return PooledConnectionInterface + */ + public function getConnection(): PooledConnectionInterface; + + /** + * This method will return a connection from the Active list into the Idle list. + * + * @param PooledConnectionInterface $connection + * @return void + */ + public function releaseConnection(PooledConnectionInterface $connection): void; + + } diff --git a/app/src/Infrastructure/Api/Database/ConnectionInterface.php b/app/src/Infrastructure/Api/Database/PooledConnectionInterface.php similarity index 100% rename from app/src/Infrastructure/Api/Database/ConnectionInterface.php rename to app/src/Infrastructure/Api/Database/PooledConnectionInterface.php diff --git a/app/src/Infrastructure/Database/ConnectionPool.php b/app/src/Infrastructure/Database/ConnectionPool.php new file mode 100644 index 0000000..534343f --- /dev/null +++ b/app/src/Infrastructure/Database/ConnectionPool.php @@ -0,0 +1,56 @@ +port; + } + + public function getPoolWaitTimeout(): int + { + return $this->poolWaitTimeout; + } +}