8000 [11.x] Proper rate limiter fix with phpredis serialization/compression enabled by TheLevti · Pull Request #54372 · laravel/framework · GitHub
[go: up one dir, main page]

Skip to content

[11.x] Proper rate limiter fix with phpredis serialization/compression enabled #54372

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
formatting
  • Loading branch information
taylorotwell committed Jan 27, 2025
commit cf87fa4fdbab998e1c5be6f3b8f3bd3543838ac1
22 changes: 14 additions & 8 deletions src/Illuminate/Cache/RateLimiter.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,16 @@ public function increment($key, $decaySeconds = 60, $amount = 1)
$key.':timer', $this->availableAt($decaySeconds), $decaySeconds
);

$added = $this->runClean(fn (): mixed => $this->cache->add($key, 0, $decaySeconds));
$added = $this->withoutSerializationOrCompression(
fn () => $this->cache->add($key, 0, $decaySeconds)
);

$hits = (int) $this->cache->increment($key, $amount);

if (! $added && $hits == 1) {
$this->runClean(fn (): mixed => $this->cache->put($key, 1, $decaySeconds));
$this->withoutSerializationOrCompression(
fn () => $this->cache->put($key, 1, $decaySeconds)
);
}

return $hits;
Expand Down Expand Up @@ -200,7 +204,7 @@ public function attempts($key)
{
$key = $this->cleanRateLimiterKey($key);

return $this->runClean(fn (): mixed => $this->cache->get($key, 0));
return $this->withoutSerializationOrCompression(fn () => $this->cache->get($key, 0));
}

/**
Expand Down Expand Up @@ -284,24 +288,26 @@ public function cleanRateLimiterKey($key)
}

/**
* Allow the rate limiter to run a callback against a clean repository.
* Execute the given callback without serialization or compression when applicable.
*
* For example when using RedisStore, the serializer and compressor can be
* disabled during the callback execution.
* @param callable $callback
* @return mixed
*/
protected function runClean(callable $callback): mixed
protected function withoutSerializationOrCompression(callable $callback)
{
$store = $this->cache->getStore();

if (! $store instanceof RedisStore) {
return $callback();
}

$connection = $store->connection();

if (! $connection instanceof PhpRedisConnection) {
return $callback();
}

return $connection->runClean($callback);
return $connection->withoutSerializationOrCompression($callback);
}

/**
Expand Down
72 changes: 37 additions & 35 deletions src/Illuminate/Redis/Connections/PacksPhpRedisValues.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,43 @@ public function pack(array $values): array
return array_map($processor, $values);
}

/**
* Execute the given callback without serialization or compression when applicable.
*
* @param callable $callback
* @return mixed
*/
public function withoutSerializationOrCompression(callable $callback)
{
$client = $this->client;

$oldSerializer = null;

if ($this->serialized()) {
$oldSerializer = $client->getOption($client::OPT_SERIALIZER);
$client->setOption($client::OPT_SERIALIZER, $client::SERIALIZER_NONE);
}

$oldCompressor = null;

if ($this->compressed()) {
$oldCompressor = $client->getOption($client::OPT_COMPRESSION);
$client->setOption($client::OPT_COMPRESSION, $client::COMPRESSION_NONE);
}

try {
return $callback();
} finally {
if ($oldSerializer !== null) {
$client->setOption($client::OPT_SERIALIZER, $oldSerializer);
}

if ($oldCompressor !== null) {
$client->setOption($client::OPT_COMPRESSION, $oldCompressor);
}
}
}

/**
* Determine if serialization is enabled.
*
Expand Down Expand Up @@ -137,41 +174,6 @@ public function lz4Compressed(): bool
$this->client->getOption(Redis::OPT_COMPRESSION) === Redis::COMPRESSION_LZ4;
}

/**
* Run a callback without serialization or compression enabled. Useful when
* performing operations like increment/decrement that should not be
* serialized or compressed on client side.
*/
public function runClean(callable $callback): mixed
{
/** @var \Redis|\RedisCluster $client */
$client = $this->client;

$oldSerializer = null;
if ($this->serialized()) {
$oldSerializer = $client->getOption($client::OPT_SERIALIZER);
$client->setOption($client::OPT_SERIALIZER, $client::SERIALIZER_NONE);
}

$oldCompressor = null;
if ($this->compressed()) {
$oldCompressor = $client->getOption($client::OPT_COMPRESSION);
$client->setOption($client::OPT_COMPRESSION, $client::COMPRESSION_NONE);
}

try {
return $callback();
} finally {
if ($oldSerializer !== null) {
$client->setOption($client::OPT_SERIALIZER, $oldSerializer);
}

if ($oldCompressor !== null) {
$client->setOption($client::OPT_COMPRESSION, $oldCompressor);
}
}
}

/**
* Determine if the current PhpRedis extension version supports packing.
*
Expand Down
0