fixed connection pool for http
This commit is contained in:
parent
288d7d3701
commit
295f2c9b7e
23
app/http.php
23
app/http.php
|
@ -3,9 +3,9 @@
|
|||
require_once __DIR__.'/../vendor/autoload.php';
|
||||
|
||||
use Appwrite\Database\Validator\Authorization;
|
||||
use Appwrite\Utopia\Response;
|
||||
use Utopia\Swoole\Files;
|
||||
use Utopia\Swoole\Request;
|
||||
use Appwrite\Utopia\Response;
|
||||
use Swoole\Process;
|
||||
use Swoole\Http\Server;
|
||||
use Swoole\Http\Request as SwooleRequest;
|
||||
|
@ -71,7 +71,7 @@ $http->on('start', function (Server $http) use ($payloadSize) {
|
|||
});
|
||||
});
|
||||
|
||||
$http->on('request', function (SwooleRequest $swooleRequest, SwooleResponse $swooleResponse) {
|
||||
$http->on('request', function (SwooleRequest $swooleRequest, SwooleResponse $swooleResponse) use ($register) {
|
||||
$request = new Request($swooleRequest);
|
||||
$response = new Response($swooleResponse);
|
||||
|
||||
|
@ -88,6 +88,17 @@ $http->on('request', function (SwooleRequest $swooleRequest, SwooleResponse $swo
|
|||
return;
|
||||
}
|
||||
|
||||
$db = $register->get('dbPool')->get();
|
||||
$redis = $register->get('redisPool')->get();
|
||||
|
||||
$register->set('db', function () use (&$db) {
|
||||
return $db;
|
||||
});
|
||||
|
||||
$register->set('cache', function () use (&$redis) { // Register cache connection
|
||||
return $redis;
|
||||
});
|
||||
|
||||
$app = new App('UTC');
|
||||
|
||||
try {
|
||||
|
@ -107,6 +118,14 @@ $http->on('request', function (SwooleRequest $swooleRequest, SwooleResponse $swo
|
|||
else {
|
||||
$swooleResponse->end('500: Server Error');
|
||||
}
|
||||
} finally {
|
||||
/** @var PDOPool $dbPool */
|
||||
$dbPool = $register->get('dbPool');
|
||||
$dbPool->put($db);
|
||||
|
||||
/** @var RedisPool $redisPool */
|
||||
$redisPool = $register->get('redisPool');
|
||||
$redisPool->put($redis);
|
||||
}
|
||||
});
|
||||
|
||||
|
|
29
app/init.php
29
app/init.php
|
@ -24,6 +24,8 @@ use Appwrite\Database\Database;
|
|||
use Appwrite\Database\Adapter\MySQL as MySQLAdapter;
|
||||
use Appwrite\Database\Adapter\Redis as RedisAdapter;
|
||||
use Appwrite\Database\Document;
|
||||
use Appwrite\Database\Pool\PDOPool;
|
||||
use Appwrite\Database\Pool\RedisPool;
|
||||
use Appwrite\Database\Validator\Authorization;
|
||||
use Appwrite\Event\Event;
|
||||
use Appwrite\Event\Realtime;
|
||||
|
@ -144,7 +146,32 @@ Database::addFilter('encrypt',
|
|||
/*
|
||||
* Registry
|
||||
*/
|
||||
$register->set('db', function () use ($register) {
|
||||
$register->set('dbPool', 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', '');
|
||||
$pool = new PDOPool(10, $dbHost, $dbScheme, $dbUser, $dbPass);
|
||||
|
||||
return $pool;
|
||||
});
|
||||
|
||||
$register->set('redisPool', function () {
|
||||
$user = App::getEnv('_APP_REDIS_USER', '');
|
||||
$pass = App::getEnv('_APP_REDIS_PASS', '');
|
||||
$auth = [];
|
||||
if ($user) {
|
||||
$auth[] = $user;
|
||||
}
|
||||
if ($pass) {
|
||||
$auth[] = $pass;
|
||||
}
|
||||
|
||||
$pool = new RedisPool(10, App::getEnv('_APP_REDIS_HOST', ''), App::getEnv('_APP_REDIS_PORT', ''), $auth);
|
||||
|
||||
return $pool;
|
||||
});
|
||||
$register->set('db', function () {
|
||||
$dbHost = App::getEnv('_APP_DB_HOST', '');
|
||||
$dbUser = App::getEnv('_APP_DB_USER', '');
|
||||
$dbPass = App::getEnv('_APP_DB_PASS', '');
|
||||
|
|
|
@ -38,32 +38,6 @@ $server->set([
|
|||
$subscriptions = [];
|
||||
$connections = [];
|
||||
|
||||
$register->set('dbPool', 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', '');
|
||||
$pool = new PDOPool(20, $dbHost, $dbScheme, $dbUser, $dbPass);
|
||||
|
||||
return $pool;
|
||||
});
|
||||
|
||||
$register->set('redisPool', function () {
|
||||
$user = App::getEnv('_APP_REDIS_USER', '');
|
||||
$pass = App::getEnv('_APP_REDIS_PASS', '');
|
||||
$auth = [];
|
||||
if ($user) {
|
||||
$auth[] = $user;
|
||||
}
|
||||
if ($pass) {
|
||||
$auth[] = $pass;
|
||||
}
|
||||
|
||||
$pool = new RedisPool(20, App::getEnv('_APP_REDIS_HOST', ''), App::getEnv('_APP_REDIS_PORT', ''), $auth);
|
||||
|
||||
return $pool;
|
||||
});
|
||||
|
||||
$server->on('workerStart', function ($server, $workerId) use (&$subscriptions, &$connections, &$register) {
|
||||
Console::success('Worker ' . $workerId . ' started succefully');
|
||||
|
||||
|
|
Loading…
Reference in a new issue