r/PHP•
malkusch

Request for feedback on malkusch/lock-0.3

Last time I got really inspiring and constructive feedback. So after incorporating those suggestions, I'd like to ask for a round two: Here is malkusch/lock-0.3. These are the changes:

  • RedLock implementation added
  • empirical contention test, which helped a lot to find issues
  • The TransactionalMutex replays transactions on any \PDOException

I'm happy to hear some feedback.

Update: This is a locking library to execute critical code exclusively protected by a mutex. E.g. consider the following code:

$balance = $bankAccount->getBalance();
$balance -= $amount;
$bankAccount->setBalance($balance);

In a concurrent situation there's a race condition and you might easily see lost updates. This is prevented by executing it in a mutex:

$mutex->synchronized(function () use ($bankAccount, $amount) {
    $balance = $bankAccount->getBalance();
    $balance -= $amount;
    $bankAccount->setBalance($balance);
});

Posted Jul 31, 2015, 11:31 PM UTC · edited

26
Loading comments…