Skip to content

Commit 0897b05

Browse files
committed
Configurable autorelease
1 parent b6d3e00 commit 0897b05

File tree

4 files changed

+39
-16
lines changed

4 files changed

+39
-16
lines changed

src/Symfony/Component/Lock/Factory.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,15 @@ public function __construct(StoreInterface $store)
3636
/**
3737
* Creates a lock for the given resource.
3838
*
39-
* @param string $resource The resource to lock
40-
* @param float $ttl Maximum expected lock duration in seconds
39+
* @param string $resource The resource to lock
40+
* @param float $ttl Maximum expected lock duration in seconds
41+
* @param bool $autoRelease Whether to automatically release the lock or not
4142
*
4243
* @return Lock
4344
*/
44-
public function createLock($resource, $ttl = 300.0)
45+
public function createLock($resource, $ttl = 300.0, $autoRelease = true)
4546
{
46-
$lock = new Lock(new Key($resource), $this->store, $ttl);
47+
$lock = new Lock(new Key($resource), $this->store, $ttl, $autoRelease);
4748
$lock->setLogger($this->logger);
4849

4950
return $lock;

src/Symfony/Component/Lock/Lock.php

+14-11
Original file line numberDiff line numberDiff line change
@@ -32,33 +32,36 @@ final class Lock implements LockInterface, LoggerAwareInterface
3232
private $store;
3333
private $key;
3434
private $ttl;
35+
private $autoRelease;
3536
private $dirty = false;
3637

3738
/**
38-
* @param Key $key Resource to lock
39-
* @param StoreInterface $store Store used to handle lock persistence
40-
* @param float|null $ttl Maximum expected lock duration in seconds
39+
* @param Key $key Resource to lock
40+
* @param StoreInterface $store Store used to handle lock persistence
41+
* @param float|null $ttl Maximum expected lock duration in seconds
42+
* @param bool $autoRelease Whether to automatically release the lock or not
4143
*/
42-
public function __construct(Key $key, StoreInterface $store, $ttl = null)
44+
public function __construct(Key $key, StoreInterface $store, $ttl = null, $autoRelease = true)
4345
{
4446
$this->store = $store;
4547
$this->key = $key;
4648
$this->ttl = $ttl;
49+
$this->autoRelease = (bool) $autoRelease;
4750

4851
$this->logger = new NullLogger();
4952
}
5053

5154
/**
52-
* Automatically release the underlying lock when the object is destructed.
55+
* Automatically releases the underlying lock when the object is destructed.
5356
*/
5457
public function __destruct()
5558
{
56-
try {
57-
if ($this->dirty && $this->isAcquired()) {
58-
$this->release();
59-
}
60-
} catch (\Throwable $e) {
61-
} catch (\Exception $e) {
59+
if (!$this->autoRelease) {
60+
return;
61+
}
62+
63+
if ($this->dirty && $this->isAcquired()) {
64+
$this->release();
6265
}
6366
}
6467

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public static function isSupported($blocking = null)
3939
return false;
4040
}
4141

42-
if ($blocking === false && \PHP_VERSION_ID < 50601) {
42+
if (false === $blocking && \PHP_VERSION_ID < 50601) {
4343
return false;
4444
}
4545

src/Symfony/Component/Lock/Tests/LockTest.php

+19
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,25 @@ public function testReleaseOnDestruction()
150150
unset($lock);
151151
}
152152

153+
public function testNoAutoReleaseWhenNotConfigured()
154+
{
155+
$key = new Key(uniqid(__METHOD__, true));
156+
$store = $this->getMockBuilder(StoreInterface::class)->getMock();
157+
$lock = new Lock($key, $store, 10, false);
158+
159+
$store
160+
->method('exists')
161+
->willReturnOnConsecutiveCalls(array(true, false))
162+
;
163+
$store
164+
->expects($this->never())
165+
->method('delete')
166+
;
167+
168+
$lock->acquire(false);
169+
unset($lock);
170+
}
171+
153172
/**
154173
* @expectedException \Symfony\Component\Lock\Exception\LockReleasingException
155174
*/

0 commit comments

Comments
 (0)