yiisoft / mutex Goto Github PK
View Code? Open in Web Editor NEWMutex lock implementation
Home Page: https://www.yiiframework.com/
License: BSD 3-Clause "New" or "Revised" License
Mutex lock implementation
Home Page: https://www.yiiframework.com/
License: BSD 3-Clause "New" or "Revised" License
Now every mutex driver must release mutex manually if option autocommit
set on true.
This decision not only adds difficulties in implementing the new driver, but also does not eliminate problems when managing the mutex.
Imagine that you have some acquired mutex:
$fileMutexFactory = new FileMutexFactory(__DIR__ . '/var/mutexes', false);
$mutex = $this->mutexes->create('some_mutex');
// some actions
$this->doSomething(); // But there will be exception
$mutex->release();
Such mutex will not be released because autocommit
turn off.
I think, mutex releaser must be required and do not rely on the user's actions. Every mutex implementation must release mutex anyway in they destructor's.
The following code will guarantees that the mutex will be released anyway on the script shutdown, and it also does not require drivers to implement it themselves.
abstract class Mutex
{
/**
* Acquires a lock.
*
* @param int $timeout Time (in seconds) to wait for lock to be released. Defaults to zero meaning that method
* will return false immediately in case lock was already acquired.
*/
abstract public function acquire(int $timeout = 0): bool;
/**
* Releases a lock.
*/
abstract public function release(): void;
abstract public function released(): bool;
final public function __destruct()
{
if (!$this->released()) {
$this->release();
}
}
}
After that, we can delete the call to the register_shutdown_function
function and the `autocommit' option.
When the Yiisoft\Mutex\MutexFactory::createAndAcquire()
method fails to acquire lock the \RuntimeException
is thrown.
Lets say you have code:
try {
$this->synchronizer->execute(
'myMutex',
function () {
// do some synchronized logic
},
30
);
} catch (\RuntimeException $ex) {
// handle exception
}
The issue with this code is that \RuntimeException
is too generic and can easily be thrown from inside the synchronized logic too. You can't easily setup the try-catch
block to catch only exception caused by failed attempt to acquire mutex.
The use case when you would need to do that might be when you want to show some output to user when the mutex is not acquired. For example something like "The work is in progress. Please try again later."
It would be much easier to do if the exception thrown is something like this
class MutexException extends RuntimeException
Good day. It not nessesery, but how about add microseconds as timeout - as additional argument or change current type to float? For example (my case) it can be used for some external API with limited requests per second. Thanks
If exception will be thrown from callback here, mutex will not be released and nobody else will be able to acquire it.
Expected exception handling.
$mutex = $this->mutexFactory->createAndAcquire($name, $timeout);
try {
/** @var mixed */
return $callback();
} catch (\Throwable $throwable) {
throw $throwable;
} finally {
$mutex->release();
}
Q | A |
---|---|
Version | 1.0.? |
PHP version | any |
Operating system | doesn't matter |
Q | A |
---|---|
Version | 1.0.? |
PHP version | |
Operating system |
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.