From 675448d48f24044cdfe80e4d37cd98439b3121d0 Mon Sep 17 00:00:00 2001 From: ArnabChatterjee20k Date: Fri, 18 Sep 2026 16:13:31 +0530 Subject: [PATCH] refactor(mqtt): inject cache resolver and planForUser into the handler The handler no longer service-locates getCache/getPlanForUser off the container by string key; both arrive as typed constructor params. The cache stays a resolver closure because the pool-backed cache can only be built inside a coroutine, so it is resolved lazily per coroutine as before. --- app/mqtt.php | 2 +- src/Appwrite/Mqtt/Handler.php | 11 +++++------ 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/app/mqtt.php b/app/mqtt.php index 9fc1531a98..fb12d76731 100644 --- a/app/mqtt.php +++ b/app/mqtt.php @@ -184,7 +184,7 @@ $mqtt = new Mqtt($telemetry, new PubSubPool($register->get('pools')->get('pubsub // The broker owns framing, decoding, dispatch, per-version encoding, packet ids, the QoS // handshake, keep-alive reaping and subscription matching. Appwrite policy lives in the Handler. -$handler = new Handler($container, $mqtt); +$handler = new Handler($container, $mqtt, $container->get('getCache'), $container->get('getPlanForUser')); $server = new Server($adapter, $handler); $server->setTelemetry($telemetry); // broker owns connection/packet/subscription metrics diff --git a/src/Appwrite/Mqtt/Handler.php b/src/Appwrite/Mqtt/Handler.php index 04638170b7..1c93bd0476 100644 --- a/src/Appwrite/Mqtt/Handler.php +++ b/src/Appwrite/Mqtt/Handler.php @@ -36,6 +36,8 @@ class Handler implements MqttHandler public function __construct( private readonly Container $container, private readonly Mqtt $mqtt, + private readonly \Closure $getCache, + private readonly \Closure $planForUser, ) { } @@ -227,8 +229,7 @@ class Handler implements MqttHandler $this->mqtt->messagesAcked->add(1); $this->mqtt->pubacksReceived->add(1); - $getCache = $this->container->get('getCache'); - $cache = $getCache(); + $cache = ($this->getCache)(); $key = $this->cursorKey($connection); $topic = $delivery['topic']; $cursor = (int) $delivery['cursor']; @@ -274,11 +275,9 @@ class Handler implements MqttHandler */ private function replayBacklog(Connection $connection, Database $projectDB, array $grantedTopics): void { - $getCache = $this->container->get('getCache'); - $getPlanForUser = $this->container->get('getPlanForUser'); - $cache = $getCache(); + $cache = ($this->getCache)(); $key = $this->cursorKey($connection); - $maxDepth = max(1, $getPlanForUser(new Document(['$id' => $connection->prefix]), $connection->identity['userId'] ?? '')); + $maxDepth = max(1, ($this->planForUser)(new Document(['$id' => $connection->prefix]), $connection->identity['userId'] ?? '')); $expiry = 0; foreach ($grantedTopics as [$document]) {