1
0
Fork 0
mirror of synced 2024-06-02 10:54:44 +12:00

Removed context, added pools

This commit is contained in:
Eldad Fux 2021-03-10 08:53:49 +02:00
parent 66099e06be
commit b26b831931
5 changed files with 118 additions and 107 deletions

View file

@ -15,12 +15,6 @@ use Utopia\CLI\Console;
// xdebug_start_trace('/tmp/trace');
ini_set('memory_limit','512M');
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
ini_set('default_socket_timeout', -1);
error_reporting(E_ALL);
$http = new Server("0.0.0.0", App::getEnv('PORT', 80));
$payloadSize = max(4000000 /* 4mb */, App::getEnv('_APP_STORAGE_LIMIT', 10000000 /* 10mb */));

View file

@ -11,6 +11,12 @@ if (\file_exists(__DIR__.'/../vendor/autoload.php')) {
require_once __DIR__.'/../vendor/autoload.php';
}
ini_set('memory_limit','512M');
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
ini_set('default_socket_timeout', -1);
error_reporting(E_ALL);
use Ahc\Jwt\JWT;
use Ahc\Jwt\JWTException;
use Appwrite\Auth\Auth;
@ -21,7 +27,6 @@ use Appwrite\Database\Document;
use Appwrite\Database\Validator\Authorization;
use Appwrite\Event\Event;
use Appwrite\Event\Realtime;
use Appwrite\Extend\PDO;
use Appwrite\OpenSSL\OpenSSL;
use Utopia\App;
use Utopia\View;
@ -31,6 +36,13 @@ use Utopia\Registry\Registry;
use MaxMind\Db\Reader;
use PHPMailer\PHPMailer\PHPMailer;
use PDO as PDONative;
use Swoole\Runtime;
use Swoole\Database\PDOConfig;
use Swoole\Database\PDOPool;
use Swoole\Database\RedisConfig;
use Swoole\Database\RedisPool;
Runtime::enableCoroutine(SWOOLE_HOOK_ALL);
const APP_NAME = 'Appwrite';
const APP_DOMAIN = 'appwrite.io';
@ -139,24 +151,35 @@ Database::addFilter('encrypt',
/*
* Registry
*/
$register->set('db', function () { // Register DB connection
$dbHost = App::getEnv('_APP_DB_HOST', '');
$dbUser = App::getEnv('_APP_DB_USER', '');
$dbPass = App::getEnv('_APP_DB_PASS', '');
$dbScheme = App::getEnv('_APP_DB_SCHEMA', '');
$register->set('dbPool', function () { // Register DB connection
$config = new PDOConfig();
$config
->withHost(App::getEnv('_APP_DB_HOST', ''))
->withPort(App::getEnv('_APP_DB_PORT', ''))
->withDbName(App::getEnv('_APP_DB_SCHEMA', ''))
->withUsername(App::getEnv('_APP_DB_USER', ''))
->withPassword(App::getEnv('_APP_DB_PASS', ''))
->withCharset('utf8mb4')
->withOptions([
PDONative::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8mb4',
PDONative::ATTR_TIMEOUT => 3, // Seconds
PDONative::ATTR_PERSISTENT => true
]);
$pdo = new PDO("mysql:host={$dbHost};dbname={$dbScheme};charset=utf8mb4", $dbUser, $dbPass, array(
PDONative::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8mb4',
PDONative::ATTR_TIMEOUT => 3, // Seconds
PDONative::ATTR_PERSISTENT => true
));
$pool = new PDOPool($config);
return $pool;
});
$register->set('db', function () use ($register) {
$pool = $register->get('dbPool');
$pdo = $pool->get()->__getObject();
// Connection settings
$pdo->setAttribute(PDONative::ATTR_DEFAULT_FETCH_MODE, PDONative::FETCH_ASSOC); // Return arrays
$pdo->setAttribute(PDONative::ATTR_ERRMODE, PDONative::ERRMODE_EXCEPTION); // Handle all errors with exceptions
return $pdo;
});
}, true);
$register->set('influxdb', function () { // Register DB connection
$host = App::getEnv('_APP_INFLUXDB_HOST', '');
$port = App::getEnv('_APP_INFLUXDB_PORT', '');
@ -178,25 +201,36 @@ $register->set('statsd', function () { // Register DB connection
return $statsd;
});
$register->set('cache', function () { // Register cache connection
$redis = new Redis();
$redis->pconnect(App::getEnv('_APP_REDIS_HOST', ''), App::getEnv('_APP_REDIS_PORT', ''));
$user = App::getEnv('_APP_REDIS_USER','');
$pass = App::getEnv('_APP_REDIS_PASS','');
$auth = [];
if(!empty($user)) {
$auth["user"] = $user;
$register->set('redisPool', function () {
$user = App::getEnv('_APP_REDIS_USER', '');
$pass = App::getEnv('_APP_REDIS_PASS', '');
$auth = '';
if (!empty($user)) {
$auth += $user;
}
if(!empty($pass)) {
$auth["pass"] = $pass;
}
if(!empty($auth)) {
$redis->auth($auth);
if (!empty($pass)) {
$auth += ':' . $pass;
}
$config = new RedisConfig();
$config
->withHost(App::getEnv('_APP_REDIS_HOST', ''))
->withPort(App::getEnv('_APP_REDIS_PORT', ''))
->withAuth($auth)
->withTimeout(0)
->withReadTimeout(0)
->withRetryInterval(0);
$pool = new RedisPool($config);
return $pool;
});
$register->set('cache', function () use ($register) { // Register cache connection
$redis = $register->get('redisPool')->get();
$redis->setOption(Redis::OPT_READ_TIMEOUT, -1);
return $redis;
});
}, true);
$register->set('smtp', function () {
$mail = new PHPMailer(true);

View file

@ -44,8 +44,6 @@ $server->on('workerStart', function ($server, $workerId) use (&$subscriptions, &
$attempts = 0;
$start = time();
$register->context('realtime-pubsub-' . $workerId);
while ($attempts < 300) {
try {
if ($attempts > 0) {
@ -54,8 +52,7 @@ $server->on('workerStart', function ($server, $workerId) use (&$subscriptions, &
sleep(5); // 5 sec delay between connection attempts
}
$redis = $register->get('cache', true);
$redis->setOption(Redis::OPT_READ_TIMEOUT, -1);
$redis = $register->get('cache');
if ($redis->ping(true)) {
$attempts = 0;
@ -121,8 +118,6 @@ $server->on('start', function (Server $server) {
$server->on('open', function (Server $server, Request $request) use (&$connections, &$subscriptions, &$register) {
Console::info("Connection open (user: {$request->fd}, connections: {}, worker: {$server->getWorkerId()})");
$register->context('realtime-' . $server->getWorkerId());
$app = new App('');
$connection = $request->fd;
$request = new SwooleRequest($request);

View file

@ -42,7 +42,7 @@
"utopia-php/cli": "0.10.0",
"utopia-php/config": "0.2.*",
"utopia-php/locale": "0.3.*",
"utopia-php/registry": "0.2.*",
"utopia-php/registry": "0.4.*",
"utopia-php/preloader": "0.2.*",
"utopia-php/domains": "0.2.*",
"utopia-php/swoole": "0.2.*",

124
composer.lock generated
View file

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "4f58de92fb64af44d915387895472881",
"content-hash": "4159791237c3f67d8a3d9df77513bcd0",
"packages": [
{
"name": "adhocore/jwt",
@ -349,7 +349,7 @@
"issues": "https://github.com/domnikl/statsd-php/issues",
"source": "https://github.com/domnikl/statsd-php/tree/master"
},
"abandoned": true,
"abandoned": "slickdeals/statsd",
"time": "2020-01-03T14:24:58+00:00"
},
{
@ -521,12 +521,12 @@
"source": {
"type": "git",
"url": "https://github.com/guzzle/promises.git",
"reference": "ddfeedfff2a52661429437da0702979f708e6ac6"
"reference": "8e7d04f1f6450fef59366c399cfad4b9383aa30d"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/guzzle/promises/zipball/ddfeedfff2a52661429437da0702979f708e6ac6",
"reference": "ddfeedfff2a52661429437da0702979f708e6ac6",
"url": "https://api.github.com/repos/guzzle/promises/zipball/8e7d04f1f6450fef59366c399cfad4b9383aa30d",
"reference": "8e7d04f1f6450fef59366c399cfad4b9383aa30d",
"shasum": ""
},
"require": {
@ -567,9 +567,9 @@
],
"support": {
"issues": "https://github.com/guzzle/promises/issues",
"source": "https://github.com/guzzle/promises/tree/master"
"source": "https://github.com/guzzle/promises/tree/1.4.1"
},
"time": "2020-10-19T16:50:15+00:00"
"time": "2021-03-07T09:25:29+00:00"
},
{
"name": "guzzlehttp/psr7",
@ -577,12 +577,12 @@
"source": {
"type": "git",
"url": "https://github.com/guzzle/psr7.git",
"reference": "f47ece9e6e8ce74e3be04bef47f46061dc18c095"
"reference": "d7fe0a0eabc266c3dcf2f20aa12121044ff196a4"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/guzzle/psr7/zipball/f47ece9e6e8ce74e3be04bef47f46061dc18c095",
"reference": "f47ece9e6e8ce74e3be04bef47f46061dc18c095",
"url": "https://api.github.com/repos/guzzle/psr7/zipball/d7fe0a0eabc266c3dcf2f20aa12121044ff196a4",
"reference": "d7fe0a0eabc266c3dcf2f20aa12121044ff196a4",
"shasum": ""
},
"require": {
@ -644,7 +644,7 @@
"issues": "https://github.com/guzzle/psr7/issues",
"source": "https://github.com/guzzle/psr7/tree/1.x"
},
"time": "2020-12-08T11:45:39+00:00"
"time": "2021-03-09T14:42:40+00:00"
},
{
"name": "influxdb/influxdb-php",
@ -1020,12 +1020,12 @@
"source": {
"type": "git",
"url": "https://github.com/php-fig/log.git",
"reference": "dd738d0b4491f32725492cf345f6b501f5922fec"
"reference": "a18c1e692e02b84abbafe4856c3cd7cc6903908c"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/php-fig/log/zipball/dd738d0b4491f32725492cf345f6b501f5922fec",
"reference": "dd738d0b4491f32725492cf345f6b501f5922fec",
"url": "https://api.github.com/repos/php-fig/log/zipball/a18c1e692e02b84abbafe4856c3cd7cc6903908c",
"reference": "a18c1e692e02b84abbafe4856c3cd7cc6903908c",
"shasum": ""
},
"require": {
@ -1063,7 +1063,7 @@
"support": {
"source": "https://github.com/php-fig/log/tree/master"
},
"time": "2020-09-18T06:44:51+00:00"
"time": "2021-03-02T15:02:34+00:00"
},
{
"name": "ralouphie/getallheaders",
@ -1850,20 +1850,20 @@
},
{
"name": "utopia-php/registry",
"version": "0.2.4",
"version": "0.3.0",
"source": {
"type": "git",
"url": "https://github.com/utopia-php/registry.git",
"reference": "428a94f1a36147e7b7221e778c01e1be08db2893"
"reference": "8b05fe6f8af73bf77e1700212a28f36be6733d3a"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/utopia-php/registry/zipball/428a94f1a36147e7b7221e778c01e1be08db2893",
"reference": "428a94f1a36147e7b7221e778c01e1be08db2893",
"url": "https://api.github.com/repos/utopia-php/registry/zipball/8b05fe6f8af73bf77e1700212a28f36be6733d3a",
"reference": "8b05fe6f8af73bf77e1700212a28f36be6733d3a",
"shasum": ""
},
"require": {
"php": ">=7.3"
"php": ">=7.4"
},
"require-dev": {
"phpunit/phpunit": "^9.3",
@ -1896,27 +1896,27 @@
],
"support": {
"issues": "https://github.com/utopia-php/registry/issues",
"source": "https://github.com/utopia-php/registry/tree/0.2.4"
"source": "https://github.com/utopia-php/registry/tree/0.3.0"
},
"time": "2020-10-24T08:51:37+00:00"
"time": "2021-03-09T17:38:40+00:00"
},
{
"name": "utopia-php/storage",
"version": "0.4.1",
"version": "0.4.3",
"source": {
"type": "git",
"url": "https://github.com/utopia-php/storage.git",
"reference": "86f749f2d79268528732e560f77dde0155e162ca"
"reference": "9db3ab713a6d392c3c2c799aeea751f6c8dc2ff7"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/utopia-php/storage/zipball/86f749f2d79268528732e560f77dde0155e162ca",
"reference": "86f749f2d79268528732e560f77dde0155e162ca",
"url": "https://api.github.com/repos/utopia-php/storage/zipball/9db3ab713a6d392c3c2c799aeea751f6c8dc2ff7",
"reference": "9db3ab713a6d392c3c2c799aeea751f6c8dc2ff7",
"shasum": ""
},
"require": {
"php": ">=7.4",
"utopia-php/framework": "0.10.0"
"utopia-php/framework": "0.*.*"
},
"require-dev": {
"phpunit/phpunit": "^9.3",
@ -1948,9 +1948,9 @@
],
"support": {
"issues": "https://github.com/utopia-php/storage/issues",
"source": "https://github.com/utopia-php/storage/tree/0.4.1"
"source": "https://github.com/utopia-php/storage/tree/0.4.3"
},
"time": "2021-02-19T05:04:44+00:00"
"time": "2021-03-02T20:25:02+00:00"
},
{
"name": "utopia-php/swoole",
@ -2065,20 +2065,20 @@
},
{
"name": "webmozart/assert",
"version": "dev-master",
"version": "1.9.1",
"source": {
"type": "git",
"url": "https://github.com/webmozarts/assert.git",
"reference": "9c89b265ccc4092d58e66d72af5d343ee77a41ae"
"reference": "bafc69caeb4d49c39fd0779086c03a3738cbb389"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/webmozarts/assert/zipball/9c89b265ccc4092d58e66d72af5d343ee77a41ae",
"reference": "9c89b265ccc4092d58e66d72af5d343ee77a41ae",
"url": "https://api.github.com/repos/webmozarts/assert/zipball/bafc69caeb4d49c39fd0779086c03a3738cbb389",
"reference": "bafc69caeb4d49c39fd0779086c03a3738cbb389",
"shasum": ""
},
"require": {
"php": "^7.2 || ^8.0",
"php": "^5.3.3 || ^7.0 || ^8.0",
"symfony/polyfill-ctype": "^1.8"
},
"conflict": {
@ -2086,15 +2086,9 @@
"vimeo/psalm": "<3.9.1"
},
"require-dev": {
"phpunit/phpunit": "^8.5.13"
"phpunit/phpunit": "^4.8.36 || ^7.5.13"
},
"default-branch": true,
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.10-dev"
}
},
"autoload": {
"psr-4": {
"Webmozart\\Assert\\": "src/"
@ -2118,9 +2112,9 @@
],
"support": {
"issues": "https://github.com/webmozarts/assert/issues",
"source": "https://github.com/webmozarts/assert/tree/master"
"source": "https://github.com/webmozarts/assert/tree/1.9.1"
},
"time": "2021-01-18T12:52:36+00:00"
"time": "2020-07-08T17:02:28+00:00"
}
],
"packages-dev": [
@ -3279,12 +3273,12 @@
"source": {
"type": "git",
"url": "https://github.com/phpDocumentor/ReflectionDocBlock.git",
"reference": "e3324ecbde7319b0bbcf0fd7ca4af19469c38da9"
"reference": "f8d350d8514ff60b5993dd0121c62299480c989c"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/phpDocumentor/ReflectionDocBlock/zipball/e3324ecbde7319b0bbcf0fd7ca4af19469c38da9",
"reference": "e3324ecbde7319b0bbcf0fd7ca4af19469c38da9",
"url": "https://api.github.com/repos/phpDocumentor/ReflectionDocBlock/zipball/f8d350d8514ff60b5993dd0121c62299480c989c",
"reference": "f8d350d8514ff60b5993dd0121c62299480c989c",
"shasum": ""
},
"require": {
@ -3328,7 +3322,7 @@
"issues": "https://github.com/phpDocumentor/ReflectionDocBlock/issues",
"source": "https://github.com/phpDocumentor/ReflectionDocBlock/tree/master"
},
"time": "2020-11-18T14:27:38+00:00"
"time": "2021-03-07T11:12:25+00:00"
},
{
"name": "phpdocumentor/type-resolver",
@ -3874,28 +3868,22 @@
},
{
"name": "psr/container",
"version": "dev-master",
"version": "1.1.x-dev",
"source": {
"type": "git",
"url": "https://github.com/php-fig/container.git",
"reference": "381524e8568e07f31d504a945b88556548c8c42e"
"reference": "8622567409010282b7aeebe4bb841fe98b58dcaf"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/php-fig/container/zipball/381524e8568e07f31d504a945b88556548c8c42e",
"reference": "381524e8568e07f31d504a945b88556548c8c42e",
"url": "https://api.github.com/repos/php-fig/container/zipball/8622567409010282b7aeebe4bb841fe98b58dcaf",
"reference": "8622567409010282b7aeebe4bb841fe98b58dcaf",
"shasum": ""
},
"require": {
"php": ">=7.2.0"
},
"default-branch": true,
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.1.x-dev"
}
},
"autoload": {
"psr-4": {
"Psr\\Container\\": "src/"
@ -3922,9 +3910,9 @@
],
"support": {
"issues": "https://github.com/php-fig/container/issues",
"source": "https://github.com/php-fig/container/tree/master"
"source": "https://github.com/php-fig/container/tree/1.1.x"
},
"time": "2020-10-13T07:07:53+00:00"
"time": "2021-03-05T17:36:06+00:00"
},
{
"name": "sebastian/cli-parser",
@ -4946,12 +4934,12 @@
"source": {
"type": "git",
"url": "https://github.com/symfony/console.git",
"reference": "c08d7d0d458eceb62996d81d3be8d9fbf5564ec4"
"reference": "4e102e4de39852a1dcd3b2169d263b88afee7fff"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/console/zipball/c08d7d0d458eceb62996d81d3be8d9fbf5564ec4",
"reference": "c08d7d0d458eceb62996d81d3be8d9fbf5564ec4",
"url": "https://api.github.com/repos/symfony/console/zipball/4e102e4de39852a1dcd3b2169d263b88afee7fff",
"reference": "4e102e4de39852a1dcd3b2169d263b88afee7fff",
"shasum": ""
},
"require": {
@ -5036,7 +5024,7 @@
"type": "tidelift"
}
],
"time": "2021-02-23T10:10:15+00:00"
"time": "2021-03-08T21:52:55+00:00"
},
{
"name": "symfony/polyfill-intl-grapheme",
@ -5456,17 +5444,17 @@
"source": {
"type": "git",
"url": "https://github.com/symfony/service-contracts.git",
"reference": "e830e6ceebd6377b019e4c9a523d6f2c27007e4a"
"reference": "96cd360b9f03a22a30cf5354e630c557bd3aac33"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/service-contracts/zipball/e830e6ceebd6377b019e4c9a523d6f2c27007e4a",
"reference": "e830e6ceebd6377b019e4c9a523d6f2c27007e4a",
"url": "https://api.github.com/repos/symfony/service-contracts/zipball/96cd360b9f03a22a30cf5354e630c557bd3aac33",
"reference": "96cd360b9f03a22a30cf5354e630c557bd3aac33",
"shasum": ""
},
"require": {
"php": ">=7.2.5",
"psr/container": "^1.0"
"psr/container": "^1.1"
},
"suggest": {
"symfony/service-implementation": ""
@ -5528,7 +5516,7 @@
"type": "tidelift"
}
],
"time": "2021-02-25T16:38:04+00:00"
"time": "2021-03-05T22:51:52+00:00"
},
{
"name": "symfony/string",