@@ -31,6 +31,7 @@ final class Lock implements LockInterface, LoggerAwareInterface
31
31
private $ store ;
32
32
private $ key ;
33
33
private $ ttl ;
34
+ private $ dirty = false ;
34
35
35
36
/**
36
37
* @param Key $key Resource to lock
@@ -52,7 +53,7 @@ public function __construct(Key $key, StoreInterface $store, $ttl = null)
52
53
public function __destruct ()
53
54
{
54
55
try {
55
- if ($ this ->isAcquired ()) {
56
+ if ($ this ->dirty && $ this -> isAcquired ()) {
56
57
$ this ->release ();
57
58
}
58
59
} catch (\Throwable $ e ) {
@@ -72,6 +73,7 @@ public function acquire($blocking = false)
72
73
$ this ->store ->waitAndSave ($ this ->key );
73
74
}
74
75
76
+ $ this ->dirty = true ;
75
77
$ this ->logger ->info ('Lock successfully acquired for "{resource}". ' , array ('resource ' => $ this ->key ));
76
78
77
79
if ($ this ->ttl ) {
@@ -80,6 +82,7 @@ public function acquire($blocking = false)
80
82
81
83
return true ;
82
84
} catch (LockConflictedException $ e ) {
85
+ $ this ->dirty = false ;
83
86
$ this ->logger ->warning ('Failed to lock the "{resource}". Someone else already acquired the lock. ' , array ('resource ' => $ this ->key ));
84
87
85
88
if ($ blocking ) {
@@ -104,8 +107,10 @@ public function refresh()
104
107
105
108
try {
106
109
$ this ->store ->putOffExpiration ($ this ->key , $ this ->ttl );
110
+ $ this ->dirty = true ;
107
111
$ this ->logger ->info ('Expiration defined for "{resource}" lock for "{ttl}" seconds. ' , array ('resource ' => $ this ->key , 'ttl ' => $ this ->ttl ));
108
112
} catch (LockConflictedException $ e ) {
113
+ $ this ->dirty = false ;
109
114
$ this ->logger ->warning ('Failed to define an expiration for the "{resource}" lock, someone else acquired the lock. ' , array ('resource ' => $ this ->key ));
110
115
throw $ e ;
111
116
} catch (\Exception $ e ) {
@@ -119,7 +124,7 @@ public function refresh()
119
124
*/
120
125
public function isAcquired ()
121
126
{
122
- return $ this ->store ->exists ($ this ->key );
127
+ return $ this ->dirty = $ this -> store ->exists ($ this ->key );
123
128
}
124
129
125
130
/**
@@ -128,6 +133,7 @@ public function isAcquired()
128
133
public function release ()
129
134
{
130
135
$ this ->store ->delete ($ this ->key );
136
+ $ this ->dirty = false ;
131
137
132
138
if ($ this ->store ->exists ($ this ->key )) {
133
139
$ this ->logger ->warning ('Failed to release the "{resource}" lock. ' , array ('resource ' => $ this ->key ));
0 commit comments