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] Release concurrency lock for errors #35546

Merged
merged 3 commits into from Dec 9, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Illuminate/Redis/Limiters/ConcurrencyLimiter.php
Expand Up @@ -2,9 +2,9 @@

namespace Illuminate\Redis\Limiters;

use Exception;
use Illuminate\Contracts\Redis\LimiterTimeoutException;
use Illuminate\Support\Str;
use Throwable;

class ConcurrencyLimiter
{
Expand Down Expand Up @@ -61,7 +61,7 @@ public function __construct($redis, $name, $maxLocks, $releaseAfter)
* @return bool
*
* @throws \Illuminate\Contracts\Redis\LimiterTimeoutException
* @throws \Exception
* @throws \Throwable
*/
public function block($timeout, $callback = null)
{
Expand All @@ -82,7 +82,7 @@ public function block($timeout, $callback = null)
return tap($callback(), function () use ($slot, $id) {
$this->release($slot, $id);
});
} catch (Exception $exception) {
} catch (Throwable $exception) {
$this->release($slot, $id);

throw $exception;
Expand Down
22 changes: 22 additions & 0 deletions tests/Redis/ConcurrentLimiterTest.php
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Tests\Redis;

use Error;
use Illuminate\Contracts\Redis\LimiterTimeoutException;
use Illuminate\Foundation\Testing\Concerns\InteractsWithRedis;
use Illuminate\Redis\Limiters\ConcurrencyLimiter;
Expand Down Expand Up @@ -140,6 +141,27 @@ public function testItFailsAfterRetryTimeout()
$this->assertEquals([1], $store);
}

public function testItReleasesIfErrorIsThrown()
{
$store = [];

$lock = new ConcurrencyLimiter($this->redis(), 'key', 1, 5);

try {
$lock->block(1, function () {
throw new Error();
});
} catch (Error $e) {
}

$lock = new ConcurrencyLimiter($this->redis(), 'key', 1, 5);
$lock->block(1, function () use (&$store) {
$store[] = 1;
});

$this->assertEquals([1], $store);
}

private function redis()
{
return $this->redis['phpredis']->connection();
Expand Down