Get the Pooled connection class set up.
This commit is contained in:
parent
4a66af0f2b
commit
07b73e7f7f
@ -2,6 +2,7 @@
|
||||
|
||||
namespace Slovocast\Infrastructure\Database;
|
||||
|
||||
use React\Mysql\MysqlClient;
|
||||
use Slovocast\Infrastructure\Api\Database\ConnectionPoolInterface;
|
||||
use Slovocast\Infrastructure\Api\Database\PooledConnectionInterface;
|
||||
|
||||
@ -13,6 +14,15 @@ class ConnectionPool implements ConnectionPoolInterface
|
||||
const int DEFAULT_WAIT_TIMEOUT = 100;
|
||||
private array $idleConnections;
|
||||
private array $activeConnections;
|
||||
private ConnectionPoolConfig $config;
|
||||
|
||||
public function __construct(ConnectionPoolConfig $config)
|
||||
{
|
||||
$this->config = $config;
|
||||
for ($i = 0; $i < $config->getTotalConnections(); $i++) {
|
||||
$this->idleConnections[] = new MysqlClient($this->config->getDsn());
|
||||
}
|
||||
}
|
||||
|
||||
public function getTotalIdleConnections(): int
|
||||
{
|
||||
|
@ -2,14 +2,18 @@
|
||||
|
||||
namespace Slovocast\Infrastructure\Database;
|
||||
|
||||
use React\Mysql\MysqlClient;
|
||||
|
||||
class ConnectionPoolConfig
|
||||
{
|
||||
public function __construct(
|
||||
public readonly string $username,
|
||||
public readonly string $password,
|
||||
public readonly string $database,
|
||||
public readonly string $host,
|
||||
protected int $port = 3306,
|
||||
protected int $poolWaitTimeout = ConnectionPool::DEFAULT_WAIT_TIMEOUT,
|
||||
protected int $totalConnections = 10,
|
||||
) { }
|
||||
|
||||
public function getPort(): int
|
||||
@ -21,4 +25,20 @@ class ConnectionPoolConfig
|
||||
{
|
||||
return $this->poolWaitTimeout;
|
||||
}
|
||||
|
||||
public function getTotalConnections(): int
|
||||
{
|
||||
return $this->totalConnections;
|
||||
}
|
||||
|
||||
public function getDsn(): string
|
||||
{
|
||||
return sprintf(
|
||||
"%s:%s@%s/%s",
|
||||
rawurlencode($this->username),
|
||||
rawurlencode($this->password),
|
||||
$this->host,
|
||||
$this->database
|
||||
);
|
||||
}
|
||||
}
|
||||
|
23
app/src/Infrastructure/Database/PooledConnection.php
Normal file
23
app/src/Infrastructure/Database/PooledConnection.php
Normal file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace Slovocast\Infrastructure\Database;
|
||||
|
||||
use React\Mysql\MysqlClient;
|
||||
use Slovocast\Infrastructure\Api\Database\ConnectionPoolInterface;
|
||||
use Slovocast\Infrastructure\Api\Database\PooledConnectionInterface;
|
||||
|
||||
class PooledConnection extends MysqlClient implements PooledConnectionInterface
|
||||
{
|
||||
protected ConnectionPoolInterface $connectionPool;
|
||||
|
||||
public function setConnectionPool(ConnectionPoolInterface $connectionPool): self
|
||||
{
|
||||
$this->connectionPool = $connectionPool;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function release(): void
|
||||
{
|
||||
$this->connectionPool->releaseConnection($this);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user