Skip to content

Commit

Permalink
feat: add config for lock timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Feb 24, 2024
1 parent 80c2d46 commit 5fd0893
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
25 changes: 25 additions & 0 deletions app/Config/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,29 @@ class Session extends BaseConfig
* DB Group for the database session.
*/
public ?string $DBGroup = null;

/**
* --------------------------------------------------------------------------
* Lock Retry Interval (microseconds)
* --------------------------------------------------------------------------
*
* This is used for RedisHandler.
*
* Time (microseconds) to wait if lock cannot be acquired.
* The default is 100,000 microseconds (= 0.1 seconds).
*/
public int $lockRetryInterval = 100_000;

/**
* --------------------------------------------------------------------------
* Lock Max Retries
* --------------------------------------------------------------------------
*
* This is used for RedisHandler.
*
* Maximum number of lock acquisition attempts.
* The default is 300 times. That is lock timeout is about 30 (0.1 * 300)
* seconds.
*/
public int $lockMaxRetries = 300;
}
18 changes: 16 additions & 2 deletions system/Session/Handlers/RedisHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ class RedisHandler extends BaseHandler
*/
protected $sessionExpiration = 7200;

/**
* Time (microseconds) to wait if lock cannot be acquired.
*/
private int $lockRetryInterval = 100_000;

/**
* Maximum number of lock acquisition attempts.
*/
private int $lockMaxRetries = 300;

/**
* @param string $ipAddress User's IP address
*
Expand All @@ -76,6 +86,7 @@ public function __construct(SessionConfig $config, string $ipAddress)
$this->sessionExpiration = ($config->expiration === 0)
? (int) ini_get('session.gc_maxlifetime')
: $config->expiration;

// Add sessionCookieName for multiple session cookies.
$this->keyPrefix .= $config->cookieName . ':';

Expand All @@ -84,6 +95,9 @@ public function __construct(SessionConfig $config, string $ipAddress)
if ($this->matchIP === true) {
$this->keyPrefix .= $this->ipAddress . ':';
}

$this->lockRetryInterval = $config->lockWait ?? $this->lockRetryInterval;
$this->lockMaxRetries = $config->lockAttempts ?? $this->lockMaxRetries;
}

protected function setSavePath(): void
Expand Down Expand Up @@ -359,14 +373,14 @@ protected function lockSession(string $sessionID): bool
);

if (! $result) {
usleep(100000);
usleep($this->lockRetryInterval);

continue;
}

$this->lockKey = $lockKey;
break;
} while (++$attempt < 300);
} while (++$attempt < $this->lockMaxRetries);

if ($attempt === 300) {
$this->logger->error(
Expand Down

0 comments on commit 5fd0893

Please sign in to comment.