-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Add docs for the Lock component #7364
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6469016
Add the Lock Component
jderusse 60bfe03
Fix typo
jderusse d1d3b71
Fixed minor typos and syntax issues
javiereguiluz 2498ccd
Fix typo
jderusse 6b6d865
Final review and some minor rewords/simplifications
javiereguiluz 17248e1
Remove codeblock
jderusse d6a218c
Rename quorum strategy
jderusse d4326c0
Reduce usage of "you"
jderusse 3035fe9
Rename quorum into strategy
jderusse File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Reduce usage of "you"
- Loading branch information
commit d4326c0e0175246b74f44e14a089c6bcba4693f3
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,8 +38,8 @@ create the lock for some resource:: | |
$store = new SemaphoreStore(); | ||
$factory = new Factory($store); | ||
|
||
Then, call to the :method:`Symfony\\Component\\Lock\\LockInterface::acquire` | ||
method to try to acquire the lock. Its first argument is an arbitrary string | ||
Then, a call to the :method:`Symfony\\Component\\Lock\\LockInterface::acquire` | ||
method will try to acquire the lock. Its first argument is an arbitrary string | ||
that represents the locked resource:: | ||
|
||
// ... | ||
|
@@ -52,27 +52,27 @@ that represents the locked resource:: | |
$lock->release(); | ||
} | ||
|
||
If the lock can not be acquired, the method returns ``false``. You can safely | ||
call the ``acquire()`` method repeatedly, even if you already acquired it. | ||
If the lock can not be acquired, the method returns ``false``. The ``acquire()`` | ||
method can be safely called repeatedly, even if the lock is already acquired. | ||
|
||
.. note:: | ||
|
||
Unlike other implementations, the Lock Component distinguishes locks | ||
instances even when they are created for the same resource. If you want to | ||
share a lock in several services, share the ``Lock`` instance returned by | ||
the ``Factory::createLock`` method. | ||
instances even when they are created for the same resource. If a lock has | ||
to be used by several services, they should share the same ``Lock`` instance | ||
returned by the ``Factory::createLock`` method. | ||
|
||
Blocking Locks | ||
-------------- | ||
|
||
By default, when a lock cannot be acquired, the ``acquire`` method returns | ||
``false`` immediately. In case you want to wait (indefinitely) until the lock | ||
can be created, pass ``false`` as the argument of the ``acquire()`` method. This | ||
``false`` immediately. To wait (indefinitely) until the lock | ||
can be created, pass ``true`` as the argument of the ``acquire()`` method. This | ||
is called a **blocking lock** because the execution of your application stops | ||
until the lock is acquired. | ||
|
||
Some of the built-in ``Store`` classes support this feature. When they don't, | ||
you can decorate them with the ``RetryTillSaveStore`` class:: | ||
they can be decorated with the ``RetryTillSaveStore`` class:: | ||
|
||
use Symfony\Component\Lock\Factory; | ||
use Symfony\Component\Lock\Store\RedisStore; | ||
|
@@ -90,7 +90,7 @@ Expiring Locks | |
|
||
Locks created remotely are difficult to manage because there is no way for the | ||
remote ``Store`` to know if the locker process is still alive. Due to bugs, | ||
fatal errors or segmentation faults, we can't guarantee that the ``release()`` | ||
fatal errors or segmentation faults, it cannot be guaranteed that ``release()`` | ||
method will be called, which would cause the resource to be locked infinitely. | ||
|
||
The best solution in those cases is to create **expiring locks**, which are | ||
|
@@ -100,12 +100,12 @@ the ``createLock()`` method. If needed, these locks can also be released early | |
with the ``release()`` method. | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. extra blank line |
||
The trickiest part when working with expiring locks is choosing the right TTL. | ||
If it's too short, other processes could acquire the lock before finishing your | ||
work; it it's too long and the process crashes before calling the ``release()`` | ||
If it's too short, other processes could acquire the lock before finishing the | ||
job; it it's too long and the process crashes before calling the ``release()`` | ||
method, the resource will stay locked until the timeout:: | ||
|
||
// ... | ||
// create a expiring lock that lasts 30 seconds | ||
// create an expiring lock that lasts 30 seconds | ||
$lock = $factory->createLock('charts-generation', 30); | ||
|
||
$lock->acquire(); | ||
|
@@ -173,16 +173,16 @@ PHP process is terminated:: | |
.. caution:: | ||
|
||
Beware that some file systems (such as some types of NFS) do not support | ||
locking. In those cases, it's better to use a local file or a remote store | ||
based on Redis or Memcached. | ||
locking. In those cases, it's better to use a directory on a local disk | ||
drive or a remote store based on Redis or Memcached. | ||
|
||
.. _lock-store-memcached: | ||
|
||
MemcachedStore | ||
~~~~~~~~~~~~~~ | ||
|
||
The MemcachedStore saves locks on a Memcached server, so first you must create | ||
a Memcached connection implements the ``\Memcached`` class. This store does not | ||
The MemcachedStore saves locks on a Memcached server, it requires a Memcached | ||
connection implementing the ``\Memcached`` class. This store does not | ||
support blocking, and expects a TTL to avoid stalled locks:: | ||
|
||
use Symfony\Component\Lock\Store\MemcachedStore; | ||
|
@@ -201,8 +201,8 @@ support blocking, and expects a TTL to avoid stalled locks:: | |
RedisStore | ||
~~~~~~~~~~ | ||
|
||
The RedisStore saves locks on a Redis server, so first you must create a Redis | ||
connection implements the ``\Redis``, ``\RedisArray``, ``\RedisCluster`` or | ||
The RedisStore saves locks on a Redis server, it requires a Redis connection | ||
implementing the ``\Redis``, ``\RedisArray``, ``\RedisCluster`` or | ||
``\Predis`` classes. This store does not support blocking, and expects a TTL to | ||
avoid stalled locks:: | ||
|
||
|
@@ -233,7 +233,7 @@ The CombinedStore is designed for High Availability applications because it | |
manages several stores in sync (for example, several Redis servers). When a lock | ||
is being acquired, it forwards the call to all the managed stores, and it | ||
collects their responses. If a simple majority of stores have acquired the lock, | ||
then the lock is considered as acquired; otherwise is not acquired:: | ||
then the lock is considered as acquired; otherwise as not acquired:: | ||
|
||
use Symfony\Component\Lock\Quorum\ConsensusStrategy; | ||
use Symfony\Component\Lock\Store\CombinedStore; | ||
|
@@ -249,8 +249,9 @@ then the lock is considered as acquired; otherwise is not acquired:: | |
|
||
$store = new CombinedStore($stores, new ConsensusStrategy()); | ||
|
||
Instead of the simple majority strategy (``ConsensusStrategy``) you can use the | ||
``UnanimousStrategy`` to require the lock to be acquired in all the stores. | ||
Instead of the simple majority strategy (``ConsensusStrategy``) an | ||
``UnanimousStrategy`` can be used to require the lock to be acquired in all | ||
he stores. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be |
||
|
||
.. _`locks`: https://en.wikipedia.org/wiki/Lock_(computer_science) | ||
.. _Packagist: https://packagist.org/packages/symfony/lock | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
locking process?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In fact, by
process
I mean the system process running the code on the machine.Maybe
if the php process is still alive
would be clearer