Skip to content

Commit

Permalink
Fix various typos and small code style changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Willem Stuursma committed Jul 7, 2018
1 parent ff25973 commit 96cd2d8
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 22 deletions.
4 changes: 2 additions & 2 deletions classes/mutex/CASMutex.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ public function __construct($timeout = 3)
}

/**
* Notifies the Mutex about a successfull CAS operation.
* Notifies the Mutex about a successful CAS operation.
*/
public function notify()
{
$this->loop->end();
}

/**
* Repeats executing a code until a compare-and-swap operation was succesful.
* Repeats executing a code until a compare-and-swap operation was successful.
*
* The code has to be designed in a way that it can be repeated without any
* side effects. When the CAS operation was successful it should notify
Expand Down
6 changes: 3 additions & 3 deletions classes/mutex/FlockMutex.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class FlockMutex extends LockMutex
* Sets the file handle.
*
* @param resource $fileHandle The file handle.
* @throws \InvalidArgumentException The file handle is not a valid resource.
* @param int $timeout
*/
public function __construct($fileHandle, $timeout = self::INFINITE_TIMEOUT)
{
Expand Down Expand Up @@ -131,8 +131,8 @@ private function lockBusy()
*/
private function acquireNonBlockingLock()
{
if (!flock($this->fileHandle, LOCK_EX | LOCK_NB, $wouldblock)) {
if ($wouldblock) {
if (!flock($this->fileHandle, LOCK_EX | LOCK_NB, $wouldBlock)) {
if ($wouldBlock) {
/*
* Another process holds the lock.
*/
Expand Down
2 changes: 2 additions & 0 deletions classes/mutex/Mutex.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace malkusch\lock\mutex;

use malkusch\lock\exception\ExecutionOutsideLockException;
use malkusch\lock\exception\LockAcquireException;
use malkusch\lock\exception\LockReleaseException;
use malkusch\lock\util\DoubleCheckedLocking;
Expand Down Expand Up @@ -32,6 +33,7 @@ abstract class Mutex
* @throws \Exception The execution block threw an exception.
* @throws LockAcquireException The mutex could not be acquired, no further side effects.
* @throws LockReleaseException The mutex could not be released, the code was already executed.
* @throws ExecutionOutsideLockException Some code was executed outside of the lock.
*/
abstract public function synchronized(callable $code);

Expand Down
30 changes: 15 additions & 15 deletions classes/mutex/RedisMutex.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,22 +106,22 @@ protected function acquire($key, $expire)
if ($isAcquired) {
// 4.
return true;
} else {
// 5.
$this->release($key);

// In addition to RedLock it's an exception if too many servers fail.
if (!$this->isMajority(count($this->redisAPIs) - $errored)) {
assert(!is_null($exception)); // The last exception for some context.
throw new LockAcquireException(
"It's not possible to acquire a lock because at least half of the Redis server are not available.",
LockAcquireException::REDIS_NOT_ENOUGH_SERVERS,
$exception
);
}
}

return false;
// 5.
$this->release($key);

// In addition to RedLock it's an exception if too many servers fail.
if (!$this->isMajority(count($this->redisAPIs) - $errored)) {
assert(!is_null($exception)); // The last exception for some context.
throw new LockAcquireException(
"It's not possible to acquire a lock because at least half of the Redis server are not available.",
LockAcquireException::REDIS_NOT_ENOUGH_SERVERS,
$exception
);
}

return false;
}

/**
Expand Down Expand Up @@ -193,7 +193,7 @@ abstract protected function add($redisAPI, $key, $value, $expire);
/**
* @param mixed $redisAPI The connected Redis API.
* @param string $script The Lua script.
* @param int $numkeys The number of arguments that represent Redis key names.
* @param int $numkeys The number of values in $arguments that represent Redis key names.
* @param array $arguments Keys and values.
*
* @return mixed The script result, or false if executing failed.
Expand Down
5 changes: 3 additions & 2 deletions classes/util/Loop.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public function end()
public function execute(callable $code)
{
$this->looping = true;

$minWait = 100; // microseconds
$deadline = microtime(true) + $this->timeout; // At this time, the lock will time out.
$result = null;
Expand All @@ -78,8 +79,8 @@ public function execute(callable $code)
break;
}

$min = (int) $minWait * 1.5 ** $i;
$max = $min * 2;
$min = (int) $minWait * 1.5 ** $i;
$max = $min * 2;

/*
* Calculate max time remaining, don't sleep any longer than that.
Expand Down

0 comments on commit 96cd2d8

Please sign in to comment.