mirror of
https://github.com/appwrite/appwrite.git
synced 2026-11-18 20:36:03 +00:00
refactor: improve EventTailRegistry class structure and documentation
This commit is contained in:
1 parent
d207fa74fa
commit
7a3580b549
1 file changed
+86
-34
@@ -1,23 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace Appwrite\Realtime;
|
||||
|
||||
use Appwrite\Utopia\Database\RuntimeQuery;
|
||||
use Utopia\WebSocket\Server;
|
||||
|
||||
// token bucket + buffers per subId of a project
|
||||
class EventTailRegistry{
|
||||
/** @var array<string, array{
|
||||
* connId:int,
|
||||
* projectId:string,
|
||||
* filters:array,
|
||||
* tokens:float,
|
||||
* lastRefill:float,
|
||||
* dropped:int,
|
||||
* delivered:int,
|
||||
* buffer:array<int,array>
|
||||
* }>
|
||||
* keyed by subscriptionId */
|
||||
private array $subs;
|
||||
// token bucket + buffers per subId of a project per worker
|
||||
class EventTailRegistry
|
||||
{
|
||||
/** @var array<string, array{
|
||||
* connId:int,
|
||||
* projectId:string,
|
||||
* filters:array<string,mixed>,
|
||||
* tokens:float,
|
||||
* lastRefill:float,
|
||||
* dropped:int,
|
||||
* delivered:int,
|
||||
* buffer:array<int,array<string,mixed>>
|
||||
* }> keyed by subscriptionId */
|
||||
private array $subs = [];
|
||||
|
||||
/** @var array<string, array<string,true>> projectId => set of subIds (hot-path lookup) */
|
||||
private array $projectSubscriptionsMap = [];
|
||||
@@ -25,20 +26,56 @@ class EventTailRegistry{
|
||||
/** @var array<int, array<string,true>> connId => set of subIds (cleanup on close) */
|
||||
private array $byConnection = [];
|
||||
|
||||
/** @var array<string,string> projectId => teamId (resolved at subscribe) */
|
||||
/** @var array<string,string> projectId => teamId (resolved once per worker at subscribe) */
|
||||
private array $teamCache = [];
|
||||
|
||||
/**
|
||||
* @param $rate tokens per second per subscriptions
|
||||
* @param $batchMax max frame per flush
|
||||
* @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)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the tail channel string for a project.
|
||||
*/
|
||||
public static function channel(string $projectId): string
|
||||
{
|
||||
return CONSOLE_TAIL_CHANNEL_PREFIX . '.' . $projectId;
|
||||
}
|
||||
|
||||
public static function projectFromChannel(string $channel): ?string
|
||||
{
|
||||
$prefix = CONSOLE_TAIL_CHANNEL_PREFIX . '.';
|
||||
if (!\str_starts_with($channel, $prefix)) {
|
||||
return null;
|
||||
}
|
||||
$projectId = \substr($channel, \strlen($prefix));
|
||||
return $projectId === '' ? null : $projectId;
|
||||
}
|
||||
|
||||
public function cachedTeam(string $projectId): ?string
|
||||
{
|
||||
return $this->teamCache[$projectId] ?? null;
|
||||
}
|
||||
|
||||
public function cacheTeam(string $projectId, string $teamId): void
|
||||
{
|
||||
$this->teamCache[$projectId] = $teamId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a tail subscription owned by this worker.
|
||||
*
|
||||
* @param array<string,mixed> $filters result of RuntimeQuery::compile()
|
||||
*/
|
||||
public function __construct(private int $rate=50, private int $batchMax=200) {}
|
||||
public function add(int $connId, string $subId, string $projectId, array $filters, float $now): void
|
||||
{
|
||||
$this->subs[$subId] = [
|
||||
'connId' => $connId,
|
||||
'projectId' => $projectId,
|
||||
'filters' => $filters, // RuntimeQuery::compile(...) result
|
||||
'filters' => $filters,
|
||||
'tokens' => (float) $this->rate,
|
||||
'lastRefill' => $now,
|
||||
'dropped' => 0,
|
||||
@@ -49,38 +86,51 @@ class EventTailRegistry{
|
||||
$this->byConnection[$connId][$subId] = true;
|
||||
}
|
||||
|
||||
public function remove(string $subId){
|
||||
public function remove(string $subId): void
|
||||
{
|
||||
$sub = $this->subs[$subId] ?? null;
|
||||
if ($sub === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
unset($this->projectSubscriptionsMap[$sub['projectId']][$subId]);
|
||||
if (empty($this->projectSubscriptionsMap[$sub['projectId']])) {
|
||||
unset($this->projectSubscriptionsMap[$sub['projectId']]);
|
||||
}
|
||||
|
||||
unset($this->byConnection[$sub['connId']][$subId]);
|
||||
if (empty($this->byConnection[$sub['connId']])) {
|
||||
unset($this->byConnection[$sub['connId']]);
|
||||
}
|
||||
|
||||
unset($this->subs[$subId]);
|
||||
}
|
||||
|
||||
public function removeConnection(int $connId){
|
||||
public function removeConnection(int $connId): void
|
||||
{
|
||||
foreach (\array_keys($this->byConnection[$connId] ?? []) as $subId) {
|
||||
$this->remove($subId);
|
||||
}
|
||||
}
|
||||
|
||||
public function ingest(string $projectId, array $payload, float $now){
|
||||
// filter + sampler
|
||||
foreach(array_keys($this->projectSubscriptionsMap[$projectId] ?? []) as $subId){
|
||||
public function isTailed(string $projectId): bool
|
||||
{
|
||||
return isset($this->projectSubscriptionsMap[$projectId]);
|
||||
}
|
||||
|
||||
public function ingest(string $projectId, array $payload, float $now): void
|
||||
{
|
||||
foreach (\array_keys($this->projectSubscriptionsMap[$projectId] ?? []) as $subId) {
|
||||
$sub = &$this->subs[$subId];
|
||||
|
||||
if(RuntimeQuery::filter($sub['filters'], $payload) === null) continue;
|
||||
if (RuntimeQuery::filter($sub['filters'], $payload) === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$elapsed = \max(0.0, $now - $sub['lastRefill']);
|
||||
// tokens will be always a fractional value lazily increasing with the rate
|
||||
$sub['tokens'] = \min((float) $this->rate, $sub['tokens'] + $elapsed*$this->rate);
|
||||
$elapsed = \max(0.0, $now - $sub['lastRefill']);
|
||||
$sub['tokens'] = \min((float) $this->rate, $sub['tokens'] + $elapsed * $this->rate);
|
||||
$sub['lastRefill'] = $now;
|
||||
|
||||
if ($sub['tokens'] >= 1.0) {
|
||||
$sub['tokens'] -= 1.0;
|
||||
@@ -93,13 +143,14 @@ class EventTailRegistry{
|
||||
unset($sub);
|
||||
}
|
||||
}
|
||||
public function flush(Server $server){
|
||||
|
||||
public function flush(Server $server): void
|
||||
{
|
||||
foreach ($this->subs as $subId => &$sub) {
|
||||
$channel = CONSOLE_TAIL_CHANNEL_PREFIX . '.' . $sub['projectId'];
|
||||
$channel = self::channel($sub['projectId']);
|
||||
|
||||
if (!empty($sub['buffer'])) {
|
||||
// TODO: if count($sub['buffer']) or encoded size could exceed batchMax /
|
||||
// setPackageMaxLength(64000), split into chunks and send multiple frames.
|
||||
// Chunk so a single frame stays under the websocket package max length.
|
||||
foreach (\array_chunk($sub['buffer'], $this->batchMax) as $chunk) {
|
||||
$server->send([$sub['connId']], (string) \json_encode([
|
||||
'type' => 'event',
|
||||
@@ -114,7 +165,7 @@ class EventTailRegistry{
|
||||
$sub['buffer'] = [];
|
||||
}
|
||||
|
||||
// counter frame (rate + dropped) — emit when something was dropped
|
||||
// Counter frame — emit the true rate vs dropped count when sampling kicked in.
|
||||
if ($sub['dropped'] > 0) {
|
||||
$server->send([$sub['connId']], (string) \json_encode([
|
||||
'type' => 'event',
|
||||
@@ -130,10 +181,11 @@ class EventTailRegistry{
|
||||
],
|
||||
]));
|
||||
}
|
||||
// TODO: decide reset cadence. Simplest: reset counters every flush.
|
||||
|
||||
// Reset counters every flush window (~150ms).
|
||||
$sub['delivered'] = 0;
|
||||
$sub['dropped'] = 0;
|
||||
}
|
||||
unset($sub);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user