Skip to content

Added pitfall hint about Lock::isAcquired() #11426

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 1 commit into from
Jun 11, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions components/lock.rst
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,51 @@ This component also provides two useful methods related to expiring locks:
``getExpiringDate()`` (which returns ``null`` or a ``\DateTimeImmutable``
object) and ``isExpired()`` (which returns a boolean).

The Owner of The Lock
---------------------

Locks that are acquired for the first time are owned[1]_ by the ``Lock`` instance that acquired
it. If you need to check whether the current ``Lock`` instance is (still) the owner of
a lock, you can use the ``isAcquired()`` method::

if ($lock->isAcquired()) {
// We (still) own the lock
}

Because of the fact that some lock stores have expiring locks (as seen and explained
above), it is possible for an instance to lose the lock it acquired automatically::

// If we cannot acquire ourselves, it means some other process is already working on it
if (!$lock->acquire()) {
return;
}

$this->beginTransaction();

// Perform a very long process that might exceed TTL of the lock

if ($lock->isAcquired()) {
// Still all good, no other instance has acquired the lock in the meantime, we're safe
$this->commit();
} else {
// Bummer! Our lock has apparently exceeded TTL and another process has started in
// the meantime so it's not safe for us to commit.
$this->rollback();
throw new \Exception('Process failed');
}

.. caution::

A common pitfall might be to use the ``isAcquired()`` method to check if
a lock has already been acquired by any process. As you can see in this example
you have to use ``acquire()`` for this. The ``isAcquired()`` method is used to check
if the lock has been acquired by the **current process** only!

.. [1] Technically, the true owners of the lock are the ones that share the same instance of ``Key``,
not ``Lock``. But from a user perspective, ``Key`` is internal and you will likely only be working
with the ``Lock`` instance so it's easier to think of the ``Lock`` instance as being the one that
is the owner of the lock.

Available Stores
----------------

Expand Down