Skip to content

Commit e45d424

Browse files
khepintimacdonaldtaylorotwell
authored
[10.x] Custom RateLimiter increase (#50197)
* Allow ratelimiter custom increments * add test * formatting * formatting * Revert formatting * Formatting * formatting --------- Co-authored-by: Tim MacDonald <hello@timacdonald.me> Co-authored-by: Taylor Otwell <taylor@laravel.com>
1 parent 7af1994 commit e45d424

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

src/Illuminate/Cache/RateLimiter.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,26 @@ public function tooManyAttempts($key, $maxAttempts)
105105
}
106106

107107
/**
108-
* Increment the counter for a given key for a given decay time.
108+
* Increment (by 1) the counter for a given key for a given decay time.
109109
*
110110
* @param string $key
111111
* @param int $decaySeconds
112112
* @return int
113113
*/
114114
public function hit($key, $decaySeconds = 60)
115+
{
116+
return $this->increment($key, $decaySeconds);
117+
}
118+
119+
/**
120+
* Increment the counter for a given key for a given decay time by a given amount.
121+
*
122+
* @param string $key
123+
* @param int $decaySeconds
124+
* @param int $amount
125+
* @return int
126+
*/
127+
public function increment($key, $decaySeconds = 60, $amount = 1)
115128
{
116129
$key = $this->cleanRateLimiterKey($key);
117130

@@ -121,7 +134,7 @@ public function hit($key, $decaySeconds = 60)
121134

122135
$added = $this->cache->add($key, 0, $decaySeconds);
123136

124-
$hits = (int) $this->cache->increment($key);
137+
$hits = (int) $this->cache->increment($key, $amount);
125138

126139
if (! $added && $hits == 1) {
127140
$this->cache->put($key, 1, $decaySeconds);

tests/Cache/CacheRateLimiterTest.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,29 @@ public function testHitProperlyIncrementsAttemptCount()
3030
$cache = m::mock(Cache::class);
3131
$cache->shouldReceive('add')->once()->with('key:timer', m::type('int'), 1)->andReturn(true);
3232
$cache->shouldReceive('add')->once()->with('key', 0, 1)->andReturn(true);
33-
$cache->shouldReceive('increment')->once()->with('key')->andReturn(1);
33+
$cache->shouldReceive('increment')->once()->with('key', 1)->andReturn(1);
3434
$rateLimiter = new RateLimiter($cache);
3535

3636
$rateLimiter->hit('key', 1);
3737
}
3838

39+
public function testIncrementProperlyIncrementsAttemptCount()
40+
{
41+
$cache = m::mock(Cache::class);
42+
$cache->shouldReceive('add')->once()->with('key:timer', m::type('int'), 1)->andReturn(true);
43+
$cache->shouldReceive('add')->once()->with('key', 0, 1)->andReturn(true);
44+
$cache->shouldReceive('increment')->once()->with('key', 5)->andReturn(5);
45+
$rateLimiter = new RateLimiter($cache);
46+
47+
$rateLimiter->increment('key', 1, 5);
48+
}
49+
3950
public function testHitHasNoMemoryLeak()
4051
{
4152
$cache = m::mock(Cache::class);
4253
$cache->shouldReceive('add')->once()->with('key:timer', m::type('int'), 1)->andReturn(true);
4354
$cache->shouldReceive('add')->once()->with('key', 0, 1)->andReturn(false);
44-
$cache->shouldReceive('increment')->once()->with('key')->andReturn(1);
55+
$cache->shouldReceive('increment')->once()->with('key', 1)->andReturn(1);
4556
$cache->shouldReceive('put')->once()->with('key', 1, 1);
4657
$rateLimiter = new RateLimiter($cache);
4758

@@ -83,7 +94,7 @@ public function testAttemptsCallbackReturnsTrue()
8394
$cache->shouldReceive('get')->once()->with('key', 0)->andReturn(0);
8495
$cache->shouldReceive('add')->once()->with('key:timer', m::type('int'), 1);
8596
$cache->shouldReceive('add')->once()->with('key', 0, 1)->andReturns(1);
86-
$cache->shouldReceive('increment')->once()->with('key')->andReturn(1);
97+
$cache->shouldReceive('increment')->once()->with('key', 1)->andReturn(1);
8798

8899
$executed = false;
89100

@@ -101,7 +112,7 @@ public function testAttemptsCallbackReturnsCallbackReturn()
101112
$cache->shouldReceive('get')->times(6)->with('key', 0)->andReturn(0);
102113
$cache->shouldReceive('add')->times(6)->with('key:timer', m::type('int'), 1);
103114
$cache->shouldReceive('add')->times(6)->with('key', 0, 1)->andReturns(1);
104-
$cache->shouldReceive('increment')->times(6)->with('key')->andReturn(1);
115+
$cache->shouldReceive('increment')->times(6)->with('key', 1)->andReturn(1);
105116

106117
$rateLimiter = new RateLimiter($cache);
107118

0 commit comments

Comments
 (0)