fix: track multiplexing pending depth as up/down counter

The gauge was sampled only after enqueue, so its minimum reading was
always 1 and it never decreased on dequeue — drained queues kept
reporting their last enqueue-time depth. Switch to an UpDownCounter so
+1 on enqueue and -1 on each dequeue (reader path and teardown) yield
the true real-time depth without periodic sampling.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
loks0nandClaude Opus 4.7 committed 2026-05-19 15:28:44 +01:00
1 parent ccbb5477e3
commit 8366a19739
1 file changed
+10 -8
+10 -8
View File
@@ -11,7 +11,7 @@ use Utopia\Cache\Adapter;
use Utopia\Cache\Feature\Telemetry as TelemetryFeature;
use Utopia\Telemetry\Adapter as Telemetry;
use Utopia\Telemetry\Adapter\None as NoTelemetry;
use Utopia\Telemetry\Gauge;
use Utopia\Telemetry\UpDownCounter;
/**
* Redis\Multiplexing adapter.
@@ -33,7 +33,7 @@ class Multiplexing implements Adapter, TelemetryFeature
*/
private Lock $sendLock;
private ?Gauge $pendingDepth = null;
private ?UpDownCounter $pendingDepth = null;
/**
* @param string $host
@@ -70,14 +70,14 @@ class Multiplexing implements Adapter, TelemetryFeature
}
/**
* Wire a telemetry adapter for connection-level metrics. The current
* pending-queue depth is recorded after each enqueue — a steady-state
* non-zero value means callers are queueing faster than Redis is
* replying.
* Wire a telemetry adapter for connection-level metrics. The pending-queue
* depth is tracked as an up/down counter — incremented on enqueue and
* decremented on dequeue — so a steady-state non-zero value means callers
* are queueing faster than Redis is replying.
*/
public function setTelemetry(Telemetry $telemetry): void
{
$this->pendingDepth = $telemetry->createGauge(
$this->pendingDepth = $telemetry->createUpDownCounter(
'cache.redis_multiplexing.pending.depth',
description: 'Pending response channels awaiting RESP frames on the multiplexed connection.',
);
@@ -228,7 +228,7 @@ class Multiplexing implements Adapter, TelemetryFeature
$error = null;
$context->pending->enqueue($response);
$this->pendingDepth?->record($context->pending->count());
$this->pendingDepth?->add(1);
try {
$context->client->send(Client::encode($args));
} catch (ConnectionException $sendError) {
@@ -340,6 +340,7 @@ class Multiplexing implements Adapter, TelemetryFeature
while (! $context->pending->isEmpty()) {
$ch = $context->pending->dequeue();
if ($ch instanceof Channel) {
$this->pendingDepth?->add(-1);
$ch->push(new ConnectionError($error));
}
}
@@ -411,6 +412,7 @@ class Multiplexing implements Adapter, TelemetryFeature
// A complete frame implies a prior pending enqueue.
$waiting = $context->pending->isEmpty() ? null : $context->pending->dequeue();
if ($waiting instanceof Channel) {
$this->pendingDepth?->add(-1);
$waiting->push($value);
} else {
// Should never happen given the send-lock invariant. Log