Download the PHP package aternos/lock without Composer
On this page you can find all versions of the php package aternos/lock. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package lock
Short Description Distributed exclusive and shared resource locking based on etcd
License MIT
Informations about the package lock
PHP library for distributed locking
Distributed exclusive and shared resource locking based on etcd (using aternos/etcd).
About
This library was created to provide a highly available distributed locking solution for a file storage system, where a failed or incorrect lock could lead to potential data loss or corruption. Therefore it tries to achieve the best possible consistency while still providing simple usability, stability and scalability. There are lots of other locking libraries out there. Some of them don't allow shared locks, some of them implement complicated algorithms on the client-side which also didn't seem to fit our needs. Therefore we've decided to create our own solution and let etcd do most of the heavy lifting.
Etcd is a fast and reliable key-value store that allows transaction operations to ensure that no unexpected changes were made
since the last read (see putIf()
and deleteIf()
in Aternos\Etcd\Client
).
This locking library uses those methods to ensure consistency. If two processes try to lock the same resource at the same
time one of them will be denied by etcd and then retry its lock by either adding it to the other lock (shared) or waiting
for the other lock to finish (exclusive). If more processes are fighting over the same lock, they will start to delay their
retries in random intervals to find a consistent conclusion.
Timeouts (UnavailableExceptions) from etcd will also be detected and the operations retried after some delay to avoid problems because of short availability problems.
This library was extracted from a different project to be used in different places and not only for a file storage. Therefore we've already used this in production to create thousands of locks every minute in only a few milliseconds.
Installation
The gRPC PHP extension has to be installed to use this library. See aternos/etcd.
Usage
The most important class is the Lock
class. Each instance of this class represents
one lock on a resource that is identified by a key. There are also several static functions to set options
for all locks, but all of them have default values, so you can just start by creating a lock:
Exclusive/shared locks
There can only be one exclusive lock at the same time, but there can be multiple shared locks. If there is any shared lock an exclusive lock is not possible. Exclusive locks can be useful for write operations while shared locks can be useful for read operations. By default all locks are shared, you can create an exclusive lock like this:
Locking time(out) and refreshing
You always have to set a timeout for your locks. The lock will be released automatically if the time runs out. This is necessary to avoid infinite dead locks. The timeout can be refreshed e.g. after writing or reading a chunk of data. You can also define a threshold for the refresh time. The lock will only be refreshed if the remaining locking time is below this threshold. This avoids spamming etcd with unnecessary queries.
Waiting for other locks
If the resource that you want to access is currently locked exclusively or you need an exclusive lock and there are still shared locks, you might want to wait some time for the locks to be released. You can specify a maximum time to wait for other locks:
Break the lock
Of course it's important to break the lock after finishing the operation to avoid running into timeouts. You can break a lock like this:
Identifier
You can identify yourself (the current process/request) by providing and identifier string. By default this library
uses the uniqid()
function to create a unique identifier. There aren't many reasons to provide a custom identifier.
The default identifier is the same for all locks in the current process. Therefore all those locks are able to take
over the other locks that were previously created in the same process. It also would be possible to see which other
process (represented by its identifier) is currently holding a lock. There is currently no such function, but that
would be easy to add.
There are two ways to provide a custom identifier. Either by setting the default for all locks or by overwriting the default identifier on a specific lock:
Settings
The default identifier above is already one of the static settings, which you can specify for all locks. All settings
are stored in protected static fields in the Lock
class (at the top) and have their own static
setter functions. Below are only a few important ones, but you can read the PHPDoc function comments for further explanations
of the other settings. Only change those if you know what you are doing.
Etcd client
You can set an etcd client object to specify the etcd connection details. See this for more information.
Etcd key prefix
You can set a prefix for all locking keys in etcd to avoid conflicts with other data in the same etcd cluster.
The default prefix is lock/
.