Skip to content

Commit 70fd6d9

Browse files
committed
Deduplicate code using global LockUtil
1 parent 613abe7 commit 70fd6d9

18 files changed

+144
-28
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ The **MySQLMutex** uses MySQL's
343343
[`GET_LOCK`](https://dev.mysql.com/doc/refman/9.0/en/locking-functions.html#function_get-lock)
344344
function.
345345

346-
It supports time outs. If the connection to the database server is lost or
346+
It supports timeouts. If the connection to the database server is lost or
347347
interrupted, the lock is automatically released.
348348

349349
Note that before MySQL 5.7.5 you cannot use nested locks, any new lock will
@@ -376,7 +376,7 @@ functions.
376376
Named locks are offered. PostgreSQL locking functions require integers but the
377377
conversion is handled automatically.
378378

379-
No time outs are supported. If the connection to the database server is lost or
379+
No timeouts are supported. If the connection to the database server is lost or
380380
interrupted, the lock is automatically released.
381381

382382
```php

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
"homepage": "https://github.com/malkusch/lock",
3636
"require": {
3737
"php": ">=7.4 <8.4",
38-
"psr/log": "^1.0 || ^2.0 || ^3.0"
38+
"psr/log": "^1.0 || ^2.0 || ^3.0",
39+
"symfony/polyfill-php80": "^1.28"
3940
},
4041
"require-dev": {
4142
"ext-memcached": "*",

src/mutex/FlockMutex.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ private function lockBusy(): void
125125
private function acquireNonBlockingLock(): bool
126126
{
127127
if (!flock($this->fileHandle, \LOCK_EX | \LOCK_NB, $wouldBlock)) {
128-
if ($wouldBlock) { // @phpstan-ignore if.condNotBoolean
128+
if ($wouldBlock === 1) {
129129
// Another process holds the lock.
130130
return false;
131131
}

src/mutex/MemcachedMutex.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class MemcachedMutex extends SpinlockMutex
1919
* it has to be added with Memcached::addServer().
2020
*
2121
* @param string $name The lock name
22-
* @param float $timeout The time in seconds a lock expires
22+
* @param float $timeout The timeout in seconds a lock expires
2323
*
2424
* @throws \LengthException The timeout must be greater than 0
2525
*/

src/mutex/MySQLMutex.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function __construct(\PDO $PDO, string $name, float $timeout = 0)
3232
#[\Override]
3333
public function lock(): void
3434
{
35-
$statement = $this->pdo->prepare('SELECT GET_LOCK(?,?)');
35+
$statement = $this->pdo->prepare('SELECT GET_LOCK(?, ?)');
3636

3737
// MySQL rounds the value to whole seconds, sadly rounds, not ceils
3838
// TODO MariaDB supports microseconds precision since 10.1.2 version,

src/mutex/PHPRedisMutex.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class PHPRedisMutex extends RedisMutex
2626
* called already.
2727
*
2828
* @param array<\Redis|\RedisCluster> $redisAPIs
29-
* @param float $timeout The time in seconds a lock expires after
29+
* @param float $timeout The timeout in seconds a lock expires
3030
*
3131
* @throws \LengthException The timeout must be greater than 0
3232
*/

src/mutex/PgAdvisoryLockMutex.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function __construct(\PDO $PDO, string $name)
3333
#[\Override]
3434
public function lock(): void
3535
{
36-
$statement = $this->pdo->prepare('SELECT pg_advisory_lock(?,?)');
36+
$statement = $this->pdo->prepare('SELECT pg_advisory_lock(?, ?)');
3737

3838
$statement->execute([
3939
$this->key1,
@@ -44,7 +44,7 @@ public function lock(): void
4444
#[\Override]
4545
public function unlock(): void
4646
{
47-
$statement = $this->pdo->prepare('SELECT pg_advisory_unlock(?,?)');
47+
$statement = $this->pdo->prepare('SELECT pg_advisory_unlock(?, ?)');
4848
$statement->execute([
4949
$this->key1,
5050
$this->key2,

src/mutex/PredisMutex.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class PredisMutex extends RedisMutex
2020
* Sets the Redis connections.
2121
*
2222
* @param ClientInterface[] $clients The Redis clients
23-
* @param float $timeout The time in seconds a lock expires
23+
* @param float $timeout The timeout in seconds a lock expires
2424
*
2525
* @throws \LengthException The timeout must be greater than 0
2626
*/

src/mutex/RedisMutex.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use malkusch\lock\exception\LockAcquireException;
88
use malkusch\lock\exception\LockReleaseException;
9+
use malkusch\lock\util\LockUtil;
910
use Psr\Log\LoggerAwareInterface;
1011
use Psr\Log\LoggerAwareTrait;
1112
use Psr\Log\NullLogger;
@@ -29,7 +30,7 @@ abstract class RedisMutex extends SpinlockMutex implements LoggerAwareInterface
2930
* Sets the Redis APIs.
3031
*
3132
* @param array<int, mixed> $redisAPIs
32-
* @param float $timeout The time in seconds a lock expires
33+
* @param float $timeout The timeout in seconds a lock expires
3334
*
3435
* @throws \LengthException The timeout must be greater than 0
3536
*/
@@ -50,7 +51,7 @@ protected function acquire(string $key, float $expire): bool
5051
// 2.
5152
$acquired = 0;
5253
$errored = 0;
53-
$this->token = bin2hex(random_bytes(16)) . md5((string) $time);
54+
$this->token = LockUtil::getInstance()->makeRandomToken();
5455
$exception = null;
5556
foreach ($this->redisAPIs as $index => $redisAPI) {
5657
try {
@@ -106,8 +107,8 @@ protected function release(string $key): bool
106107
*
107108
* @link https://redis.io/commands/set
108109
*/
109-
$script = 'if redis.call("get",KEYS[1]) == ARGV[1] then
110-
return redis.call("del",KEYS[1])
110+
$script = 'if redis.call("get", KEYS[1]) == ARGV[1] then
111+
return redis.call("del", KEYS[1])
111112
else
112113
return 0
113114
end

src/mutex/SpinlockMutex.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use malkusch\lock\exception\ExecutionOutsideLockException;
88
use malkusch\lock\exception\LockAcquireException;
99
use malkusch\lock\exception\LockReleaseException;
10+
use malkusch\lock\util\LockUtil;
1011
use malkusch\lock\util\Loop;
1112

1213
/**
@@ -16,9 +17,6 @@
1617
*/
1718
abstract class SpinlockMutex extends LockMutex
1819
{
19-
/** The prefix for the lock key. */
20-
private const PREFIX = 'php-malkusch-lock:';
21-
2220
/** @var float The timeout in seconds a lock may live */
2321
private $timeout;
2422

@@ -34,15 +32,15 @@ abstract class SpinlockMutex extends LockMutex
3432
/**
3533
* Sets the timeout.
3634
*
37-
* @param float $timeout The time in seconds a lock expires
35+
* @param float $timeout The timeout in seconds a lock expires
3836
*
3937
* @throws \LengthException The timeout must be greater than 0
4038
*/
4139
public function __construct(string $name, float $timeout = 3)
4240
{
4341
$this->timeout = $timeout;
4442
$this->loop = new Loop($this->timeout);
45-
$this->key = self::PREFIX . $name;
43+
$this->key = LockUtil::getInstance()->getKeyPrefix() . ':' . $name;
4644
}
4745

4846
#[\Override]
@@ -52,7 +50,7 @@ protected function lock(): void
5250
$this->acquired = microtime(true);
5351

5452
/*
55-
* The expiration time for the lock is increased by one second
53+
* The expiration timeout for the lock is increased by one second
5654
* to ensure that we delete only our keys. This will prevent the
5755
* case that this key expires before the timeout, and another process
5856
* acquires successfully the same key which would then be deleted

0 commit comments

Comments
 (0)