Skip to content

Commit a5b504b

Browse files
Create new NotExpirableStoreException for stores that don't support expiration of locks.
1 parent 5a0cad2 commit a5b504b

File tree

12 files changed

+80
-5
lines changed

12 files changed

+80
-5
lines changed

src/Symfony/Component/Console/Command/LockableTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ private function lock($name = null, $blocking = false)
4848
$store = new FlockStore();
4949
}
5050

51-
$this->lock = (new Factory($store))->createLock($name ?: $this->getName());
51+
$this->lock = (new Factory($store))->createLock($name ?: $this->getName(), null);
5252
if (!$this->lock->acquire($blocking)) {
5353
$this->lock = null;
5454

src/Symfony/Component/Console/Tests/Command/LockableTraitTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function testLockReturnsFalseIfAlreadyLockedByAnotherCommand()
4747
$store = new FlockStore();
4848
}
4949

50-
$lock = (new Factory($store))->createLock($command->getName());
50+
$lock = (new Factory($store))->createLock($command->getName(), null);
5151
$lock->acquire();
5252

5353
$tester = new CommandTester($command);
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Lock\Exception;
13+
14+
/**
15+
* NotExpirableStoreException is thrown when a store doesn't support expiration of locks.
16+
*
17+
* @author Ganesh Chandrasekaran <gchandrasekaran@wayfair.com>
18+
*/
19+
class NotExpirableStoreException extends \LogicException implements ExceptionInterface
20+
{
21+
}

src/Symfony/Component/Lock/Store/CombinedStore.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\Lock\Exception\InvalidArgumentException;
1818
use Symfony\Component\Lock\Exception\LockConflictedException;
1919
use Symfony\Component\Lock\Exception\LockExpiredException;
20+
use Symfony\Component\Lock\Exception\NotExpirableStoreException;
2021
use Symfony\Component\Lock\Exception\NotSupportedException;
2122
use Symfony\Component\Lock\Key;
2223
use Symfony\Component\Lock\StoreInterface;
@@ -102,6 +103,7 @@ public function putOffExpiration(Key $key, $ttl)
102103
{
103104
$successCount = 0;
104105
$failureCount = 0;
106+
$notExpirableStoresCount = 0;
105107
$storesCount = \count($this->stores);
106108
$expireAt = microtime(true) + $ttl;
107109

@@ -115,6 +117,9 @@ public function putOffExpiration(Key $key, $ttl)
115117

116118
$store->putOffExpiration($key, $adjustedTtl);
117119
++$successCount;
120+
} catch (NotExpirableStoreException $e) {
121+
$this->logger->notice('This store does not support expiration of locks.', $store);
122+
++$notExpirableStoresCount;
118123
} catch (\Exception $e) {
119124
$this->logger->warning('One store failed to put off the expiration of the "{resource}" lock.', array('resource' => $key, 'store' => $store, 'exception' => $e));
120125
++$failureCount;
@@ -125,6 +130,10 @@ public function putOffExpiration(Key $key, $ttl)
125130
}
126131
}
127132

133+
if ($notExpirableStoresCount === $storesCount) {
134+
throw new NotExpirableStoreException();
135+
}
136+
128137
if ($key->isExpired()) {
129138
throw new LockExpiredException(sprintf('Failed to put off the expiration of the "%s" lock within the specified time.', $key));
130139
}

src/Symfony/Component/Lock/Store/FlockStore.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Lock\Exception\InvalidArgumentException;
1515
use Symfony\Component\Lock\Exception\LockConflictedException;
1616
use Symfony\Component\Lock\Exception\LockStorageException;
17+
use Symfony\Component\Lock\Exception\NotExpirableStoreException;
1718
use Symfony\Component\Lock\Key;
1819
use Symfony\Component\Lock\StoreInterface;
1920

@@ -108,7 +109,7 @@ private function lock(Key $key, $blocking)
108109
*/
109110
public function putOffExpiration(Key $key, $ttl)
110111
{
111-
// do nothing, the flock locks forever.
112+
throw new NotExpirableStoreException();
112113
}
113114

114115
/**

src/Symfony/Component/Lock/Store/SemaphoreStore.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Lock\Exception\InvalidArgumentException;
1515
use Symfony\Component\Lock\Exception\LockConflictedException;
16+
use Symfony\Component\Lock\Exception\NotExpirableStoreException;
1617
use Symfony\Component\Lock\Key;
1718
use Symfony\Component\Lock\StoreInterface;
1819

@@ -102,7 +103,7 @@ public function delete(Key $key)
102103
*/
103104
public function putOffExpiration(Key $key, $ttl)
104105
{
105-
// do nothing, the semaphore locks forever.
106+
throw new NotExpirableStoreException();
106107
}
107108

108109
/**

src/Symfony/Component/Lock/Store/ZookeeperStore.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Lock\Exception\LockAcquiringException;
1515
use Symfony\Component\Lock\Exception\LockConflictedException;
1616
use Symfony\Component\Lock\Exception\LockReleasingException;
17+
use Symfony\Component\Lock\Exception\NotExpirableStoreException;
1718
use Symfony\Component\Lock\Exception\NotSupportedException;
1819
use Symfony\Component\Lock\Key;
1920
use Symfony\Component\Lock\StoreInterface;
@@ -91,7 +92,7 @@ public function waitAndSave(Key $key)
9192
*/
9293
public function putOffExpiration(Key $key, $ttl)
9394
{
94-
throw new NotSupportedException();
95+
throw new NotExpirableStoreException();
9596
}
9697

9798
/**

src/Symfony/Component/Lock/StoreInterface.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public function waitAndSave(Key $key);
4949
* @param float $ttl amount of second to keep the lock in the store
5050
*
5151
* @throws LockConflictedException
52+
* @throws NotExpirableStoreException
5253
* @throws NotSupportedException
5354
*/
5455
public function putOffExpiration(Key $key, $ttl);

src/Symfony/Component/Lock/Tests/Store/FlockStoreTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
class FlockStoreTest extends AbstractStoreTest
2121
{
2222
use BlockingStoreTestTrait;
23+
use NotExpiringStoreTestTrait;
2324

2425
/**
2526
* {@inheritdoc}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Lock\Tests\Store;
13+
14+
use Symfony\Component\Lock\Key;
15+
16+
/**
17+
* @author Ganesh Chandrasekaran <gchandrasekaran@wayfair.com>
18+
*/
19+
trait NotExpiringStoreTestTrait
20+
{
21+
/**
22+
* @see AbstractStoreTest::getStore()
23+
*/
24+
abstract protected function getStore();
25+
26+
/**
27+
* @expectedException \Symfony\Component\Lock\Exception\NotExpirableStoreException
28+
*/
29+
public function testPutOffExpirationThrowsException()
30+
{
31+
$store = $this->getStore();
32+
$key = new Key(uniqid(__METHOD__, true));
33+
34+
$store->save($key);
35+
$store->putOffExpiration($key, 10.0);
36+
}
37+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy