mirror of
https://github.com/appwrite/appwrite.git
synced 2026-11-18 20:36:03 +00:00
addressed greptile comments
This commit is contained in:
1 parent
acdeb28422
commit
1952079fee
4 files changed
+114
-16
No files matched your search
+23
-3
@@ -727,6 +727,11 @@ $server->onWorkerStart(function (int $workerId) use ($server, $register, $stats,
|
||||
if ($subscriptionDelta !== 0) {
|
||||
$register->get('telemetry.workerSubscriptionCounter')->add($subscriptionDelta, $register->get('telemetry.workerAttributes'));
|
||||
}
|
||||
|
||||
// Tail entries live outside the subscription tree, so the rebuild
|
||||
// above doesn't touch them. Drop any whose authorizing team role
|
||||
// the connection no longer holds (membership revoked / project moved).
|
||||
$eventTailRegistry->revalidateConnection($connection, $roles);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -815,9 +820,24 @@ $server->onWorkerStart(function (int $workerId) use ($server, $register, $stats,
|
||||
// whether it had regular subscribers ($total). The firehose is published
|
||||
// unconditionally, so a project tailed from the console with no other
|
||||
// realtime clients must still be observed here.
|
||||
$src = $event['project'] ?? '';
|
||||
if ($src !== '' && $src !== 'console' && $eventTailRegistry->isTailed($src)) {
|
||||
$eventTailRegistry->ingest($src, Realtime::toTailMetadata($event), \microtime(true));
|
||||
//
|
||||
// Most events carry the owning project in $event['project']. But some
|
||||
// project-owned events (schema changes, deployments, rules, migrations,
|
||||
// project settings) are rerouted by fromPayload() to project='console';
|
||||
// for those the real project id lives in the `projects.<id>` channel.
|
||||
// Resolve that so the tail still receives them.
|
||||
$tailProjectId = $event['project'] ?? '';
|
||||
if ($tailProjectId === 'console') {
|
||||
$tailProjectId = '';
|
||||
foreach (($event['data']['channels'] ?? []) as $channel) {
|
||||
if (\str_starts_with($channel, 'projects.')) {
|
||||
$tailProjectId = \substr($channel, \strlen('projects.'));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if ($tailProjectId !== '' && $eventTailRegistry->isTailed($tailProjectId)) {
|
||||
$eventTailRegistry->ingest($tailProjectId, Realtime::toTailMetadata($event), \microtime(true));
|
||||
}
|
||||
});
|
||||
} catch (Throwable $th) {
|
||||
|
||||
@@ -13,6 +13,7 @@ use Utopia\WebSocket\Server;
|
||||
class EventTailRegistry
|
||||
{
|
||||
/** @var array<int, array<string, array<string, array{
|
||||
* role:string,
|
||||
* filters:array<string,mixed>,
|
||||
* tokens:float,
|
||||
* lastRefill:float,
|
||||
@@ -25,12 +26,18 @@ class EventTailRegistry
|
||||
/** @var array<string, array<int, array<string,true>>> projectId => connId => set of subIds */
|
||||
private array $projectIndex = [];
|
||||
|
||||
private int $rate;
|
||||
|
||||
private int $batchMax;
|
||||
|
||||
/**
|
||||
* @param int $rate tokens per second per subscription
|
||||
* @param int $batchMax max frames per flushed websocket frame (kept under setPackageMaxLength)
|
||||
*/
|
||||
public function __construct(private int $rate = 50, private int $batchMax = 200)
|
||||
public function __construct(int $rate = 50, int $batchMax = 200)
|
||||
{
|
||||
$this->rate = \max(1, $rate);
|
||||
$this->batchMax = \max(1, $batchMax);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -64,10 +71,12 @@ class EventTailRegistry
|
||||
* remove()/removeConnection() (i.e. unsubscribe or disconnect), matching the tree.
|
||||
*
|
||||
* @param array<string,mixed> $filters result of RuntimeQuery::compile()
|
||||
* @param string $role the team role this tail was authorized with
|
||||
*/
|
||||
public function add(int $connId, string $subId, string $projectId, array $filters, float $now): void
|
||||
public function add(int $connId, string $subId, string $projectId, array $filters, float $now, string $role = ''): void
|
||||
{
|
||||
$this->entries[$connId][$subId][$projectId] = [
|
||||
'role' => $role,
|
||||
'filters' => $filters,
|
||||
'tokens' => (float) $this->rate,
|
||||
'lastRefill' => $now,
|
||||
@@ -85,12 +94,7 @@ class EventTailRegistry
|
||||
public function remove(int $connId, string $subId): void
|
||||
{
|
||||
foreach (\array_keys($this->entries[$connId][$subId] ?? []) as $projectId) {
|
||||
$this->detachFromProjectIndex((string) $projectId, $connId, $subId);
|
||||
}
|
||||
|
||||
unset($this->entries[$connId][$subId]);
|
||||
if (empty($this->entries[$connId])) {
|
||||
unset($this->entries[$connId]);
|
||||
$this->detachEntry($connId, $subId, (string) $projectId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -101,7 +105,27 @@ class EventTailRegistry
|
||||
}
|
||||
}
|
||||
|
||||
private function detachFromProjectIndex(string $projectId, int $connId, string $subId): void
|
||||
/**
|
||||
* Drop any tail whose authorizing team role the connection no longer holds. Called
|
||||
* from the permissions-changed path (which already recomputes a connection's roles
|
||||
* and rebuilds its tree) so a membership revocation or project transfer stops the
|
||||
* tail mid-connection instead of streaming until disconnect.
|
||||
*
|
||||
* @param array<int,string> $roles the connection's current roles
|
||||
*/
|
||||
public function revalidateConnection(int $connId, array $roles): void
|
||||
{
|
||||
foreach (($this->entries[$connId] ?? []) as $subId => $byProject) {
|
||||
foreach ($byProject as $projectId => $entry) {
|
||||
$role = $entry['role'];
|
||||
if ($role !== '' && !\in_array($role, $roles, true)) {
|
||||
$this->detachEntry($connId, (string) $subId, (string) $projectId);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function detachEntry(int $connId, string $subId, string $projectId): void
|
||||
{
|
||||
unset($this->projectIndex[$projectId][$connId][$subId]);
|
||||
if (empty($this->projectIndex[$projectId][$connId])) {
|
||||
@@ -110,6 +134,14 @@ class EventTailRegistry
|
||||
if (empty($this->projectIndex[$projectId])) {
|
||||
unset($this->projectIndex[$projectId]);
|
||||
}
|
||||
|
||||
unset($this->entries[$connId][$subId][$projectId]);
|
||||
if (empty($this->entries[$connId][$subId])) {
|
||||
unset($this->entries[$connId][$subId]);
|
||||
}
|
||||
if (empty($this->entries[$connId])) {
|
||||
unset($this->entries[$connId]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -118,8 +118,9 @@ class Subscribe extends Action
|
||||
);
|
||||
}
|
||||
$teamId = (string) $project->getAttribute('teamId', '');
|
||||
$requiredRole = Role::team($teamId)->toString();
|
||||
|
||||
if ($teamId === '' || !\in_array(Role::team($teamId)->toString(), $roles, true)) {
|
||||
if ($teamId === '' || !\in_array($requiredRole, $roles, true)) {
|
||||
throw new Exception(
|
||||
Exception::REALTIME_POLICY_VIOLATION,
|
||||
'Not authorized to tail project "' . $targetProjectId . '".'
|
||||
@@ -127,7 +128,9 @@ class Subscribe extends Action
|
||||
}
|
||||
|
||||
$compiled ??= RuntimeQuery::compile($parsedPayload['queries']);
|
||||
$pendingTails[] = [$parsedPayload['subscriptionId'], $targetProjectId, $compiled];
|
||||
// Carry the authorizing role so the tail can be revoked if the viewer
|
||||
// later loses team membership (or the project is transferred).
|
||||
$pendingTails[] = [$parsedPayload['subscriptionId'], $targetProjectId, $compiled, $requiredRole];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -146,8 +149,8 @@ class Subscribe extends Action
|
||||
// registry is keyed by (connId, subId, projectId): re-subscribing the same channel
|
||||
// overwrites its filter in place, while a reused subscription id that names a new
|
||||
// tail channel keeps the ones already registered (mirrors the tree).
|
||||
foreach ($pendingTails as [$subId, $targetProjectId, $compiled]) {
|
||||
$eventTailRegistry->add($connectionId, $subId, $targetProjectId, $compiled, $now);
|
||||
foreach ($pendingTails as [$subId, $targetProjectId, $compiled, $role]) {
|
||||
$eventTailRegistry->add($connectionId, $subId, $targetProjectId, $compiled, $now, $role);
|
||||
}
|
||||
|
||||
$subscriptionsAfter = \count($realtime->getSubscriptionMetadata($connectionId));
|
||||
|
||||
@@ -535,4 +535,47 @@ final class EventTailRegistryTest extends TestCase
|
||||
$this->assertContains('console.tail.projA', $channels, 'projA stopped delivering after projB was added');
|
||||
$this->assertContains('console.tail.projB', $channels);
|
||||
}
|
||||
|
||||
public function testRateClampedToAtLeastOne(): void
|
||||
{
|
||||
// A misconfigured non-positive rate must not disable the tail entirely.
|
||||
foreach ([0, -5] as $badRate) {
|
||||
$registry = new EventTailRegistry(rate: $badRate);
|
||||
$registry->add(1, 'x', 'projA', $this->compile([]), 0.0);
|
||||
$registry->ingest('projA', ['type' => 'databases'], 0.0);
|
||||
|
||||
$sent = [];
|
||||
$registry->flush($this->server($sent));
|
||||
|
||||
$eventFrames = array_values(array_filter($sent, fn ($f) => $f['data']['events'] === ['console.tail']));
|
||||
$this->assertCount(1, $eventFrames, "rate {$badRate} must clamp to >=1 so events still flow");
|
||||
$this->assertCount(1, $eventFrames[0]['data']['payload']);
|
||||
}
|
||||
}
|
||||
|
||||
public function testRevalidateConnectionDropsTailsWithRevokedRole(): void
|
||||
{
|
||||
$registry = new EventTailRegistry(rate: 100);
|
||||
$registry->add(1, 'x', 'projA', $this->compile([]), 0.0, 'team:teamA');
|
||||
$registry->add(1, 'y', 'projB', $this->compile([]), 0.0, 'team:teamB');
|
||||
$this->assertTrue($registry->isTailed('projA'));
|
||||
$this->assertTrue($registry->isTailed('projB'));
|
||||
|
||||
// The connection keeps teamB membership but loses teamA.
|
||||
$registry->revalidateConnection(1, ['user:u1', 'team:teamB']);
|
||||
|
||||
$this->assertFalse($registry->isTailed('projA'), 'tail for the revoked team must be dropped');
|
||||
$this->assertTrue($registry->isTailed('projB'), 'tail for the still-held team must remain');
|
||||
}
|
||||
|
||||
public function testRevalidateConnectionKeepsEntriesWithoutRole(): void
|
||||
{
|
||||
// Entries registered without an authorizing role are never pruned (defensive).
|
||||
$registry = new EventTailRegistry(rate: 100);
|
||||
$registry->add(1, 'x', 'projA', $this->compile([]), 0.0);
|
||||
|
||||
$registry->revalidateConnection(1, ['user:u1']);
|
||||
|
||||
$this->assertTrue($registry->isTailed('projA'));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user