55The Lock Component
66====================
77
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.
1111
1212.. versionadded :: 3.3
1313 The Lock component was introduced in Symfony 3.3.
@@ -22,17 +22,16 @@ You can install the component in 2 different ways:
2222
2323.. include :: /components/require_autoload.rst.inc
2424
25-
2625Usage
2726-----
2827
2928In order to centralize state of locks, you first need to create a ``Store ``.
3029Then, you can ask to this store to create a Lock for your ``resource ``.
3130
3231The :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
3433: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
3635it.
3736
3837.. code-block :: php
4140 use Symfony\Component\Lock\Exception\LockConflictedException;
4241
4342 $store = new SemaphoreStore();
44- $lock = $store->createLock('hello ');
43+ $lock = $store->createLock('invoice-pdf-generation ');
4544
4645 try {
4746 $lock->acquire();
48- // the resource "hello " is locked. You can perform your task safely .
47+ // the resource "invoice-pdf-generation " is locked.
4948
50- // do whatever you want .
49+ // You can compute and generate invoice safely here .
5150
5251 $lock->release();
5352 } 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
5554 }
5655
57- The first argument of `createLock ` is a string representation of the
56+ The first argument of `` createLock ` ` is a string representation of the
5857``resource `` to lock.
5958
6059.. note ::
6160
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 ``.
6464 If you want to share a lock in several services. You have to share the
6565 instance of Lock returned by the ``Store::createLock `` method.
6666
67-
6867Blocking locks
6968--------------
7069
@@ -73,7 +72,7 @@ You can pass an optional blocking argument as the first argument to the
7372defaults to ``false ``. If this is set to ``true ``, your PHP code will wait
7473infinitely until the lock is released by another process.
7574
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,
7776you can decorate it with the ``RetryTillSaveStore ``.
7877
7978.. code-block :: php
@@ -84,20 +83,17 @@ you can decorate it with the ``RetryTillSaveStore``.
8483 $store = new RedisStore(new \Predis\Client('tcp://localhost:6379'));
8584 $store = new RetryTillSaveStore($store);
8685
87- $lock = $store->createLock('hello ');
86+ $lock = $store->createLock('notification-flush ');
8887
8988 $lock->acquire(true);
9089
91-
92-
9390 Expirable Locks
9491---------------
9592
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
10197infinitely.
10298
10399To 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
106102don't call the ``release() `` method.
107103
108104That'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,
112108the ``resource `` will stay lock till the timeout.
113109
114110
@@ -118,7 +114,7 @@ the ``resource`` will stay lock till the timeout.
118114
119115 $store = new RedisStore(new \Predis\Client('tcp://localhost:6379'));
120116
121- $lock = $store->createLock('hello ', 30);
117+ $lock = $store->createLock('charts-generation ', 30);
122118
123119 $lock->acquire();
124120 try {
@@ -129,12 +125,11 @@ the ``resource`` will stay lock till the timeout.
129125
130126 .. tip ::
131127
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
133129 expirable lock by wraping the job in a try/catch block for instance.
134130
135-
136131When 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
138133:method: `Symfony\\ Component\\ Lock\\ LockInterface::refresh ` method in order to
139134put off the TTL of the Lock. Thereby you can choose a small initial TTL, and
140135regulary refresh the lock
@@ -145,7 +140,7 @@ regulary refresh the lock
145140
146141 $store = new RedisStore(new \Predis\Client('tcp://localhost:6379'));
147142
148- $lock = $store->createLock('hello ', 30);
143+ $lock = $store->createLock('charts-generation ', 30);
149144
150145 $lock->acquire();
151146 try {
@@ -159,14 +154,13 @@ regulary refresh the lock
159154 $lock->release()
160155 }
161156
162-
163157 Available Stores
164158----------------
165159
166160``Stores `` are classes that implement :class: `Symfony\\ Component\\ Lock\\ StoreInterface `.
167161This component provides several adapters ready to use in your applications.
168162
169- Here is a small summary of availanble ``Stores `` and theire capabilities.
163+ Here is a small summary of available ``Stores `` and their capabilities.
170164
171165+----------------------------------------------+--------+----------+-----------+
172166| Store | Scope | Blocking | Expirable |
@@ -289,7 +283,7 @@ result, uses a quorum to decide whether or not the lock is acquired.
289283
290284 This ``Store `` is usefull for High availability application. You can provide
291285 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
293287 server failure.
294288
295289.. code-block :: php
0 commit comments