Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make retry timeout configurable #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 4 additions & 2 deletions classes/lock/redis_lock_factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down