5
5
The Lock Component
6
6
====================
7
7
8
- The Lock Component provides a mechanism to garentee an exclusive access into
9
- a critical section. The component ships with ready to use stores for the
10
- most common backends.
8
+ The Lock Component provides a mechanism to guarantee an exclusive access
9
+ into a critical section. The component ships with ready to use stores for
10
+ the most common backends.
11
11
12
12
.. versionadded :: 3.3
13
13
The Lock component was introduced in Symfony 3.3.
@@ -22,17 +22,16 @@ You can install the component in 2 different ways:
22
22
23
23
.. include :: /components/require_autoload.rst.inc
24
24
25
-
26
25
Usage
27
26
-----
28
27
29
28
In order to centralize state of locks, you first need to create a ``Store ``.
30
29
Then, you can ask to this store to create a Lock for your ``resource ``.
31
30
32
31
The :method: `Symfony\\ Component\\ Lock\\ LockInterface::acquire ` method tries to
33
- acquire the lock. If the lock is can not be acquired, the method throws a
32
+ acquire the lock. If the lock can not be acquired, the method throws a
34
33
:class: `Symfony\\ Component\\ Lock\\ Exception\\ LockConflictedException `. You can
35
- safly call the ``acquire() `` method several time , even if you already acquired
34
+ safely call the ``acquire() `` method several times , even if you already acquired
36
35
it.
37
36
38
37
.. code-block :: php
41
40
use Symfony\Component\Lock\Exception\LockConflictedException;
42
41
43
42
$store = new SemaphoreStore();
44
- $lock = $store->createLock('hello ');
43
+ $lock = $store->createLock('invoice-pdf-generation ');
45
44
46
45
try {
47
46
$lock->acquire();
48
- // the resource "hello " is locked. You can perform your task safely .
47
+ // the resource "invoice-pdf-generation " is locked.
49
48
50
- // do whatever you want .
49
+ // You can compute and generate invoice safely here .
51
50
52
51
$lock->release();
53
52
} catch (LockConflictedException $e) {
54
- // the resource "hello " is already locked by another process
53
+ // the resource "invoice-pdf-generation " is already locked by another process
55
54
}
56
55
57
- The first argument of `createLock ` is a string representation of the
56
+ The first argument of `` createLock ` ` is a string representation of the
58
57
``resource `` to lock.
59
58
60
59
.. note ::
61
60
62
- In opposition to some other implementations, the Lock Component distinguish
63
- locks instances, even when they are created from the same ``resource ``.
61
+ In opposition to some other implementations, the Lock Component
62
+ distinguishes locks instances, even when they are created from the same
63
+ ``resource ``.
64
64
If you want to share a lock in several services. You have to share the
65
65
instance of Lock returned by the ``Store::createLock `` method.
66
66
67
-
68
67
Blocking locks
69
68
--------------
70
69
@@ -73,7 +72,7 @@ You can pass an optional blocking argument as the first argument to the
73
72
defaults to ``false ``. If this is set to ``true ``, your PHP code will wait
74
73
infinitely until the lock is released by another process.
75
74
76
- Some ``Store `` (but not all) natively supports this features . When they don't,
75
+ Some ``Store `` (but not all) natively supports this feature . When they don't,
77
76
you can decorate it with the ``RetryTillSaveStore ``.
78
77
79
78
.. code-block :: php
@@ -84,20 +83,17 @@ you can decorate it with the ``RetryTillSaveStore``.
84
83
$store = new RedisStore(new \Predis\Client('tcp://localhost:6379'));
85
84
$store = new RetryTillSaveStore($store);
86
85
87
- $lock = $store->createLock('hello ');
86
+ $lock = $store->createLock('notification-flush ');
88
87
89
88
$lock->acquire(true);
90
89
91
-
92
-
93
90
Expirable Locks
94
91
---------------
95
92
96
- Working with a remote ``Store `` is hard. In oposition to local ``Stores ``
97
- (like :ref: `FlockStore <lock-store-flock >` or :ref: `SemaphoreStore <lock-store-semaphore >`) there is now way for the remote
98
- ``Store `` to know whether or not the locker process is till alive. Due tu bugs,
99
- fatal errors or segmentation fault, we can't garentee that the ``release() ``
100
- function will be called, which would cause a ``resource `` to be locked
93
+ Working with a remote ``Store `` is hard. There is now way for the remote
94
+ ``Store `` to know if the locker process is till alive.
95
+ Due to bugs, fatal errors or segmentation fault, we can't guarantee that the
96
+ ``release() `` method will be called, which would cause a ``resource `` to be locked
101
97
infinitely.
102
98
103
99
To fill this gap, the remote ``Stores `` provide an expirable mechanism: The lock
@@ -106,9 +102,9 @@ When the timeout occured, the lock is automatically released even if the locker
106
102
don't call the ``release() `` method.
107
103
108
104
That's why, when you create a lock on an expirable ``Store ``. You have to choose
109
- carrefully the correct TTL. When too low, you take the risk to "loose " the lock
110
- (and someone else acquire it) wheras you don't finish your task.
111
- When too hight and the process crash before you call the ``release() `` method,
105
+ carefully the correct TTL. When too low, you take the risk to "lose " the lock
106
+ (and someone else acquire it) whereas you don't finish your task.
107
+ When too high and the process crash before you call the ``release() `` method,
112
108
the ``resource `` will stay lock till the timeout.
113
109
114
110
@@ -118,7 +114,7 @@ the ``resource`` will stay lock till the timeout.
118
114
119
115
$store = new RedisStore(new \Predis\Client('tcp://localhost:6379'));
120
116
121
- $lock = $store->createLock('hello ', 30);
117
+ $lock = $store->createLock('charts-generation ', 30);
122
118
123
119
$lock->acquire();
124
120
try {
@@ -129,12 +125,11 @@ the ``resource`` will stay lock till the timeout.
129
125
130
126
.. tip ::
131
127
132
- To avoid to let the Lock in a locking state, try to always release an
128
+ To avoid letting the Lock in a locking state, try to always release an
133
129
expirable lock by wraping the job in a try/catch block for instance.
134
130
135
-
136
131
When you have to work on a really long task, you should not set the TTL to
137
- overlaps the duration of this task. Instead, the Lock Component expose a
132
+ overlap the duration of this task. Instead, the Lock Component expose a
138
133
:method: `Symfony\\ Component\\ Lock\\ LockInterface::refresh ` method in order to
139
134
put off the TTL of the Lock. Thereby you can choose a small initial TTL, and
140
135
regulary refresh the lock
@@ -145,7 +140,7 @@ regulary refresh the lock
145
140
146
141
$store = new RedisStore(new \Predis\Client('tcp://localhost:6379'));
147
142
148
- $lock = $store->createLock('hello ', 30);
143
+ $lock = $store->createLock('charts-generation ', 30);
149
144
150
145
$lock->acquire();
151
146
try {
@@ -159,14 +154,13 @@ regulary refresh the lock
159
154
$lock->release()
160
155
}
161
156
162
-
163
157
Available Stores
164
158
----------------
165
159
166
160
``Stores `` are classes that implement :class: `Symfony\\ Component\\ Lock\\ StoreInterface `.
167
161
This component provides several adapters ready to use in your applications.
168
162
169
- Here is a small summary of availanble ``Stores `` and theire capabilities.
163
+ Here is a small summary of available ``Stores `` and their capabilities.
170
164
171
165
+----------------------------------------------+--------+----------+-----------+
172
166
| Store | Scope | Blocking | Expirable |
@@ -289,7 +283,7 @@ result, uses a quorum to decide whether or not the lock is acquired.
289
283
290
284
This ``Store `` is usefull for High availability application. You can provide
291
285
several Redis Server, and use theses server to manage the Lock. A
292
- MajorityQuorum is enougth to safly acquire a lock while it allow some Redis
286
+ MajorityQuorum is enougth to safely acquire a lock while it allow some Redis
293
287
server failure.
294
288
295
289
.. code-block :: php
0 commit comments