function Lock::acquire
Overrides LockInterface::acquire
1 call to Lock::acquire()
- Lock::acquireRead in vendor/
symfony/ lock/ Lock.php - Acquires the lock for reading. If the lock is acquired by someone else in write mode, the parameter `blocking` determines whether or not the call should block until the release of the lock.
File
-
vendor/
symfony/ lock/ Lock.php, line 67
Class
- Lock
- Lock is the default implementation of the LockInterface.
Namespace
Symfony\Component\LockCode
public function acquire(bool $blocking = false) : bool {
$this->key
->resetLifetime();
try {
if ($blocking) {
if (!$this->store instanceof BlockingStoreInterface) {
while (true) {
try {
$this->store
->save($this->key);
break;
} catch (LockConflictedException) {
usleep((100 + random_int(-10, 10)) * 1000);
}
}
}
else {
$this->store
->waitAndSave($this->key);
}
}
else {
$this->store
->save($this->key);
}
$this->dirty = true;
$this->logger?->debug('Successfully acquired the "{resource}" lock.', [
'resource' => $this->key,
]);
if ($this->ttl) {
$this->refresh();
}
if ($this->key
->isExpired()) {
try {
$this->release();
} catch (\Exception) {
// swallow exception to not hide the original issue
}
throw new LockExpiredException(\sprintf('Failed to store the "%s" lock.', $this->key));
}
return true;
} catch (LockConflictedException $e) {
$this->dirty = false;
$this->logger?->info('Failed to acquire the "{resource}" lock. Someone else already acquired the lock.', [
'resource' => $this->key,
]);
if ($blocking) {
throw $e;
}
return false;
} catch (\Exception $e) {
$this->logger?->notice('Failed to acquire the "{resource}" lock.', [
'resource' => $this->key,
'exception' => $e,
]);
throw new LockAcquiringException(\sprintf('Failed to acquire the "%s" lock.', $this->key), 0, $e);
}
}