Skip to content
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

[8.x] Adds attempt method to Rate Limiter #38313

Merged
merged 4 commits into from
Aug 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
20 changes: 20 additions & 0 deletions src/Illuminate/Cache/RateLimiter.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,26 @@ public function limiter(string $name)
return $this->limiters[$name] ?? null;
}

/**
* Attempts to execute a callback if it's not limited.
*
* @param string $key
* @param int $maxAttempts
* @param \Closure $callback
* @param int $decaySeconds
* @return mixed
*/
public function attempt($key, $maxAttempts, Closure $callback, $decaySeconds = 60)
{
if ($this->tooManyAttempts($key, $maxAttempts)) {
return false;
}

return tap($callback() ?: true, function () use ($key, $decaySeconds) {
$this->hit($key, $decaySeconds);
});
}

/**
* Determine if the given key has been "accessed" too many times.
*
Expand Down
1 change: 1 addition & 0 deletions src/Illuminate/Support/Facades/RateLimiter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* @method static int retriesLeft($key, $maxAttempts)
* @method static void clear($key)
* @method static int availableIn($key)
* @method static bool attempt($key, $maxAttempts, \Closure $callback, $decaySeconds = 60)
*
* @see \Illuminate\Cache\RateLimiter
*/
Expand Down
49 changes: 49 additions & 0 deletions tests/Cache/CacheRateLimiterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,53 @@ public function testAvailableInReturnsPositiveValues()
$this->assertTrue($rateLimiter->availableIn('key:timer') >= 0);
$this->assertTrue($rateLimiter->availableIn('key:timer') >= 0);
}

public function testAttemptsCallbackReturnsTrue()
{
$cache = m::mock(Cache::class);
$cache->shouldReceive('get')->once()->with('key', 0)->andReturn(0);
$cache->shouldReceive('add')->once()->with('key:timer', m::type('int'), 1);
$cache->shouldReceive('add')->once()->with('key', 0, 1)->andReturns(1);
$cache->shouldReceive('increment')->once()->with('key')->andReturn(1);

$executed = false;

$rateLimiter = new RateLimiter($cache);

$this->assertTrue($rateLimiter->attempt('key', 1, function() use (&$executed) {
$executed = true;
}, 1));
$this->assertTrue($executed);
}

public function testAttemptsCallbackReturnsCallbackReturn()
{
$cache = m::mock(Cache::class);
$cache->shouldReceive('get')->once()->with('key', 0)->andReturn(0);
$cache->shouldReceive('add')->once()->with('key:timer', m::type('int'), 1);
$cache->shouldReceive('add')->once()->with('key', 0, 1)->andReturns(1);
$cache->shouldReceive('increment')->once()->with('key')->andReturn(1);

$rateLimiter = new RateLimiter($cache);

$this->assertEquals('foo', $rateLimiter->attempt('key', 1, function() {
return 'foo';
}, 1));
}

public function testAttemptsCallbackReturnsFalse()
{
$cache = m::mock(Cache::class);
$cache->shouldReceive('get')->once()->with('key', 0)->andReturn(2);
$cache->shouldReceive('has')->once()->with('key:timer')->andReturn(true);

$executed = false;

$rateLimiter = new RateLimiter($cache);

$this->assertFalse($rateLimiter->attempt('key', 1, function() use (&$executed) {
$executed = true;
}, 1));
$this->assertFalse($executed);
}
}