SymfonyLockSessionLock implements SessionLockInterface
Session lock backed by a symfony/lock store, for sessions shared across machines.
Pairs with Psr16SessionStore: a Redis, database or Zookeeper lock
store reaches every worker that reaches the cache. Each lock expires after
$ttl, so a worker that dies holding one does not block the session for good.
The lease is not refreshed while a handler runs: $ttl must exceed the longest
handler, or another request can take the lock before the first one saved.
A fatal error in a handler (memory limit, max_execution_time) skips the protocol's release and Lock::__destruct(), so the first acquire registers releaseAll() as a shutdown function, which still runs after one.
Tags
Table of Contents
Interfaces
- SessionLockInterface
- Serializes the requests of one session across the workers that serve it.
Constants
- RETRY_DELAY_MICROSECONDS : mixed = 10000
Properties
- $lockFactory : LockFactory
- $locks : array<string, LockInterface>
- $logger : LoggerInterface
- $prefix : string
- $releasesOnShutdown : bool
- $timeout : float
- $ttl : float
Methods
- __construct() : mixed
- acquire() : void
- Blocks until the lock of the session is held, or gives up after the implementation's timeout.
- release() : void
- releaseAll() : void
- Releases every lock this instance still holds; a lock that fails to release is logged and left to expire.
- tryAcquire() : bool
- Takes the lock of the session only if no other request holds it right now, without waiting.
- describe() : string
- The lock component wraps a failing store in its own exception with a generic message, so the cause, e.g.
- lock() : bool
Constants
RETRY_DELAY_MICROSECONDS
private
mixed
RETRY_DELAY_MICROSECONDS
= 10000
Properties
$lockFactory read-only
private
LockFactory
$lockFactory
$locks
private
array<string, LockInterface>
$locks
= []
$logger read-only
private
LoggerInterface
$logger
= new NullLogger()
$prefix read-only
private
string
$prefix
= 'mcp-session-'
$releasesOnShutdown
private
bool
$releasesOnShutdown
= false
$timeout read-only
private
float
$timeout
= 30.0
$ttl read-only
private
float
$ttl
= 300.0
Methods
__construct()
public
__construct(LockFactory $lockFactory[, float $timeout = 30.0 ][, float $ttl = 300.0 ][, string $prefix = 'mcp-session-' ][, LoggerInterface $logger = new NullLogger() ]) : mixed
Parameters
- $lockFactory : LockFactory
- $timeout : float = 30.0
-
how long to wait for a lock held by another request, in seconds
- $ttl : float = 300.0
-
how long a held lock lives before the store expires it, in seconds; longer than the longest handler
- $prefix : string = 'mcp-session-'
-
prepended to the session id to form the lock resource name
- $logger : LoggerInterface = new NullLogger()
acquire()
Blocks until the lock of the session is held, or gives up after the implementation's timeout.
public
acquire(Uuid $sessionId) : void
Parameters
- $sessionId : Uuid
release()
public
release(Uuid $sessionId) : void
Parameters
- $sessionId : Uuid
releaseAll()
Releases every lock this instance still holds; a lock that fails to release is logged and left to expire.
public
releaseAll() : void
tryAcquire()
Takes the lock of the session only if no other request holds it right now, without waiting.
public
tryAcquire(Uuid $sessionId) : bool
Parameters
- $sessionId : Uuid
Return values
bool —false when another request holds the lock
describe()
The lock component wraps a failing store in its own exception with a generic message, so the cause, e.g.
private
static describe(Throwable $e) : string
a refused connection, is added from the innermost exception.
Parameters
- $e : Throwable
Return values
stringlock()
private
lock(Uuid $sessionId, float $timeout) : bool
Parameters
- $sessionId : Uuid
- $timeout : float
Return values
bool —false when another request still held the lock after $timeout seconds