Skip to content

Commit b7267f2

Browse files
authored
[12.x] RateLimiter remaining to 0 to prevent negative values. (#57851)
* RateLimiter remaining to 0 to prevent negative values * Remove unused Repository import from CacheRateLimiterTest
1 parent 9474230 commit b7267f2

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/Illuminate/Cache/RateLimiter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ public function remaining($key, $maxAttempts)
232232

233233
$attempts = $this->attempts($key);
234234

235-
return $maxAttempts - $attempts;
235+
return max(0, $maxAttempts - $attempts);
236236
}
237237

238238
/**

tests/Cache/CacheRateLimiterTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,18 @@ public function testHitHasNoMemoryLeak()
7676
$rateLimiter->hit('key', 1);
7777
}
7878

79+
public function testRemainingIsNotNegative(): void
80+
{
81+
$cache = m::mock(Cache::class);
82+
$cache->shouldReceive('get')->with('key', 0)->andReturn(5);
83+
$cache->shouldReceive('getStore')->andReturn(new ArrayStore);
84+
85+
$rateLimiter = new RateLimiter($cache);
86+
87+
$this->assertSame(0, $rateLimiter->remaining('key', 3));
88+
$this->assertSame(0, $rateLimiter->retriesLeft('key', 3));
89+
}
90+
7991
public function testRetriesLeftReturnsCorrectCount()
8092
{
8193
$cache = m::mock(Cache::class);

0 commit comments

Comments
 (0)