8000 Add a private variable to store lock state · symfony/symfony@5144cc1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5144cc1

committed
Add a private variable to store lock state
1 parent 0e5932a commit 5144cc1

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

src/Symfony/Component/Lock/Lock.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ final class Lock implements LockInterface, LoggerAwareInterface
3131
private $store;
3232
private $key;
3333
private $ttl;
34+
private $dirty = false;
3435

3536
/**
3637
* @param Key $key Resource to lock
@@ -52,7 +53,7 @@ public function __construct(Key $key, StoreInterface $store, $ttl = null)
5253
public function __destruct()
5354
{
5455
try {
55-
if ($this->isAcquired()) {
56+
if ($this->dirty && $this->isAcquired()) {
5657
$this->release();
5758
}
5859
} catch (\Throwable $e) {
@@ -72,6 +73,7 @@ public function acquire($blocking = false)
7273
$this->store->waitAndSave($this->key);
7374
}
7475

76+
$this->dirty = true;
7577
$this->logger->info('Lock successfully acquired for "{resource}".', array('resource' => $this->key));
7678

7779
if ($this->ttl) {
@@ -80,6 +82,7 @@ public function acquire($blocking = false)
8082

8183
return true;
8284
} catch (LockConflictedException $e) {
85+
$this->dirty = false;
8386
$this->logger->warning('Failed to lock the "{resource}". Someone else already acquired the lock.', array('resource' => $this->key));
8487

8588
if ($blocking) {
@@ -104,8 +107,10 @@ public function refresh()
104107

105108
try {
106109
$this->store->putOffExpiration($this->key, $this->ttl);
110+
$this->dirty = true;
107111
$this->logger->info('Expiration defined for "{resource}" lock for "{ttl}" seconds.', array('resource' => $this->key, 'ttl' => $this->ttl));
108112
} catch (LockConflictedException $e) {
113+
$this->dirty = false;
109114
$this->logger->warning('Failed to define an expiration for the "{resource}" lock, someone else acquired the lock.', array('resource' => $this->key));
110115
throw $e;
111116
} catch (\Exception $e) {
@@ -119,7 +124,7 @@ public function refresh()
119124
*/
120125
public function isAcquired()
121126
{
122-
return $this->store->exists($this->key);
127+
return $this->dirty = $this->store->exists($this->key);
123128
}
124129

125130
/**
@@ -128,6 +133,7 @@ public function isAcquired()
128133
public function release()
129134
{
130135
$this->store->delete($this->key);
136+
$this->dirty = false;
131137

132138
if ($this->store->exists($this->key)) {
133139
$this->logger->warning('Failed to release the "{resource}" lock.', array('resource' => $this->key));

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

Lines changed: 6 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->atLeast(1))
106+
->expects($this->any())
107107
->method('exists')
108108
->with($key)
109109
->willReturn(true);
@@ -123,7 +123,7 @@ public function testRelease()
123123
->with($key);
124124

125125
$store
126-
->expects($this->atLeast(1))
126+
->expects($this->once())
127127
->method('exists')
128128
->with($key)
129129
->willReturn(false);
@@ -145,6 +145,8 @@ public function testReleaseOnDestruction()
145145
->expects($this->once())
146146
->method('delete')
147147
;
148+
149+
$lock->acquire(false);
148150
unset($lock);
149151
}
150152

@@ -158,12 +160,12 @@ public function testReleaseThrowsExceptionIfNotWellDeleted()
158160
$lock = new Lock($key, $store, 10);
159161

160162
$store
161-
->expects($this->atLeast(1))
163+
->expects($this->once())
162164
->method('delete')
163165
->with($key);
164166

165167
$store
166-
->expects($this->atLeast(1))
168+
->expects($this->once())
167169
->method('exists')
168170
->with($key)
169171
->willReturn(true);

0 commit comments

Comments
 (0)
0