1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
|
**[Requirements](#requirements)** |
**[Installation](#installation)** |
**[Usage](#usage)** |
**[License](#license)**
# php-lock/lock
[](https://packagist.org/packages/malkusch/lock)
[](https://packagist.org/packages/malkusch/lock/stats)
[](https://github.com/php-lock/lock/actions?query=branch:master)
[](https://packagist.org/packages/malkusch/lock)
This library helps executing critical code in concurrent situations.
php-lock/lock follows semantic versioning. Read more on [semver.org][1].
----
## Requirements
- PHP 7.4 - 8.3
- Optionally [nrk/predis][2] to use the Predis locks.
- Optionally the [php-pcntl][3] extension to enable locking with `flock()`
without busy waiting in CLI scripts.
- Optionally `flock()`, `ext-redis`, `ext-pdo_mysql`, `ext-pdo_sqlite`,
`ext-pdo_pgsql` or `ext-memcached` can be used as a backend for locks. See
examples below.
- If `ext-redis` is used for locking and is configured to use igbinary for
serialization or lzf for compression, additionally `ext-igbinary` and/or
`ext-lzf` have to be installed.
----
## Installation
### Composer
To use this library through [composer][4], run the following terminal command
inside your repository's root folder.
```sh
composer require malkusch/lock
```
## Usage
This library uses the namespace `malkusch\lock`.
### Mutex
The [`malkusch\lock\mutex\Mutex`][5] class is an abstract class and provides the
base API for this library.
#### Mutex::synchronized()
[`malkusch\lock\mutex\Mutex::synchronized()`][6] executes code exclusively. This
method guarantees that the code is only executed by one process at once. Other
processes have to wait until the mutex is available. The critical code may throw
an exception, which would release the lock as well.
This method returns whatever is returned to the given callable. The return
value is not checked, thus it is up to the user to decide if for example the
return value `false` or `null` should be seen as a failed action.
Example:
```php
$newBalance = $mutex->synchronized(function () use (
$bankAccount,
$amount
): int {
$balance = $bankAccount->getBalance();
$balance -= $amount;
if ($balance < 0) {
throw new \DomainException('You have no credit');
}
$bankAccount->setBalance($balance);
return $balance;
});
```
#### Mutex::check()
[`malkusch\lock\mutex\Mutex::check()`][7] sets a callable, which will be
executed when [`malkusch\lock\util\DoubleCheckedLocking::then()`][8] is called,
and performs a double-checked locking pattern, where it's return value decides
if the lock needs to be acquired and the synchronized code to be executed.
See [https://en.wikipedia.org/wiki/Double-checked_locking][9] for a more
detailed explanation of that feature.
If the check's callable returns `false`, no lock will be acquired and the
synchronized code will not be executed. In this case the
[`malkusch\lock\util\DoubleCheckedLocking::then()`][8] method, will also return
`false` to indicate that the check did not pass either before or after acquiring
the lock.
In the case where the check's callable returns a value other than `false`, the
[`malkusch\lock\util\DoubleCheckedLocking::then()`][8] method, will
try to acquire the lock and on success will perform the check again. Only when
the check returns something other than `false` a second time, the synchronized
code callable, which has been passed to `then()` will be executed. In this case
the return value of `then()` will be what ever the given callable returns and
thus up to the user to return `false` or `null` to indicate a failed action as
this return value will not be checked by the library.
Example:
```php
$newBalance = $mutex->check(function () use ($bankAccount, $amount): bool {
return $bankAccount->getBalance() >= $amount;
})->then(function () use ($bankAccount, $amount): int {
$balance = $bankAccount->getBalance();
$balance -= $amount;
$bankAccount->setBalance($balance);
return $balance;
});
if ($newBalance === false) {
if ($balance < 0) {
throw new \DomainException('You have no credit');
}
}
```
### Extracting code result after lock release exception
Mutex implementations based on [`malkush\lock\mutex\LockMutex`][12] will throw
[`malkusch\lock\exception\LockReleaseException`][13] in case of lock release
problem, but the synchronized code block will be already executed at this point.
In order to read the code result (or an exception thrown there),
`LockReleaseException` provides methods to extract it.
Example:
```php
try {
// or $mutex->check(...)
$result = $mutex->synchronized(function () {
if (someCondition()) {
throw new \DomainException();
}
return 'result';
});
} catch (LockReleaseException $unlockException) {
if ($unlockException->getCodeException() !== null) {
$codeException = $unlockException->getCodeException();
// do something with the code exception
} else {
$code_result = $unlockException->getCodeResult();
// do something with the code result
}
// deal with LockReleaseException or propagate it
throw $unlockException;
}
```
### Implementations
Because the [`malkusch\lock\mutex\Mutex`](#mutex) class is an abstract class,
you can choose from one of the provided implementations or create/extend your
own implementation.
- [`CASMutex`](#casmutex)
- [`FlockMutex`](#flockmutex)
- [`MemcachedMutex`](#memcachedmutex)
- [`PHPRedisMutex`](#phpredismutex)
- [`PredisMutex`](#predismutex)
- [`SemaphoreMutex`](#semaphoremutex)
- [`TransactionalMutex`](#transactionalmutex)
- [`MySQLMutex`](#mysqlmutex)
- [`PgAdvisoryLockMutex`](#pgadvisorylockmutex)
#### CASMutex
The **CASMutex** has to be used with a [Compare-and-swap][10] operation. This
mutex is lock free. It will repeat executing the code until the CAS operation
was successful. The code should therefore notify the mutex by calling
[`malkusch\lock\mutex\CASMutex::notify()`][11].
As the mutex keeps executing the critical code, it must not have any side
effects as long as the CAS operation was not successful.
Example:
```php
$mutex = new CASMutex();
$mutex->synchronized(function () use ($memcached, $mutex, $amount): void {
$balance = $memcached->get('balance', null, $casToken);
$balance -= $amount;
if (!$memcached->cas($casToken, 'balance', $balance)) {
return;
}
$mutex->notify();
});
```
#### FlockMutex
The **FlockMutex** is a lock implementation based on
[`flock()`](http://php.net/manual/en/function.flock.php).
Example:
```php
$mutex = new FlockMutex(fopen(__FILE__, 'r'));
$mutex->synchronized(function () use ($bankAccount, $amount) {
$balance = $bankAccount->getBalance();
$balance -= $amount;
if ($balance < 0) {
throw new \DomainException('You have no credit');
}
$bankAccount->setBalance($balance);
});
```
Timeouts are supported as an optional second argument. This uses the `ext-pcntl`
extension if possible or busy waiting if not.
#### MemcachedMutex
The **MemcachedMutex** is a spinlock implementation which uses the
[`Memcached` API](http://php.net/manual/en/book.memcached.php).
Example:
```php
$memcache = new \Memcached();
$memcache->addServer('localhost', 11211);
$mutex = new MemcachedMutex('balance', $memcache);
$mutex->synchronized(function () use ($bankAccount, $amount) {
$balance = $bankAccount->getBalance();
$balance -= $amount;
if ($balance < 0) {
throw new \DomainException('You have no credit');
}
$bankAccount->setBalance($balance);
});
```
#### PHPRedisMutex
The **PHPRedisMutex** is the distributed lock implementation of
[RedLock](http://redis.io/topics/distlock) which uses the
[`phpredis` extension](https://github.com/phpredis/phpredis).
This implementation requires at least `phpredis-2.2.4`.
If used with a cluster of Redis servers, acquiring and releasing locks will
continue to function as long as a majority of the servers still works.
Example:
```php
$redis = new \Redis();
$redis->connect('localhost');
$mutex = new PHPRedisMutex([$redis], 'balance');
$mutex->synchronized(function () use ($bankAccount, $amount) {
$balance = $bankAccount->getBalance();
$balance -= $amount;
if ($balance < 0) {
throw new \DomainException('You have no credit');
}
$bankAccount->setBalance($balance);
});
```
#### PredisMutex
The **PredisMutex** is the distributed lock implementation of
[RedLock](http://redis.io/topics/distlock) which uses the
[`Predis` API](https://github.com/nrk/predis).
Example:
```php
$redis = new \Predis\Client('redis://localhost');
$mutex = new PredisMutex([$redis], 'balance');
$mutex->synchronized(function () use ($bankAccount, $amount) {
$balance = $bankAccount->getBalance();
$balance -= $amount;
if ($balance < 0) {
throw new \DomainException('You have no credit');
}
$bankAccount->setBalance($balance);
});
```
#### SemaphoreMutex
The **SemaphoreMutex** is a lock implementation based on
[Semaphore](http://php.net/manual/en/ref.sem.php).
Example:
```php
$semaphore = sem_get(ftok(__FILE__, 'a'));
$mutex = new SemaphoreMutex($semaphore);
$mutex->synchronized(function () use ($bankAccount, $amount) {
$balance = $bankAccount->getBalance();
$balance -= $amount;
if ($balance < 0) {
throw new \DomainException('You have no credit');
}
$bankAccount->setBalance($balance);
});
```
#### TransactionalMutex
The **TransactionalMutex**
delegates the serialization to the DBS. The exclusive code is executed within
a transaction. It's up to you to set the correct transaction isolation level.
However if the transaction fails (i.e. a `PDOException` was thrown), the code
will be executed again in a new transaction. Therefore the code must not have
any side effects besides SQL statements. Also the isolation level should be
conserved for the repeated transaction. If the code throws an exception,
the transaction is rolled back and not replayed again.
Example:
```php
$mutex = new TransactionalMutex($pdo);
$mutex->synchronized(function () use ($pdo, $accountId, $amount) {
$select = $pdo->prepare(
'SELECT balance FROM account WHERE id = ? FOR UPDATE'
);
$select->execute([$accountId]);
$balance = $select->fetchColumn();
$balance -= $amount;
if ($balance < 0) {
throw new \DomainException('You have no credit');
}
$pdo->prepare('UPDATE account SET balance = ? WHERE id = ?')
->execute([$balance, $accountId]);
});
```
#### MySQLMutex
The **MySQLMutex** uses MySQL's
[`GET_LOCK`](https://dev.mysql.com/doc/refman/9.0/en/locking-functions.html#function_get-lock)
function.
It supports time outs. If the connection to the database server is lost or
interrupted, the lock is automatically released.
Note that before MySQL 5.7.5 you cannot use nested locks, any new lock will
silently release already held locks. You should probably refrain from using this
mutex on MySQL versions < 5.7.5.
Also note that `GET_LOCK` function is server wide and the MySQL manual suggests
you to namespace your locks like `dbname.lockname`.
```php
$pdo = new \PDO('mysql:host=localhost;dbname=test', 'username');
$mutex = new MySQLMutex($pdo, 'balance', 15);
$mutex->synchronized(function () use ($bankAccount, $amount) {
$balance = $bankAccount->getBalance();
$balance -= $amount;
if ($balance < 0) {
throw new \DomainException('You have no credit');
}
$bankAccount->setBalance($balance);
});
```
#### PgAdvisoryLockMutex
The **PgAdvisoryLockMutex** uses PostgreSQL's
[advisory locking](https://www.postgresql.org/docs/9.4/static/functions-admin.html#FUNCTIONS-ADVISORY-LOCKS)
functions.
Named locks are offered. PostgreSQL locking functions require integers but the
conversion is handled automatically.
No time outs are supported. If the connection to the database server is lost or
interrupted, the lock is automatically released.
```php
$pdo = new \PDO('pgsql:host=localhost;dbname=test', 'username');
$mutex = new PgAdvisoryLockMutex($pdo, 'balance');
$mutex->synchronized(function () use ($bankAccount, $amount) {
$balance = $bankAccount->getBalance();
$balance -= $amount;
if ($balance < 0) {
throw new \DomainException('You have no credit');
}
$bankAccount->setBalance($balance);
});
```
## License
This project is free and is licensed under the MIT.
[1]: http://semver.org/
[2]: https://github.com/nrk/predis
[3]: http://php.net/manual/en/book.pcntl.php
[4]: https://getcomposer.org/
[5]: https://github.com/php-lock/lock/blob/35526aee28/src/mutex/Mutex.php#L15
[6]: https://github.com/php-lock/lock/blob/35526aee28/src/mutex/Mutex.php#L38
[7]: https://github.com/php-lock/lock/blob/35526aee28/src/mutex/Mutex.php#L60
[8]: https://github.com/php-lock/lock/blob/35526aee28/src/util/DoubleCheckedLocking.php#L63
[9]: https://en.wikipedia.org/wiki/Double-checked_locking
[10]: https://en.wikipedia.org/wiki/Compare-and-swap
[11]: https://github.com/php-lock/lock/blob/35526aee28/src/mutex/CASMutex.php#L42
[12]: https://github.com/php-lock/lock/blob/35526aee28/src/mutex/LockMutex.php
[13]: https://github.com/php-lock/lock/blob/35526aee28/src/exception/LockReleaseException.php
|