Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 39 additions & 36 deletions app/realtime.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Appwrite\Utopia\Database\Documents\User;
use Appwrite\Utopia\Request;
use Appwrite\Utopia\Response;
use Swoole\Coroutine;
use Swoole\Http\Request as SwooleRequest;
use Swoole\Http\Response as SwooleResponse;
use Swoole\Runtime;
Expand Down Expand Up @@ -52,14 +53,14 @@
if (!function_exists('getConsoleDB')) {
function getConsoleDB(): Database
{
global $register;

static $database = null;
$ctx = Coroutine::getContext();

if ($database !== null) {
return $database;
if (isset($ctx['dbForPlatform'])) {
return $ctx['dbForPlatform'];
}

global $register;

/** @var Group $pools */
$pools = $register->get('pools');

Expand All @@ -69,25 +70,27 @@ function getConsoleDB(): Database
->setNamespace('_console')
->setMetadata('host', \gethostname())
->setMetadata('project', '_console');

$database->setDocumentType('users', User::class);

return $database;
return $ctx['dbForPlatform'] = $database;
}
}

// Allows overriding
if (!function_exists('getProjectDB')) {
function getProjectDB(Document $project): Database
{
global $register;
$ctx = Coroutine::getContext();

static $databases = [];
if (!isset($ctx['dbForProject'])) {
$ctx['dbForProject'] = [];
}

if (isset($databases[$project->getSequence()])) {
return $databases[$project->getSequence()];
if (isset($ctx['dbForProject'][$project->getSequence()])) {
return $ctx['dbForProject'][$project->getSequence()];
}

global $register;

/** @var Group $pools */
$pools = $register->get('pools');

Expand Down Expand Up @@ -125,22 +128,22 @@ function getProjectDB(Document $project): Database

$database->setDocumentType('users', User::class);

return $databases[$project->getSequence()] = $database;
return $ctx['dbForProject'][$project->getSequence()] = $database;
}
}

// Allows overriding
if (!function_exists('getCache')) {
function getCache(): Cache
{
global $register;
$ctx = Coroutine::getContext();

static $cache = null;

if ($cache !== null) {
return $cache;
if (isset($ctx['cache'])) {
return $ctx['cache'];
}

global $register;

$pools = $register->get('pools'); /** @var Group $pools */

$list = Config::getParam('pools-cache', []);
Expand All @@ -150,18 +153,18 @@ function getCache(): Cache
$adapters[] = new CachePool($pools->get($value));
}

return $cache = new Cache(new Sharding($adapters));
return $ctx['cache'] = new Cache(new Sharding($adapters));
}
}

// Allows overriding
if (!function_exists('getRedis')) {
function getRedis(): \Redis
{
static $redis = null;
$ctx = Coroutine::getContext();

if ($redis !== null) {
return $redis;
if (isset($ctx['redis'])) {
return $ctx['redis'];
}

$host = System::getEnv('_APP_REDIS_HOST', 'localhost');
Expand All @@ -175,46 +178,46 @@ function getRedis(): \Redis
}
$redis->setOption(\Redis::OPT_READ_TIMEOUT, -1);

return $redis;
return $ctx['redis'] = $redis;
}
}

if (!function_exists('getTimelimit')) {
function getTimelimit(): TimeLimitRedis
function getTimelimit(string $key = "", int $limit = 0, int $seconds = 1): TimeLimitRedis
{
static $timelimit = null;
$ctx = Coroutine::getContext();

if ($timelimit !== null) {
return $timelimit;
if (isset($ctx['timelimit'])) {
return $ctx['timelimit'];
}

return $timelimit = new TimeLimitRedis("", 0, 1, getRedis());
return $ctx['timelimit'] = new TimeLimitRedis($key, $limit, $seconds, getRedis());
}
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

if (!function_exists('getRealtime')) {
function getRealtime(): Realtime
{
static $realtime = null;
$ctx = Coroutine::getContext();

if ($realtime !== null) {
return $realtime;
if (isset($ctx['realtime'])) {
return $ctx['realtime'];
}

return $realtime = new Realtime();
return $ctx['realtime'] = new Realtime();
}
}

if (!function_exists('getTelemetry')) {
function getTelemetry(int $workerId): Utopia\Telemetry\Adapter
{
static $telemetry = null;
$ctx = Coroutine::getContext();

if ($telemetry !== null) {
return $telemetry;
if (isset($ctx['telemetry'])) {
return $ctx['telemetry'];
}

return $telemetry = new NoTelemetry();
return $ctx['telemetry'] = new NoTelemetry();
}
}

Expand Down