feat: fix realtime tests

This commit is contained in:
Christy Jacob 2022-08-24 14:21:26 +05:30
parent d2bf8b25de
commit ceb11f839f
4 changed files with 79 additions and 48 deletions

View file

@ -345,7 +345,6 @@ $http->on('request', function (SwooleRequest $swooleRequest, SwooleResponse $swo
$swooleResponse->end(\json_encode($output)); $swooleResponse->end(\json_encode($output));
} finally { } finally {
$dbPool->reset(); $dbPool->reset();
/** @var RedisPool $redisPool */ /** @var RedisPool $redisPool */
$redisPool = $register->get('redisPool'); $redisPool = $register->get('redisPool');
$redisPool->put($redis); $redisPool->put($redis);

View file

@ -19,6 +19,10 @@ use Utopia\Database\Query;
use Utopia\Database\Validator\Authorization; use Utopia\Database\Validator\Authorization;
use Utopia\Registry\Registry; use Utopia\Registry\Registry;
use Appwrite\Utopia\Request; use Appwrite\Utopia\Request;
use Utopia\Cache\Adapter\Redis as RedisCache;
use Utopia\Cache\Cache;
use Utopia\Database\Adapter\MariaDB;
use Utopia\Database\Database;
use Utopia\WebSocket\Server; use Utopia\WebSocket\Server;
use Utopia\WebSocket\Adapter; use Utopia\WebSocket\Adapter;
@ -88,10 +92,27 @@ $logError = function (Throwable $error, string $action) use ($register) {
$server->error($logError); $server->error($logError);
function getDatabase(Registry &$register, string $projectID) function getDatabase(Registry &$register, string $projectId)
{ {
$redis = $register->get('redisPool')->get(); $redis = $register->get('redisPool')->get();
$database = $register->get('dbPool')->getDBFromPool($projectID, $redis); $dbPool = $register->get('dbPool');
/** Get the console DB */
$database = $dbPool->getConsoleDB();
$pdo = $dbPool->getDBFromPool($database);
$cache = new Cache(new RedisCache($redis));
$database = new Database(new MariaDB($pdo->getConnection()), $cache);
$database->setDefaultDatabase(App::getEnv('_APP_DB_SCHEMA', 'appwrite'));
$database->setNamespace("_console");
if ($projectId !== 'console') {
$project = Authorization::skip(fn() => $database->getDocument('projects', $projectId));
$database = $project->getAttribute('database', '');
$pdo = $dbPool->getDBFromPool($database);
$database = new Database(new MariaDB($pdo->getConnection()), $cache);
$database->setDefaultDatabase(App::getEnv('_APP_DB_SCHEMA', 'appwrite'));
$database->setNamespace("_{$project->getInternalId()}");
}
return [ return [
$database, $database,
@ -341,9 +362,6 @@ $server->onOpen(function (int $connection, SwooleRequest $request) use ($server,
App::setResource('response', fn() => $response); App::setResource('response', fn() => $response);
try { try {
/** @var \Utopia\Database\Document $console */
$console = $app->getResource('console');
/** @var \Utopia\Database\Document $project */ /** @var \Utopia\Database\Document $project */
$project = $app->getResource('project'); $project = $app->getResource('project');
@ -355,7 +373,9 @@ $server->onOpen(function (int $connection, SwooleRequest $request) use ($server,
} }
$dbForProject = $app->getResource('dbForProject'); $dbForProject = $app->getResource('dbForProject');
/** @var \Utopia\Database\Document $console */
$console = $app->getResource('console');
/** @var \Utopia\Database\Document $user */ /** @var \Utopia\Database\Document $user */
$user = $app->getResource('user'); $user = $app->getResource('user');
@ -452,20 +472,33 @@ $server->onOpen(function (int $connection, SwooleRequest $request) use ($server,
$server->onMessage(function (int $connection, string $message) use ($server, $register, $realtime, $containerId) { $server->onMessage(function (int $connection, string $message) use ($server, $register, $realtime, $containerId) {
try { try {
$response = new Response(new SwooleResponse()); $response = new Response(new SwooleResponse());
$projectId = $realtime->connections[$connection]['projectId'];
$redis = $register->get('redisPool')->get(); $redis = $register->get('redisPool')->get();
$dbPool = $register->get('dbPool'); $dbPool = $register->get('dbPool');
$dbForProject = $dbPool->getDBFromPool($projectId, $redis); $projectId = $realtime->connections[$connection]['projectId'];
/** Get the console DB */
$database = $dbPool->getConsoleDB();
$pdo = $dbPool->getDBFromPool($database);
$cache = new Cache(new RedisCache($redis));
$database = new Database(new MariaDB($pdo->getConnection()), $cache);
$database->setDefaultDatabase(App::getEnv('_APP_DB_SCHEMA', 'appwrite'));
$database->setNamespace("_console");
if ($projectId !== 'console') {
$project = Authorization::skip(fn() => $database->getDocument('projects', $projectId));
$database = $project->getAttribute('database', '');
$pdo = $dbPool->getDBFromPool($database);
$database = new Database(new MariaDB($pdo->getConnection()), $cache);
$database->setDefaultDatabase(App::getEnv('_APP_DB_SCHEMA', 'appwrite'));
$database->setNamespace("_{$project->getInternalId()}");
}
/* /*
* Abuse Check * Abuse Check
* *
* Abuse limits are sending 32 times per minute and connection. * Abuse limits are sending 32 times per minute and connection.
*/ */
$timeLimit = new TimeLimit('url:{url},connection:{connection}', 32, 60, $dbForProject); $timeLimit = new TimeLimit('url:{url},connection:{connection}', 32, 60, $database);
$timeLimit $timeLimit
->setParam('{connection}', $connection) ->setParam('{connection}', $connection)
@ -496,7 +529,7 @@ $server->onMessage(function (int $connection, string $message) use ($server, $re
Auth::$unique = $session['id'] ?? ''; Auth::$unique = $session['id'] ?? '';
Auth::$secret = $session['secret'] ?? ''; Auth::$secret = $session['secret'] ?? '';
$user = $dbForProject->getDocument('users', Auth::$unique); $user = $database->getDocument('users', Auth::$unique);
if ( if (
empty($user->getId()) // Check a document has been found in the DB empty($user->getId()) // Check a document has been found in the DB
@ -541,7 +574,7 @@ $server->onMessage(function (int $connection, string $message) use ($server, $re
$server->close($connection, $th->getCode()); $server->close($connection, $th->getCode());
} }
} finally { } finally {
call_user_func($returnProjectDB); $dbPool->reset();
$register->get('redisPool')->put($redis); $register->get('redisPool')->put($redis);
} }
}); });

62
composer.lock generated
View file

@ -1894,16 +1894,16 @@
}, },
{ {
"name": "utopia-php/cache", "name": "utopia-php/cache",
"version": "0.6.0", "version": "0.6.1",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/utopia-php/cache.git", "url": "https://github.com/utopia-php/cache.git",
"reference": "8ea1353a4bbab617e23c865a7c97b60d8074aee3" "reference": "9889235a6d3da6cbb1f435201529da4d27c30e79"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/utopia-php/cache/zipball/8ea1353a4bbab617e23c865a7c97b60d8074aee3", "url": "https://api.github.com/repos/utopia-php/cache/zipball/9889235a6d3da6cbb1f435201529da4d27c30e79",
"reference": "8ea1353a4bbab617e23c865a7c97b60d8074aee3", "reference": "9889235a6d3da6cbb1f435201529da4d27c30e79",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -1941,9 +1941,9 @@
], ],
"support": { "support": {
"issues": "https://github.com/utopia-php/cache/issues", "issues": "https://github.com/utopia-php/cache/issues",
"source": "https://github.com/utopia-php/cache/tree/0.6.0" "source": "https://github.com/utopia-php/cache/tree/0.6.1"
}, },
"time": "2022-04-04T12:30:05+00:00" "time": "2022-08-10T08:12:46+00:00"
}, },
{ {
"name": "utopia-php/cli", "name": "utopia-php/cli",
@ -2051,16 +2051,16 @@
}, },
{ {
"name": "utopia-php/database", "name": "utopia-php/database",
"version": "0.18.7", "version": "0.18.9",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/utopia-php/database.git", "url": "https://github.com/utopia-php/database.git",
"reference": "d542ee433f1a545d926ffaf707bdf952dc18a52e" "reference": "227b3ca919149b7b0d6556c8effe9ee46ed081e6"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/utopia-php/database/zipball/d542ee433f1a545d926ffaf707bdf952dc18a52e", "url": "https://api.github.com/repos/utopia-php/database/zipball/227b3ca919149b7b0d6556c8effe9ee46ed081e6",
"reference": "d542ee433f1a545d926ffaf707bdf952dc18a52e", "reference": "227b3ca919149b7b0d6556c8effe9ee46ed081e6",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -2109,9 +2109,9 @@
], ],
"support": { "support": {
"issues": "https://github.com/utopia-php/database/issues", "issues": "https://github.com/utopia-php/database/issues",
"source": "https://github.com/utopia-php/database/tree/0.18.7" "source": "https://github.com/utopia-php/database/tree/0.18.9"
}, },
"time": "2022-07-11T10:20:33+00:00" "time": "2022-07-19T09:42:53+00:00"
}, },
{ {
"name": "utopia-php/domains", "name": "utopia-php/domains",
@ -2948,16 +2948,16 @@
}, },
{ {
"name": "matthiasmullie/minify", "name": "matthiasmullie/minify",
"version": "1.3.68", "version": "1.3.69",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/matthiasmullie/minify.git", "url": "https://github.com/matthiasmullie/minify.git",
"reference": "c00fb02f71b2ef0a5f53fe18c5a8b9aa30f48297" "reference": "a61c949cccd086808063611ef9698eabe42ef22f"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/matthiasmullie/minify/zipball/c00fb02f71b2ef0a5f53fe18c5a8b9aa30f48297", "url": "https://api.github.com/repos/matthiasmullie/minify/zipball/a61c949cccd086808063611ef9698eabe42ef22f",
"reference": "c00fb02f71b2ef0a5f53fe18c5a8b9aa30f48297", "reference": "a61c949cccd086808063611ef9698eabe42ef22f",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -3006,7 +3006,7 @@
], ],
"support": { "support": {
"issues": "https://github.com/matthiasmullie/minify/issues", "issues": "https://github.com/matthiasmullie/minify/issues",
"source": "https://github.com/matthiasmullie/minify/tree/1.3.68" "source": "https://github.com/matthiasmullie/minify/tree/1.3.69"
}, },
"funding": [ "funding": [
{ {
@ -3014,7 +3014,7 @@
"type": "github" "type": "github"
} }
], ],
"time": "2022-04-19T08:28:56+00:00" "time": "2022-08-01T09:00:18+00:00"
}, },
{ {
"name": "matthiasmullie/path-converter", "name": "matthiasmullie/path-converter",
@ -3524,23 +3524,23 @@
}, },
{ {
"name": "phpunit/php-code-coverage", "name": "phpunit/php-code-coverage",
"version": "9.2.15", "version": "9.2.16",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/sebastianbergmann/php-code-coverage.git", "url": "https://github.com/sebastianbergmann/php-code-coverage.git",
"reference": "2e9da11878c4202f97915c1cb4bb1ca318a63f5f" "reference": "2593003befdcc10db5e213f9f28814f5aa8ac073"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/2e9da11878c4202f97915c1cb4bb1ca318a63f5f", "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/2593003befdcc10db5e213f9f28814f5aa8ac073",
"reference": "2e9da11878c4202f97915c1cb4bb1ca318a63f5f", "reference": "2593003befdcc10db5e213f9f28814f5aa8ac073",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
"ext-dom": "*", "ext-dom": "*",
"ext-libxml": "*", "ext-libxml": "*",
"ext-xmlwriter": "*", "ext-xmlwriter": "*",
"nikic/php-parser": "^4.13.0", "nikic/php-parser": "^4.14",
"php": ">=7.3", "php": ">=7.3",
"phpunit/php-file-iterator": "^3.0.3", "phpunit/php-file-iterator": "^3.0.3",
"phpunit/php-text-template": "^2.0.2", "phpunit/php-text-template": "^2.0.2",
@ -3589,7 +3589,7 @@
], ],
"support": { "support": {
"issues": "https://github.com/sebastianbergmann/php-code-coverage/issues", "issues": "https://github.com/sebastianbergmann/php-code-coverage/issues",
"source": "https://github.com/sebastianbergmann/php-code-coverage/tree/9.2.15" "source": "https://github.com/sebastianbergmann/php-code-coverage/tree/9.2.16"
}, },
"funding": [ "funding": [
{ {
@ -3597,7 +3597,7 @@
"type": "github" "type": "github"
} }
], ],
"time": "2022-03-07T09:28:20+00:00" "time": "2022-08-20T05:26:47+00:00"
}, },
{ {
"name": "phpunit/php-file-iterator", "name": "phpunit/php-file-iterator",
@ -5271,16 +5271,16 @@
}, },
{ {
"name": "twig/twig", "name": "twig/twig",
"version": "v3.4.1", "version": "v3.4.2",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/twigphp/Twig.git", "url": "https://github.com/twigphp/Twig.git",
"reference": "e939eae92386b69b49cfa4599dd9bead6bf4a342" "reference": "e07cdd3d430cd7e453c31b36eb5ad6c0c5e43077"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/twigphp/Twig/zipball/e939eae92386b69b49cfa4599dd9bead6bf4a342", "url": "https://api.github.com/repos/twigphp/Twig/zipball/e07cdd3d430cd7e453c31b36eb5ad6c0c5e43077",
"reference": "e939eae92386b69b49cfa4599dd9bead6bf4a342", "reference": "e07cdd3d430cd7e453c31b36eb5ad6c0c5e43077",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -5331,7 +5331,7 @@
], ],
"support": { "support": {
"issues": "https://github.com/twigphp/Twig/issues", "issues": "https://github.com/twigphp/Twig/issues",
"source": "https://github.com/twigphp/Twig/tree/v3.4.1" "source": "https://github.com/twigphp/Twig/tree/v3.4.2"
}, },
"funding": [ "funding": [
{ {
@ -5343,7 +5343,7 @@
"type": "tidelift" "type": "tidelift"
} }
], ],
"time": "2022-05-17T05:48:52+00:00" "time": "2022-08-12T06:47:24+00:00"
} }
], ],
"aliases": [], "aliases": [],

View file

@ -177,8 +177,8 @@ abstract class Worker
$cache = $register->get('cache'); $cache = $register->get('cache');
$dbPool = $register->get('dbPool'); $dbPool = $register->get('dbPool');
$dbForProject = $dbPool->getDB($database, $cache); $dbForProject = $dbPool->getDB($database, $cache);
$namespace = "_$internalId"; $namespace = "_$internalId";
$dbForProject->setDefaultDatabase(App::getEnv('_APP_DB_SCHEMA', 'appwrite')); $dbForProject->setDefaultDatabase(App::getEnv('_APP_DB_SCHEMA', 'appwrite'));
$dbForProject->setNamespace($namespace); $dbForProject->setNamespace($namespace);
@ -201,7 +201,6 @@ abstract class Worker
} }
$dbForConsole = $dbPool->getDB($database, $cache); $dbForConsole = $dbPool->getDB($database, $cache);
$namespace = "_console"; $namespace = "_console";
$dbForConsole->setDefaultDatabase(App::getEnv('_APP_DB_SCHEMA', 'appwrite')); $dbForConsole->setDefaultDatabase(App::getEnv('_APP_DB_SCHEMA', 'appwrite'));
$dbForConsole->setNamespace($namespace); $dbForConsole->setNamespace($namespace);