php-lock / lock

Lock library to provide serialized execution of PHP code.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SpinlockMutex unlock method

TheLevti opened this issue · comments

Shouldn't the unlock method of SpinlockMutex throw a TimeoutException if the executed time lasted longer than the set timeout instead of an LockReleaseException?

Related code from SpinlockMutex:

protected function unlock()
    {
        $elapsed = microtime(true) - $this->acquired;
        if ($elapsed >= $this->timeout) {
            $message = sprintf(
                "The code executed for %d seconds. But the timeout is %d seconds.",
                $elapsed,
                $this->timeout
            );
            throw new LockReleaseException($message);
        }
        /*
         * Worst case would still be one second before the key expires.
         * This guarantees that we don't delete a wrong key.
         */
        if (!$this->release($this->key)) {
            throw new LockReleaseException("Failed to release the lock.");
        }
    }

If not, can you explain why?

I guess that a dedicated exception would be fine. I wouldn't want to re-use TimeoutException. Do you have a good suggestion for a name?

Ah true. I just saw that it extends LockAcquireException. So its a nogo to reuse it. My problem is that I have to regex the exception message to differentiate between those two cases where this LockReleaseException is thrown.

$elapsed = microtime(true) - $this->acquired;
if ($elapsed >= $this->timeout) {
    $message = sprintf(
        "The code executed for %d seconds. But the timeout is %d seconds.",
        $elapsed,
        $this->timeout
    );
    throw new LockReleaseException($message);
}

So like you suggested, a dedicated exception would be a solution. I think of an exception that extends LockReleaseException and is called something like 'unexpected execution time exception' or so. Anything related to that if statement would be ok.

Cool, I'll try to wrap something up in the next few days.

I was thinking about CodePartiallyExecutedOutsideLockException or something in that way.

If you want, you're also free to create a PR.

ExecutionOutsideLockException is a bit shorter.

Sure, I can do that. Will submit a pull request soon.

Thanks, appreciate it. Would be great if it could have a message like:

The code executed for %d seconds. But the timeout is %d seconds. The last %d seconds were executed outside the lock.

Ok, let me change the message. Did not see you last comment before I started working on it.