diff --git a/src/LockSinglon.php b/src/LockSinglon.php new file mode 100644 index 0000000..d732eca --- /dev/null +++ b/src/LockSinglon.php @@ -0,0 +1,30 @@ +servers = $servers; - - $this->retryDelay = $retryDelay; - $this->retryCount = $retryCount; - $this->quorum = min(count($servers), (count($servers) / 2 + 1)); } - public function lock($resource, $ttl) + public function lock($resource, $ttl, $retryDelay = 200, $try_times = 3) { $this->initInstances(); $token = uniqid(); - $retry = $this->retryCount; - do { + while (true) { $n = 0; $startTime = microtime(true) * 1000; @@ -59,13 +52,15 @@ public function lock($resource, $ttl) } } - // Wait a random delay before to retry - $delay = mt_rand(floor($this->retryDelay / 2), $this->retryDelay); - usleep($delay * 1000); + $try_times--; - $retry--; + if ($try_times === 0) + break; - } while ($retry > 0); + // Wait a random delay before to retry + $delay = mt_rand(floor($retryDelay / 2), $retryDelay); + usleep($delay * 1000); + } return false; }