Skip to content

Commit 4367e08

Browse files
committed
Configurable autorelease
1 parent 9ac995c commit 4367e08

File tree

3 files changed

+36
-9
lines changed

3 files changed

+36
-9
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

+12-5
Original file line numberDiff line numberDiff line change
@@ -31,27 +31,34 @@ final class Lock implements LockInterface, LoggerAwareInterface
3131
private $store;
3232
private $key;
3333
private $ttl;
34+
private $autoRelease;
3435
private $dirty = false;
3536

3637
/**
37-
* @param Key $key Resource to lock
38-
* @param StoreInterface $store Store used to handle lock persistence
39-
* @param float|null $ttl Maximum expected lock duration in seconds
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
41+
* @param bool $autoRelease Whether to automatically release the lock or not
4042
*/
41-
public function __construct(Key $key, StoreInterface $store, $ttl = null)
43+
public function __construct(Key $key, StoreInterface $store, $ttl = null, $autoRelease = true)
4244
{
4345
$this->store = $store;
4446
$this->key = $key;
4547
$this->ttl = $ttl;
48+
$this->autoRelease = (bool) $autoRelease;
4649

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

5053
/**
51-
* Automatically release the underlying lock when the object is destructed.
54+
* Automatically releases the underlying lock when the object is destructed.
5255
*/
5356
public function __destruct()
5457
{
58+
if (!$this->autoRelease) {
59+
return;
60+
}
61+
5562
try {
5663
if ($this->dirty && $this->isAcquired()) {
5764
$this->release();

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)