From 0353b826ac01e60230092bddb8cd2d564c73902b Mon Sep 17 00:00:00 2001 From: ArnabChatterjee20k Date: Wed, 3 Dec 2025 21:32:22 +0530 Subject: [PATCH 1/4] Refactor: use Coroutine context for database and cache functions --- app/realtime.php | 69 ++++++++++++++++++++++++++---------------------- 1 file changed, 38 insertions(+), 31 deletions(-) diff --git a/app/realtime.php b/app/realtime.php index 3a68947cf6..9fb2f1ab48 100644 --- a/app/realtime.php +++ b/app/realtime.php @@ -8,6 +8,7 @@ use Appwrite\PubSub\Adapter\Pool as PubSubPool; 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; @@ -52,14 +53,14 @@ Runtime::enableCoroutine(SWOOLE_HOOK_ALL); if (!function_exists('getConsoleDB')) { function getConsoleDB(): Database { - global $register; + $ctx = Coroutine::getContext(); - static $database = null; - - if ($database !== null) { - return $database; + if (isset($ctx['consoleDB'])) { + return $ctx['consoleDB']; } + global $register; + /** @var Group $pools */ $pools = $register->get('pools'); @@ -70,9 +71,7 @@ if (!function_exists('getConsoleDB')) { ->setMetadata('host', \gethostname()) ->setMetadata('project', '_console'); - $database->setDocumentType('users', User::class); - - return $database; + return $ctx['consoleDB'] = $database; } } @@ -80,14 +79,18 @@ if (!function_exists('getConsoleDB')) { if (!function_exists('getProjectDB')) { function getProjectDB(Document $project): Database { - global $register; + $ctx = Coroutine::getContext(); - static $databases = []; - - if (isset($databases[$project->getSequence()])) { - return $databases[$project->getSequence()]; + if (!isset($ctx['getProjectDB'])) { + $ctx['getProjectDB'] = []; } + if (isset($ctx['getProjectDB'][$project->getSequence()])) { + return $ctx['getProjectDB'][$project->getSequence()]; + } + + global $register; + /** @var Group $pools */ $pools = $register->get('pools'); @@ -123,9 +126,7 @@ if (!function_exists('getProjectDB')) { ->setMetadata('host', \gethostname()) ->setMetadata('project', $project->getId()); - $database->setDocumentType('users', User::class); - - return $databases[$project->getSequence()] = $database; + return $ctx['getProjectDB'][$project->getSequence()] = $database; } } @@ -133,14 +134,14 @@ if (!function_exists('getProjectDB')) { if (!function_exists('getCache')) { function getCache(): Cache { - global $register; + $ctx = Coroutine::getContext(); - static $cache = null; - - if ($cache !== null) { - return $cache; + if (isset($ctx['getCache'])) { + return $ctx['getCache']; } + global $register; + $pools = $register->get('pools'); /** @var Group $pools */ $list = Config::getParam('pools-cache', []); @@ -150,7 +151,7 @@ if (!function_exists('getCache')) { $adapters[] = new CachePool($pools->get($value)); } - return $cache = new Cache(new Sharding($adapters)); + return $ctx['getCache'] = new Cache(new Sharding($adapters)); } } @@ -158,10 +159,10 @@ if (!function_exists('getCache')) { if (!function_exists('getRedis')) { function getRedis(): \Redis { - static $redis = null; + $ctx = Coroutine::getContext(); - if ($redis !== null) { - return $redis; + if (isset($ctx['getRedis'])) { + return $ctx['getRedis']; } $host = System::getEnv('_APP_REDIS_HOST', 'localhost'); @@ -175,33 +176,39 @@ if (!function_exists('getRedis')) { } $redis->setOption(\Redis::OPT_READ_TIMEOUT, -1); - return $redis; + return $ctx['getRedis'] = $redis; } } if (!function_exists('getTimelimit')) { function getTimelimit(): TimeLimitRedis { - static $timelimit = null; + $ctx = Coroutine::getContext(); - if ($timelimit !== null) { - return $timelimit; + if (isset($ctx['getTimelimit'])) { + return $ctx['getTimelimit']; } - return $timelimit = new TimeLimitRedis("", 0, 1, getRedis()); + return $ctx['getTimelimit'] = new TimeLimitRedis("", 0, 1, getRedis()); } } if (!function_exists('getRealtime')) { function getRealtime(): Realtime { + $ctx = Coroutine::getContext(); + + if (isset($ctx['getRealtime'])) { + return $ctx['getRealtime']; + } + static $realtime = null; if ($realtime !== null) { return $realtime; } - return $realtime = new Realtime(); + return $ctx['getRealtime'] = new Realtime(); } } From 5878a82b8a0fe9090f95c3c01d9323d719dfcf73 Mon Sep 17 00:00:00 2001 From: ArnabChatterjee20k Date: Wed, 3 Dec 2025 21:54:45 +0530 Subject: [PATCH 2/4] add user class --- app/realtime.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/realtime.php b/app/realtime.php index 9fb2f1ab48..f924d4ab0f 100644 --- a/app/realtime.php +++ b/app/realtime.php @@ -70,7 +70,7 @@ if (!function_exists('getConsoleDB')) { ->setNamespace('_console') ->setMetadata('host', \gethostname()) ->setMetadata('project', '_console'); - + $database->setDocumentType('users', User::class); return $ctx['consoleDB'] = $database; } } @@ -126,6 +126,8 @@ if (!function_exists('getProjectDB')) { ->setMetadata('host', \gethostname()) ->setMetadata('project', $project->getId()); + $database->setDocumentType('users', User::class); + return $ctx['getProjectDB'][$project->getSequence()] = $database; } } From c4cc8595d66d73d11781bf781fcdff98f9a4054e Mon Sep 17 00:00:00 2001 From: ArnabChatterjee20k Date: Wed, 3 Dec 2025 22:08:05 +0530 Subject: [PATCH 3/4] removed redundant static realtime --- app/realtime.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/app/realtime.php b/app/realtime.php index f924d4ab0f..3e575b4439 100644 --- a/app/realtime.php +++ b/app/realtime.php @@ -204,12 +204,6 @@ if (!function_exists('getRealtime')) { return $ctx['getRealtime']; } - static $realtime = null; - - if ($realtime !== null) { - return $realtime; - } - return $ctx['getRealtime'] = new Realtime(); } } From da1285bb22ff446d8d17c13640e5df0bc39f3e07 Mon Sep 17 00:00:00 2001 From: ArnabChatterjee20k Date: Fri, 5 Dec 2025 09:25:34 +0530 Subject: [PATCH 4/4] updated time limit key, cached resource --- app/realtime.php | 50 ++++++++++++++++++++++++------------------------ 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/app/realtime.php b/app/realtime.php index 3e575b4439..734dcd70bb 100644 --- a/app/realtime.php +++ b/app/realtime.php @@ -55,8 +55,8 @@ if (!function_exists('getConsoleDB')) { { $ctx = Coroutine::getContext(); - if (isset($ctx['consoleDB'])) { - return $ctx['consoleDB']; + if (isset($ctx['dbForPlatform'])) { + return $ctx['dbForPlatform']; } global $register; @@ -71,7 +71,7 @@ if (!function_exists('getConsoleDB')) { ->setMetadata('host', \gethostname()) ->setMetadata('project', '_console'); $database->setDocumentType('users', User::class); - return $ctx['consoleDB'] = $database; + return $ctx['dbForPlatform'] = $database; } } @@ -81,12 +81,12 @@ if (!function_exists('getProjectDB')) { { $ctx = Coroutine::getContext(); - if (!isset($ctx['getProjectDB'])) { - $ctx['getProjectDB'] = []; + if (!isset($ctx['dbForProject'])) { + $ctx['dbForProject'] = []; } - if (isset($ctx['getProjectDB'][$project->getSequence()])) { - return $ctx['getProjectDB'][$project->getSequence()]; + if (isset($ctx['dbForProject'][$project->getSequence()])) { + return $ctx['dbForProject'][$project->getSequence()]; } global $register; @@ -128,7 +128,7 @@ if (!function_exists('getProjectDB')) { $database->setDocumentType('users', User::class); - return $ctx['getProjectDB'][$project->getSequence()] = $database; + return $ctx['dbForProject'][$project->getSequence()] = $database; } } @@ -138,8 +138,8 @@ if (!function_exists('getCache')) { { $ctx = Coroutine::getContext(); - if (isset($ctx['getCache'])) { - return $ctx['getCache']; + if (isset($ctx['cache'])) { + return $ctx['cache']; } global $register; @@ -153,7 +153,7 @@ if (!function_exists('getCache')) { $adapters[] = new CachePool($pools->get($value)); } - return $ctx['getCache'] = new Cache(new Sharding($adapters)); + return $ctx['cache'] = new Cache(new Sharding($adapters)); } } @@ -163,8 +163,8 @@ if (!function_exists('getRedis')) { { $ctx = Coroutine::getContext(); - if (isset($ctx['getRedis'])) { - return $ctx['getRedis']; + if (isset($ctx['redis'])) { + return $ctx['redis']; } $host = System::getEnv('_APP_REDIS_HOST', 'localhost'); @@ -178,20 +178,20 @@ if (!function_exists('getRedis')) { } $redis->setOption(\Redis::OPT_READ_TIMEOUT, -1); - return $ctx['getRedis'] = $redis; + return $ctx['redis'] = $redis; } } if (!function_exists('getTimelimit')) { - function getTimelimit(): TimeLimitRedis + function getTimelimit(string $key = "", int $limit = 0, int $seconds = 1): TimeLimitRedis { $ctx = Coroutine::getContext(); - if (isset($ctx['getTimelimit'])) { - return $ctx['getTimelimit']; + if (isset($ctx['timelimit'])) { + return $ctx['timelimit']; } - return $ctx['getTimelimit'] = new TimeLimitRedis("", 0, 1, getRedis()); + return $ctx['timelimit'] = new TimeLimitRedis($key, $limit, $seconds, getRedis()); } } @@ -200,24 +200,24 @@ if (!function_exists('getRealtime')) { { $ctx = Coroutine::getContext(); - if (isset($ctx['getRealtime'])) { - return $ctx['getRealtime']; + if (isset($ctx['realtime'])) { + return $ctx['realtime']; } - return $ctx['getRealtime'] = 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(); } }