This commit is contained in:
Eldad Fux
2022-11-03 17:19:55 +02:00
parent fdf88d8355
commit de610855fe
3 changed files with 2 additions and 212 deletions
-5
View File
@@ -249,11 +249,6 @@ $http->on('request', function (SwooleRequest $swooleRequest, SwooleResponse $swo
return;
}
$pools = $register->get('pools'); /** @var Group $pools */
var_dump('current console connection');
var_dump($pools->get('console')->pop()->getID());
var_dump('current pool size');
var_dump($pools->get('console')->count());
// Console::log('sleep start');
// System::sleep(3);
// Console::log('sleep end');
+2 -204
View File
@@ -1,205 +1,5 @@
<?php
use Swoole\Coroutine\Channel;
use Utopia\Pools\Connection;
use Utopia\Pools\Pool as PoolsPool;
class Pool extends PoolsPool
{
/**
* @var string
*/
protected string $name;
/**
* @var int
*/
protected int $size = 0;
/**
* @var callable
*/
protected $init = null;
/**
* @var int
*/
protected int $reconnectAttempts = 3;
/**
* @var int
*/
protected int $reconnectSleep = 1; // seconds
/**
* @var Channel
*/
public Channel $channel;
/**
* @var array
*/
protected array $active = [];
/**
* @var string $name
* @var int $size
* @var callable $init
*/
public function __construct(string $name, int $size, callable $init)
{
$this->name = $name;
$this->size = $size;
$this->init = $init;
$this->channel = new Channel($size);
}
/**
* @return string
*/
public function getName(): string
{
return $this->name;
}
/**
* @return int
*/
public function getSize(): int
{
return $this->size;
}
/**
* @return int
*/
public function getReconnectAttempts(): int
{
return $this->reconnectAttempts;
}
/**
* @var int $reconnectAttempts
* @return self
*/
public function setReconnectAttempts(int $reconnectAttempts): self
{
$this->reconnectAttempts = $reconnectAttempts;
return $this;
}
/**
* @return int
*/
public function getReconnectSleep(): int
{
return $this->reconnectSleep;
}
/**
* @var int $reconnectSleep
* @return self
*/
public function setReconnectSleep(int $reconnectSleep): self
{
$this->reconnectSleep = $reconnectSleep;
return $this;
}
/**
* @return self
*/
public function fill(): self
{
if(!$this->channel->isEmpty()) {
return $this;
}
for ($i=0; $i < $this->size; $i++) {
$attempts = 0;
do {
try {
$attempts++;
$connection = new Connection(($this->init)());
break; // leave loop if successful
} catch (\Exception $e) {
if ($attempts >= $this->getReconnectAttempts()) {
throw new \Exception('Failed to create connection: ' . $e->getMessage());
}
sleep($this->getReconnectSleep());
}
} while ($attempts < $this->getReconnectAttempts());
$connection->setID($this->getName().'-'.$i);
$this->channel->push($connection);
}
return $this;
}
/**
* @return Connection
*/
public function pop(): Connection
{
if ($this->channel->isEmpty()) {
throw new Exception('Pool is empty');
}
$connection = $this->channel->pop();
$this->active[$connection->getID()] = $connection;
return $connection;
}
/**
* @param Connection $connection
* @return self
*/
public function push(Connection $connection): self
{
$this->channel->push($connection);
unset($this->active[$connection->getID()]);
return $this;
}
/**
* @return int
*/
public function count(): int
{
return $this->channel->length();
}
/**
* @return self
*/
public function reclaim(): self
{
foreach ($this->active as $connection) {
$this->push($connection);
}
return $this;
}
/**
* @return bool
*/
public function isEmpty(): bool
{
return $this->channel->isEmpty();
}
/**
* @return bool
*/
public function isFull(): bool
{
return $this->channel->isFull();
}
}
/**
* Init
@@ -270,6 +70,7 @@ use Utopia\CLI\Console;
use Utopia\Database\Adapter\MariaDB;
use Utopia\Database\Adapter\MySQL;
use Utopia\Pools\Group;
use Utopia\Pools\Pool;
use Ahc\Jwt\JWT;
use Ahc\Jwt\JWTException;
use MaxMind\Db\Reader;
@@ -821,10 +622,7 @@ $register->set('pools', function () {
break;
}
$pool = new Pool($name, 64, function () use ($type, $resource, $dsn) {
$pool = new Pool($name, 5, function () use ($type, $resource, $dsn) {
// Get Adapter
$adapter = null;
-3
View File
@@ -84,7 +84,6 @@ services:
- ./public:/usr/src/code/public
- ./src:/usr/src/code/src
- ./dev:/usr/local/dev
- ./vendor/utopia-php/framework:/usr/src/code/vendor/utopia-php/framework
depends_on:
- mariadb
- redis
@@ -361,7 +360,6 @@ services:
volumes:
- ./app:/usr/src/code/app
- ./src:/usr/src/code/src
#- ./vendor/utopia-php/database:/usr/src/code/vendor/utopia-php/database
depends_on:
- redis
- mariadb
@@ -619,7 +617,6 @@ services:
volumes:
- ./app:/usr/src/code/app
- ./src:/usr/src/code/src
#- ./vendor/utopia-php/database:/usr/src/code/vendor/utopia-php/database
depends_on:
- redis
environment: