From c06f236d8d53c8a9a1829ed1b002385faa2a1c57 Mon Sep 17 00:00:00 2001 From: Esben Petersen Date: Wed, 9 Dec 2020 10:18:51 +0100 Subject: [PATCH 1/3] release concurrency lock for errors --- .../Redis/Limiters/ConcurrencyLimiter.php | 6 ++--- tests/Redis/ConcurrentLimiterTest.php | 23 +++++++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Redis/Limiters/ConcurrencyLimiter.php b/src/Illuminate/Redis/Limiters/ConcurrencyLimiter.php index 2dafa6c9e5d4..a381f7d17ae4 100644 --- a/src/Illuminate/Redis/Limiters/ConcurrencyLimiter.php +++ b/src/Illuminate/Redis/Limiters/ConcurrencyLimiter.php @@ -2,7 +2,7 @@ namespace Illuminate\Redis\Limiters; -use Exception; +use Throwable; use Illuminate\Contracts\Redis\LimiterTimeoutException; use Illuminate\Support\Str; @@ -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) { @@ -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; diff --git a/tests/Redis/ConcurrentLimiterTest.php b/tests/Redis/ConcurrentLimiterTest.php index 78a6792274a5..6be86f625dbe 100644 --- a/tests/Redis/ConcurrentLimiterTest.php +++ b/tests/Redis/ConcurrentLimiterTest.php @@ -6,6 +6,7 @@ use Illuminate\Foundation\Testing\Concerns\InteractsWithRedis; use Illuminate\Redis\Limiters\ConcurrencyLimiter; use PHPUnit\Framework\TestCase; +use Error; use Throwable; /** @@ -140,6 +141,28 @@ 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(); From 1c75776bbd78add2521e738fe432523041f76361 Mon Sep 17 00:00:00 2001 From: Esben Petersen Date: Wed, 9 Dec 2020 10:28:30 +0100 Subject: [PATCH 2/3] fix style errors --- src/Illuminate/Redis/Limiters/ConcurrencyLimiter.php | 2 +- tests/Redis/ConcurrentLimiterTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Redis/Limiters/ConcurrencyLimiter.php b/src/Illuminate/Redis/Limiters/ConcurrencyLimiter.php index a381f7d17ae4..5fcc921e2c1f 100644 --- a/src/Illuminate/Redis/Limiters/ConcurrencyLimiter.php +++ b/src/Illuminate/Redis/Limiters/ConcurrencyLimiter.php @@ -2,9 +2,9 @@ namespace Illuminate\Redis\Limiters; -use Throwable; use Illuminate\Contracts\Redis\LimiterTimeoutException; use Illuminate\Support\Str; +use Throwable; class ConcurrencyLimiter { diff --git a/tests/Redis/ConcurrentLimiterTest.php b/tests/Redis/ConcurrentLimiterTest.php index 6be86f625dbe..30597176cbf1 100644 --- a/tests/Redis/ConcurrentLimiterTest.php +++ b/tests/Redis/ConcurrentLimiterTest.php @@ -2,11 +2,11 @@ namespace Illuminate\Tests\Redis; +use Error; use Illuminate\Contracts\Redis\LimiterTimeoutException; use Illuminate\Foundation\Testing\Concerns\InteractsWithRedis; use Illuminate\Redis\Limiters\ConcurrencyLimiter; use PHPUnit\Framework\TestCase; -use Error; use Throwable; /** From aaace72af7910c3ab699e9682407e235630c000c Mon Sep 17 00:00:00 2001 From: Esben Petersen Date: Wed, 9 Dec 2020 10:29:25 +0100 Subject: [PATCH 3/3] fix style errors --- tests/Redis/ConcurrentLimiterTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Redis/ConcurrentLimiterTest.php b/tests/Redis/ConcurrentLimiterTest.php index 30597176cbf1..35e5e64d2ea0 100644 --- a/tests/Redis/ConcurrentLimiterTest.php +++ b/tests/Redis/ConcurrentLimiterTest.php @@ -152,7 +152,6 @@ public function testItReleasesIfErrorIsThrown() throw new Error(); }); } catch (Error $e) { - } $lock = new ConcurrencyLimiter($this->redis(), 'key', 1, 5);