Skip to content

Commit

Permalink
[8.x] Release concurrency lock for errors (#35546)
Browse files Browse the repository at this point in the history
* release concurrency lock for errors

* fix style errors

* fix style errors
  • Loading branch information
esbenp committed Dec 9, 2020
1 parent 50303ee commit 597d101
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
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

0 comments on commit 597d101

Please sign in to comment.