Add a connection pool class and start utilizing it.
This commit is contained in:
parent
f5abbcb8c9
commit
f788ba63ae
@ -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)
|
||||
);
|
||||
}
|
||||
|
@ -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]);
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user