Skip to content

Commit 07c6304

Browse files
authored
Rename TimeoutException to LockAcquireTimeoutException (#70)
1 parent 3ca295c commit 07c6304

37 files changed

+319
-424
lines changed

README.md

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
**[Requirements](#requirements)** |
22
**[Installation](#installation)** |
33
**[Usage](#usage)** |
4+
**[Authors](#authors)** |
45
**[License](#license)**
56

67
# php-lock/lock
@@ -198,10 +199,10 @@ The **MemcachedMutex** is a spinlock implementation which uses the
198199

199200
Example:
200201
```php
201-
$memcache = new \Memcached();
202-
$memcache->addServer('localhost', 11211);
202+
$memcached = new \Memcached();
203+
$memcached->addServer('localhost', 11211);
203204

204-
$mutex = new MemcachedMutex('balance', $memcache);
205+
$mutex = new MemcachedMutex('balance', $memcached);
205206
$mutex->synchronized(function () use ($bankAccount, $amount) {
206207
$balance = $bankAccount->getBalance();
207208
$balance -= $amount;
@@ -263,7 +264,7 @@ $mutex->synchronized(function () use ($bankAccount, $amount) {
263264
#### TransactionalMutex
264265

265266
The **TransactionalMutex**
266-
delegates the serialization to the DBS. The exclusive code is executed within
267+
delegates the serialization to the database. The exclusive code is executed within
267268
a transaction. It's up to you to set the correct transaction isolation level.
268269
However if the transaction fails (i.e. a `PDOException` was thrown), the code
269270
will be executed again in a new transaction. Therefore the code must not have
@@ -350,9 +351,9 @@ $mutex->synchronized(function () use ($bankAccount, $amount) {
350351

351352
## Authors
352353

353-
Since year 2015 the development was led by Markus Malkusch, Willem Stuursma-Ruwen, Michael Voříšek and many GitHub contributors.
354+
Since year 2015 the development was led by Markus Malkusch, Willem Stuursma-Ruwen and many GitHub contributors.
354355

355-
Currently this library is maintained by Michael Voříšek - [GitHub][https://github.com/mvorisek] and [LinkedIn][https://www.linkedin.com/mvorisek].
356+
Currently this library is maintained by Michael Voříšek - [GitHub](https://github.com/mvorisek) | [LinkedIn](https://www.linkedin.com/mvorisek).
356357

357358
Commercial support is available.
358359

@@ -364,10 +365,10 @@ This project is free and is licensed under the MIT.
364365
[2]: https://github.com/nrk/predis
365366
[3]: http://php.net/manual/en/book.pcntl.php
366367
[4]: https://getcomposer.org/
367-
[5]: https://github.com/php-lock/lock/blob/35526aee28/src/mutex/Mutex.php#L15
368-
[6]: https://github.com/php-lock/lock/blob/35526aee28/src/mutex/Mutex.php#L38
369-
[7]: https://github.com/php-lock/lock/blob/35526aee28/src/mutex/Mutex.php#L60
370-
[8]: https://github.com/php-lock/lock/blob/35526aee28/src/util/DoubleCheckedLocking.php#L63
368+
[5]: https://github.com/php-lock/lock/blob/3ca295ccda/src/Mutex/Mutex.php#L15
369+
[6]: https://github.com/php-lock/lock/blob/3ca295ccda/src/Mutex/Mutex.php#L38
370+
[7]: https://github.com/php-lock/lock/blob/3ca295ccda/src/Mutex/Mutex.php#L60
371+
[8]: https://github.com/php-lock/lock/blob/3ca295ccda/src/Util/DoubleCheckedLocking.php#L61
371372
[9]: https://en.wikipedia.org/wiki/Double-checked_locking
372-
[10]: https://github.com/php-lock/lock/blob/35526aee28/src/mutex/LockMutex.php
373-
[11]: https://github.com/php-lock/lock/blob/35526aee28/src/exception/LockReleaseException.php
373+
[10]: https://github.com/php-lock/lock/blob/3ca295ccda/src/Mutex/AbstractLockMutex.php
374+
[11]: https://github.com/php-lock/lock/blob/3ca295ccda/src/Exception/LockReleaseException.php

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"flock",
1111
"semaphore",
1212
"redlock",
13-
"memcache",
13+
"memcached",
1414
"redis",
1515
"advisory-locks",
1616
"mysql",

src/Exception/DeadlineException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44

55
namespace Malkusch\Lock\Exception;
66

7-
class DeadlineException extends \RuntimeException implements PhpLockException {}
7+
class DeadlineException extends \RuntimeException {}

src/Exception/ExecutionOutsideLockException.php

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,50 +4,31 @@
44

55
namespace Malkusch\Lock\Exception;
66

7+
use Malkusch\Lock\Util\LockUtil;
8+
79
/**
8-
* Execution outside lock exception.
9-
*
1010
* This exception should be thrown when for example the lock is released or the
1111
* lock times out before the critical code has finished execution. This is a
1212
* serious exception. Side effects might have happened while the critical code
1313
* was executed outside of the lock which should not be trusted to be valid.
1414
*
15-
* Should only be used in contexts where the is being released.
15+
* Should only be used in contexts where the lock is being released.
1616
*
1717
* @see \Malkusch\Lock\Mutex\AbstractSpinlockMutex::unlock()
1818
*/
1919
class ExecutionOutsideLockException extends LockReleaseException
2020
{
2121
/**
22-
* Creates a new instance of the ExecutionOutsideLockException class.
23-
*
2422
* @param float $elapsedTime Total elapsed time of the synchronized code callback execution
2523
* @param float $timeout The lock timeout in seconds
2624
*/
2725
public static function create(float $elapsedTime, float $timeout): self
2826
{
29-
$elapsedTimeStr = (string) round($elapsedTime, 6);
30-
if (\is_finite($elapsedTime) && strpos($elapsedTimeStr, '.') === false) {
31-
$elapsedTimeStr .= '.0';
32-
}
33-
34-
$timeoutStr = (string) round($timeout, 6);
35-
if (\is_finite($timeout) && strpos($timeoutStr, '.') === false) {
36-
$timeoutStr .= '.0';
37-
}
38-
39-
$overTime = round($elapsedTime, 6) - round($timeout, 6);
40-
$overTimeStr = (string) round($overTime, 6);
41-
if (\is_finite($timeout) && strpos($overTimeStr, '.') === false) {
42-
$overTimeStr .= '.0';
43-
}
44-
4527
return new self(\sprintf(
46-
'The code executed for %s seconds. But the timeout is %s ' .
47-
'seconds. The last %s seconds were executed outside of the lock.',
48-
$elapsedTimeStr,
49-
$timeoutStr,
50-
$overTimeStr
28+
'The code executed for %s seconds. But the timeout is %s seconds. The last %s seconds were executed outside of the lock.',
29+
LockUtil::getInstance()->formatTimeout($elapsedTime),
30+
LockUtil::getInstance()->formatTimeout($timeout),
31+
LockUtil::getInstance()->formatTimeout(round($elapsedTime, 6) - round($timeout, 6))
5132
));
5233
}
5334
}

src/Exception/LockAcquireException.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
namespace Malkusch\Lock\Exception;
66

77
/**
8-
* Lock acquire exception.
8+
* Failed to acquire a lock.
99
*
10-
* Used when the lock could not be acquired. This exception implies that the
11-
* critical code was not executed, or at least had no side effects.
10+
* This exception implies that the critical code was not executed.
1211
*/
1312
class LockAcquireException extends MutexException {}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Malkusch\Lock\Exception;
6+
7+
use Malkusch\Lock\Util\LockUtil;
8+
9+
class LockAcquireTimeoutException extends LockAcquireException
10+
{
11+
/**
12+
* @param float $acquireTimeout In seconds
13+
*/
14+
public static function create(float $acquireTimeout): self
15+
{
16+
return new self('Lock acquire timeout of '
17+
. LockUtil::getInstance()->formatTimeout($acquireTimeout)
18+
. ' seconds has been exceeded');
19+
}
20+
}

src/Exception/LockReleaseException.php

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,22 @@
55
namespace Malkusch\Lock\Exception;
66

77
/**
8-
* Lock release exception.
8+
* Failed to release the lock.
99
*
10-
* Failed to release the lock. Take this exception very serious. Failing to
11-
* release a lock might have the potential to introduce deadlocks. Also the
10+
* Take this exception very serious.
11+
*
12+
* Failing to release a lock might have the potential to introduce deadlocks. Also the
1213
* critical code was executed i.e. side effects may have happened.
1314
*/
1415
class LockReleaseException extends MutexException
1516
{
16-
/**
17-
* Result that has been returned during the critical code execution.
18-
*
19-
* @var mixed
20-
*/
17+
/** @var mixed */
2118
private $codeResult;
2219

23-
/**
24-
* Exception that has happened during the critical code execution.
25-
*
26-
* @var \Throwable|null
27-
*/
28-
private $codeException;
20+
private ?\Throwable $codeException = null;
2921

3022
/**
31-
* Gets the result that has been returned during the critical code
32-
* execution.
23+
* Gets the result that has been returned during the critical code execution.
3324
*
3425
* @return mixed The return value of the executed code block
3526
*/
@@ -39,10 +30,7 @@ public function getCodeResult()
3930
}
4031

4132
/**
42-
* Sets the result that has been returned during the critical code
43-
* execution.
44-
*
45-
* @param mixed $codeResult The return value of the executed code block
33+
* @param mixed $codeResult
4634
*
4735
* @return $this
4836
*/
@@ -54,22 +42,14 @@ public function setCodeResult($codeResult): self
5442
}
5543

5644
/**
57-
* Gets the exception that has happened during the synchronized code
58-
* execution.
59-
*
60-
* @return \Throwable|null The exception thrown by the code block or null when there has been no exception
45+
* Gets the exception that has happened during the synchronized code execution.
6146
*/
6247
public function getCodeException(): ?\Throwable
6348
{
6449
return $this->codeException;
6550
}
6651

6752
/**
68-
* Sets the exception that has happened during the critical code
69-
* execution.
70-
*
71-
* @param \Throwable $codeException The exception thrown by the code block
72-
*
7353
* @return $this
7454
*/
7555
public function setCodeException(\Throwable $codeException): self

src/Exception/MutexException.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,10 @@
55
namespace Malkusch\Lock\Exception;
66

77
/**
8-
* Mutex exception.
9-
*
10-
* Generic exception for any other not covered reason. Usually extended by
11-
* child classes.
8+
* Usually extended by more meaningful classes.
129
*/
13-
class MutexException extends \RuntimeException implements PhpLockException
10+
class MutexException extends \RuntimeException
1411
{
1512
/** Not enough redis servers */
16-
public const REDIS_NOT_ENOUGH_SERVERS = 1;
13+
public const CODE_REDLOCK_NOT_ENOUGH_SERVERS = 23786;
1714
}

src/Exception/PhpLockException.php

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/Exception/TimeoutException.php

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)