mirror of
https://github.com/appwrite/appwrite.git
synced 2026-11-18 20:36:03 +00:00
refactor(cache): match the NOSCRIPT error code, not a substring
Redis error replies carry no numeric code (getCode() is 0 on both the raw socket and php-redis); the code is the reply's leading token — "NOSCRIPT No matching script." Match that token via NoScript::matches() instead of a substring anywhere in the message, so echoed key/value text containing the word can't false-trigger the EVAL fallback. Centralizes the literal and the NoScript construction (NoScript::from) across both adapters. Addresses review. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
1 parent
7cde69437e
commit
a494130eea
4 files changed
+78
-10
No files matched your search
@@ -200,20 +200,22 @@ class Redis extends Leasable implements Adapter, Retryable
|
||||
try {
|
||||
$result = $this->redis->evalSha($sha, [$key, ...$args], 1);
|
||||
} catch (\RedisException $e) {
|
||||
if (\str_contains($e->getMessage(), 'NOSCRIPT')) {
|
||||
throw new NoScript($e->getMessage(), 0, $e);
|
||||
// php-redis may throw NOSCRIPT rather than returning false.
|
||||
if (NoScript::matches($e->getMessage())) {
|
||||
throw NoScript::from($e);
|
||||
}
|
||||
|
||||
throw $e;
|
||||
}
|
||||
|
||||
// php-redis reports a script error as false + getLastError() rather
|
||||
// than a throw. A lease script always returns an int on success, so
|
||||
// false is an error; NOSCRIPT means leaseRun() should resend the body.
|
||||
if ($result === false && \str_contains((string) $this->redis->getLastError(), 'NOSCRIPT')) {
|
||||
// ...or it reports the same error as false + getLastError(). A lease
|
||||
// script always returns an int on success, so false is an error to
|
||||
// inspect; NOSCRIPT means leaseRun() should resend the body.
|
||||
$error = (string) $this->redis->getLastError();
|
||||
if ($result === false && NoScript::matches($error)) {
|
||||
$this->redis->clearLastError();
|
||||
|
||||
throw new NoScript('NOSCRIPT');
|
||||
throw NoScript::from($error);
|
||||
}
|
||||
|
||||
return $result;
|
||||
|
||||
@@ -183,9 +183,9 @@ class Multiplexing extends Leasable implements Adapter, TelemetryFeature
|
||||
} catch (\RedisException $e) {
|
||||
// NOSCRIPT is a server reply, not a connection fault: signal leaseRun()
|
||||
// to resend the body. ConnectionException (a RedisException subclass)
|
||||
// never carries this message, so reconnect handling is unaffected.
|
||||
if (\str_contains($e->getMessage(), 'NOSCRIPT')) {
|
||||
throw new NoScript($e->getMessage(), 0, $e);
|
||||
// never carries this code, so reconnect handling is unaffected.
|
||||
if (NoScript::matches($e->getMessage())) {
|
||||
throw NoScript::from($e);
|
||||
}
|
||||
|
||||
throw $e;
|
||||
|
||||
@@ -10,4 +10,29 @@ namespace Utopia\Cache\Adapter\Redis;
|
||||
*/
|
||||
final class NoScript extends \RuntimeException
|
||||
{
|
||||
/**
|
||||
* RESP error code for an EVALSHA cache miss. Redis error replies carry no
|
||||
* numeric code (getCode() is 0 on both the raw socket and php-redis), so the
|
||||
* code is the reply's leading token — "NOSCRIPT No matching script." — the
|
||||
* same across the raw RESP frame and php-redis getLastError().
|
||||
*/
|
||||
private const CODE = 'NOSCRIPT';
|
||||
|
||||
/**
|
||||
* True when a Redis error string's code (its leading token) is NOSCRIPT.
|
||||
* Matching the token, not a substring, avoids false positives on echoed key
|
||||
* or value text that merely contains the word.
|
||||
*/
|
||||
public static function matches(string $error): bool
|
||||
{
|
||||
return \explode(' ', $error, 2)[0] === self::CODE;
|
||||
}
|
||||
|
||||
/** Build the signal from the underlying Redis error (exception or message). */
|
||||
public static function from(\Throwable|string $reason): self
|
||||
{
|
||||
return $reason instanceof \Throwable
|
||||
? new self($reason->getMessage(), 0, $reason)
|
||||
: new self($reason);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace Utopia\Tests\Unit\Redis;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Utopia\Cache\Adapter\Redis\NoScript;
|
||||
|
||||
class NoScriptTest extends TestCase
|
||||
{
|
||||
public function testMatchesWhenNoScriptIsTheLeadingCodeToken(): void
|
||||
{
|
||||
$this->assertTrue(NoScript::matches('NOSCRIPT No matching script. Please use EVAL.'));
|
||||
$this->assertTrue(NoScript::matches('NOSCRIPT'));
|
||||
}
|
||||
|
||||
public function testDoesNotMatchWhenNoScriptIsNotTheCode(): void
|
||||
{
|
||||
// A substring check would wrongly trip on echoed key/value text or a
|
||||
// different error code — the code is only ever the leading token.
|
||||
$this->assertFalse(NoScript::matches('ERR user set key to "NOSCRIPT"'));
|
||||
$this->assertFalse(NoScript::matches('WRONGTYPE Operation against a key holding the wrong kind of value'));
|
||||
$this->assertFalse(NoScript::matches(''));
|
||||
}
|
||||
|
||||
public function testFromExceptionPreservesMessageAndCause(): void
|
||||
{
|
||||
$cause = new \RedisException('NOSCRIPT No matching script. Please use EVAL.');
|
||||
$signal = NoScript::from($cause);
|
||||
|
||||
$this->assertSame($cause->getMessage(), $signal->getMessage());
|
||||
$this->assertSame($cause, $signal->getPrevious());
|
||||
}
|
||||
|
||||
public function testFromStringCarriesMessage(): void
|
||||
{
|
||||
$signal = NoScript::from('NOSCRIPT No matching script. Please use EVAL.');
|
||||
|
||||
$this->assertSame('NOSCRIPT No matching script. Please use EVAL.', $signal->getMessage());
|
||||
$this->assertNull($signal->getPrevious());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user