Coder Social home page Coder Social logo

mutex's Issues

Release mutex anyway despite autocommit option

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.

The exception thrown, when the mutex acquire fails, is too generic

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

Allow microseconds (float?) as timeout

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

Handling exception while mutex is acquired implicitly

What steps will reproduce the problem?

If exception will be thrown from callback here, mutex will not be released and nobody else will be able to acquire it.

What is the expected result?

Expected exception handling.

$mutex = $this->mutexFactory->createAndAcquire($name, $timeout);

try {
     /** @var mixed */
     return $callback();
} catch (\Throwable $throwable) {
     throw $throwable;
} finally {
   $mutex->release();
}

Additional info

Q A
Version 1.0.?
PHP version any
Operating system doesn't matter

update irc link

What steps will reproduce the problem?

What is the expected result?

What do you get instead?

Additional info

Q A
Version 1.0.?
PHP version
Operating system

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.