Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release mutex anyway despite autocommit option #35

Closed
kafkiansky opened this issue Aug 5, 2021 · 2 comments
Closed

Release mutex anyway despite autocommit option #35

kafkiansky opened this issue Aug 5, 2021 · 2 comments

Comments

@kafkiansky
Copy link

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.

@samdark
Copy link
Member

samdark commented Aug 7, 2021

The flag was removed. Having abstract class instead of an interface has its drawbacks.

@kafkiansky
Copy link
Author

The flag was removed. Having abstract class instead of an interface has its drawbacks.

You don't need an interface or an abstract class, but you go your own way as always.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants