diff --git a/README.md b/README.md index efe07e4..3998b5d 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,8 @@ Set: - It can also be `hostname:port` if you want to use other port different than `6379` (Default) * `$CFG->lock_factory` to `'\\local_redislock\\lock\\redis_lock_factory'` in your config file. * `$CFG->local_redislock_auth` with your Redis server's password string. +* `$CFG->local_redislock_retry_timeout` the time in ms to sleep between attempts to claim the lock (Default: 750) +* `$CFG->local_redislock_retry_jitter` amount of random jitter in ms to add/subtract from `$CFG->local_redislock_retry_timeout`. (Default: 250) ## Flags diff --git a/classes/lock/redis_lock_factory.php b/classes/lock/redis_lock_factory.php index c4465a8..a41877a 100644 --- a/classes/lock/redis_lock_factory.php +++ b/classes/lock/redis_lock_factory.php @@ -94,6 +94,8 @@ public function __construct($type, \Redis $redis = null, $logging = null) { $this->redisserver = $CFG->local_redislock_redis_server ?? null; $this->shareconnection = empty($CFG->local_redislock_disable_shared_connection); $this->auth = $CFG->local_redislock_redis_auth ?? null; + $this->lockretry = ($CFG->local_redislock_retry_timeout ?? 750) * 1000; + $this->lockretry_jitter = ($CFG->local_redislock_retry_jitter ?? 250) * 1000; if (is_null($redis)) { shared_redis_connection::get_instance()->add_factory(); $redis = $this->bootstrap_redis(); @@ -202,7 +204,7 @@ public function get_lock($resource, $timeout, $maxlifetime = 86400) { } if (!$locked && $timeout !== 0) { - usleep(rand(500000, 1000000)); // Sleep between 0.5 and 1 second. + usleep(rand($this->lockretry - $this->lockretry_jitter, $this->lockretry + $this->lockretry_jitter)); } } while (!$locked && $now < $giveuptime); @@ -262,7 +264,7 @@ public function release_lock(lock $lock) { $this->redis = $this->bootstrap_redis(); // Sleep the loop for a bit so we don't spam connections. - usleep(rand(500000, 1000000)); + usleep(rand($this->lockretry - $this->lockretry_jitter, $this->lockretry + $this->lockretry_jitter)); } } while ($exception);