Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 309a6c1

Browse files
committed
Automaticaly release lock on destruction
1 parent db8d87d commit 309a6c1

File tree

4 files changed

+40
-9
lines changed

4 files changed

+40
-9
lines changed

src/Symfony/Component/Lock/Factory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function __construct(StoreInterface $store)
3737
* Creates a lock for the given resource.
3838
*
3939
* @param string $resource The resource to lock
40-
* @param float $ttl maximum expected lock duration
40+
* @param float $ttl Maximum expected lock duration in seconds
4141
*
4242
* @return Lock
4343
*/

src/Symfony/Component/Lock/Lock.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ final class Lock implements LockInterface, LoggerAwareInterface
3333
private $ttl;
3434

3535
/**
36-
* @param Key $key
37-
* @param StoreInterface $store
38-
* @param float|null $ttl
36+
* @param Key $key Resource to lock
37+
* @param StoreInterface $store Store used to handle lock persistence
38+
* @param float|null $ttl Maximum expected lock duration in seconds
3939
*/
4040
public function __construct(Key $key, StoreInterface $store, $ttl = null)
4141
{
@@ -46,6 +46,20 @@ public function __construct(Key $key, StoreInterface $store, $ttl = null)
4646
$this->logger = new NullLogger();
4747
}
4848

49+
/**
50+
* Automatically release the underlying lock when the object is destructed.
51+
*/
52+
public function __destruct()
53+
{
54+
try {
55+
if ($this->isAcquired()) {
56+
$this->release();
57+
}
58+
} catch (\Throwable $e) {
59+
} catch (\Exception $e) {
60+
}
61+
}
62+
4963
/**
5064
* {@inheritdoc}
5165
*/

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public function delete(Key $key)
9999
*/
100100
public function putOffExpiration(Key $key, $ttl)
101101
{
102-
// do nothing, the flock locks forever.
102+
// do nothing, the semaphore locks forever.
103103
}
104104

105105
/**

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

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public function testIsAquired()
103103
$lock = new Lock($key, $store, 10);
104104

105105
$store
106-
->expects($this->once())
106+
->expects($this->atLeast(1))
107107
->method('exists')
108108
->with($key)
109109
->willReturn(true);
@@ -123,14 +123,31 @@ public function testRelease()
123123
->with($key);
124124

125125
$store
126-
->expects($this->once())
126+
->expects($this->atLeast(1))
127127
->method('exists')
128128
->with($key)
129129
->willReturn(false);
130130

131131
$lock->release();
132132
}
133133

134+
public function testReleaseOnDestruction()
135+
{
136+
$key = new Key(uniqid(__METHOD__, true));
137+
$store = $this->getMockBuilder(StoreInterface::class)->getMock();
138+
$lock = new Lock($key, $store, 10);
139+
140+
$store
141+
->method('exists')
142+
->willReturnOnConsecutiveCalls(array(true, false))
143+
;
144+
$store
145+
->expects($this->once())
146+
->method('delete')
147+
;
148+
unset($lock);
149+
}
150+
134151
/**
135152
* @expectedException \Symfony\Component\Lock\Exception\LockReleasingException
136153
*/
@@ -141,12 +158,12 @@ public function testReleaseThrowsExceptionIfNotWellDeleted()
141158
$lock = new Lock($key, $store, 10);
142159

143160
$store
144-
->expects($this->once())
161+
->expects($this->atLeast(1))
145162
->method('delete')
146163
->with($key);
147164

148165
$store
149-
->expects($this->once())
166+
->expects($this->atLeast(1))
150167
->method('exists')
151168
->with($key)
152169
->willReturn(true);

0 commit comments

Comments
 (0)