1
0
Fork 0
mirror of synced 2024-07-04 06:00:53 +12:00
This commit is contained in:
Eldad Fux 2022-10-28 17:45:16 +03:00
parent 269266127d
commit 8e492a9797
5 changed files with 232 additions and 9 deletions

2
.env
View file

@ -25,6 +25,8 @@ _APP_DB_PASS=password
_APP_DB_ROOT_PASS=rootsecretpassword
_APP_CONNECTIONS_DB_PROJECT=db_fra1_02=mysql://user:password@mariadb:3306/appwrite
_APP_CONNECTIONS_DB_CONSOLE=db_fra1_01=mysql://user:password@mariadb:3306/appwrite
#_APP_CONNECTIONS_DB_PROJECT=db_fra1_02=mysql://doadmin:AVNS_sgtjg6ZSHBdg66422x-@db-mysql-fra1-shmuel-test-do-user-10204879-0.b.db.ondigitalocean.com:25060/console
#_APP_CONNECTIONS_DB_CONSOLE=db_fra1_01=mysql://doadmin:AVNS_sgtjg6ZSHBdg66422x-@db-mysql-fra1-shmuel-test-do-user-10204879-0.b.db.ondigitalocean.com:25060/projects
_APP_CONNECTIONS_CACHE=redis_fra1_01=redis://redis:6379
_APP_CONNECTIONS_QUEUE=redis_fra1_01=redis://redis:6379
_APP_CONNECTIONS_PUBSUB=redis_fra1_01=redis://redis:6379

View file

@ -1003,7 +1003,7 @@ $collections = [
'$id' => ID::custom('secret'),
'type' => Database::VAR_STRING,
'format' => '',
'size' => 512, // var_dump of \bin2hex(\random_bytes(128)) => string(256) doubling for encryption
'size' => 512, // Output of \bin2hex(\random_bytes(128)) => string(256) doubling for encryption
'signed' => true,
'required' => true,
'default' => null,

View file

@ -20,6 +20,7 @@ use Utopia\Database\Database;
use Utopia\Database\Document;
use Utopia\Swoole\Files;
use Appwrite\Utopia\Request;
use Swoole\Coroutine\Channel;
use Utopia\Logger\Log;
use Utopia\Logger\Log\User;
use Utopia\Pools\Group;
@ -59,9 +60,13 @@ include __DIR__ . '/controllers/general.php';
$http->on('start', function (Server $http) use ($payloadSize, $register) {
$app = new App('UTC');
global $http;
var_dump('Test size is:'.$register->get('test')->length());
var_dump('Startup is served by:'.$http->worker_id);
go(function () use ($register, $app) {
var_dump('Test 2 size is:'.$register->get('test')->length());
$pools = $register->get('pools'); /** @var Group $pools */
App::setResource('pools', fn() => $pools);
@ -119,7 +124,7 @@ $http->on('start', function (Server $http) use ($payloadSize, $register) {
/**
* Skip to prevent 0.16 migration issues.
*/
if (in_array($key, ['cache', 'variables']) && $dbForConsole->exists(App::getEnv('_APP_DB_SCHEMA', 'appwrite'), 'bucket_1')) {
if (in_array($key, ['cache', 'variables']) && $dbForConsole->exists($dbForConsole->getDefaultDatabase(), 'bucket_1')) {
continue;
}
@ -155,7 +160,7 @@ $http->on('start', function (Server $http) use ($payloadSize, $register) {
$dbForConsole->createCollection($key, $attributes, $indexes);
}
if ($dbForConsole->getDocument('buckets', 'default')->isEmpty() && !$dbForConsole->exists(App::getEnv('_APP_DB_SCHEMA', 'appwrite'), 'bucket_1')) {
if ($dbForConsole->getDocument('buckets', 'default')->isEmpty() && !$dbForConsole->exists($dbForConsole->getDefaultDatabase(), 'bucket_1')) {
Console::success('[Setup] - Creating default bucket...');
$dbForConsole->createDocument('buckets', new Document([
'$id' => ID::custom('default'),
@ -233,6 +238,9 @@ $http->on('start', function (Server $http) use ($payloadSize, $register) {
$http->on('request', function (SwooleRequest $swooleRequest, SwooleResponse $swooleResponse) use ($register) {
$request = new Request($swooleRequest);
$response = new Response($swooleResponse);
var_dump('Test 3 size is:'.$register->get('test')->length());
global $http;
var_dump('Served by:'.$http->worker_id);
if (Files::isFileLoaded($request->getURI())) {
$time = (60 * 60 * 24 * 365 * 2); // 45 days cache

View file

@ -1,5 +1,206 @@
<?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
*
@ -69,7 +270,6 @@ 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;
@ -498,7 +698,8 @@ $register->set('logger', function () {
return new Logger($adapter);
});
$register->set('pools', function () {
Console::info('Initializing pools');
$group = new Group();
$fallbackForDB = AppwriteURL::unparse([
@ -622,6 +823,9 @@ $register->set('pools', function () {
break;
}
$pool = new Pool($name, 64, function () use ($type, $resource, $dsn) {
// Get Adapter
$adapter = null;
@ -634,7 +838,6 @@ $register->set('pools', function () {
default => null
};
var_dump($dsn->getDatabase());
$adapter->setDefaultDatabase($dsn->getDatabase());
break;
@ -658,7 +861,7 @@ $register->set('pools', function () {
return $adapter;
});
var_dump($pool->channel->length());
$group->add($pool);
}
@ -667,12 +870,22 @@ $register->set('pools', function () {
try {
$group->fill();
Console::success('Pools have been filled succefully');
} catch (\Throwable $th) {
Console::error('Connection failure: ' . $th->getMessage());
}
return $group;
});
$register->set('test', function () {
var_dump('[[init test!!]]');
$test = new Channel(5);
$test->push(1);
$test->push(1);
$test->push(1);
$test->push(1);
return $test;
});
$register->set('influxdb', function () {
// Register DB connection
$host = App::getEnv('_APP_INFLUXDB_HOST', '');

View file

@ -206,7 +206,7 @@ $cli
unset($models[$key]);
}
}
// var_dump($models);
$arguments = [new App('UTC'), $services, $routes, $models, $keys[$platform], $authCounts[$platform] ?? 0];
foreach (['swagger2', 'open-api3'] as $format) {
$formatInstance = match ($format) {