Skip to content

Commit

Permalink
Only modify the keys if there are duplicates
Browse files Browse the repository at this point in the history
  • Loading branch information
timacdonald committed Oct 15, 2024
1 parent 2da94d7 commit 4958a8d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
10 changes: 9 additions & 1 deletion src/Illuminate/Cache/RateLimiter.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,16 @@ public function limiter($name)
return $result;
}

$duplicates = collect($result)->duplicates('key');

if ($duplicates->isEmpty()) {
return $result;
}

foreach ($result as $limit) {
$limit->key = $limit->uniqueKey();
if ($duplicates->contains($limit->key)) {
$limit->key = $limit->fallbackKey();
}
}

return $result;
Expand Down
8 changes: 2 additions & 6 deletions src/Illuminate/Cache/RateLimiting/Limit.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,12 @@ public function response(callable $callback)
}

/**
* Retrieve a unique key for the limit.
* Retrieve a fallback key for the limit.
*
* @return string
*/
public function uniqueKey()
public function fallbackKey()
{
if ($this->key !== '') {
return $this->key;
}

return "attempts:{$this->maxAttempts}:decay:{$this->decaySeconds}";
}
}

0 comments on commit 4958a8d

Please sign in to comment.