Same filename in this branch
- 10 core/lib/Drupal/Core/Lock/DatabaseLockBackend.php
- 10 core/lib/Drupal/Core/ProxyClass/Lock/DatabaseLockBackend.php
Same filename and directory in other branches
- 8.9.x core/lib/Drupal/Core/Lock/DatabaseLockBackend.php
- 9 core/lib/Drupal/Core/Lock/DatabaseLockBackend.php
Namespace
Drupal\Core\Lock
File
core/lib/Drupal/Core/Lock/DatabaseLockBackend.php
View source
<?php
namespace Drupal\Core\Lock;
use Drupal\Component\Utility\Crypt;
use Drupal\Core\Database\Connection;
use Drupal\Core\Database\DatabaseException;
use Drupal\Core\Database\IntegrityConstraintViolationException;
class DatabaseLockBackend extends LockBackendAbstract {
const TABLE_NAME = 'semaphore';
protected $database;
public function __construct(Connection $database) {
drupal_register_shutdown_function([
$this,
'releaseAll',
]);
$this->database = $database;
}
public function acquire($name, $timeout = 30.0) {
$name = $this
->normalizeName($name);
$timeout = max($timeout, 0.001);
$expire = microtime(TRUE) + $timeout;
if (isset($this->locks[$name])) {
$success = (bool) $this->database
->update('semaphore')
->fields([
'expire' => $expire,
])
->condition('name', $name)
->condition('value', $this
->getLockId())
->execute();
if (!$success) {
unset($this->locks[$name]);
}
return $success;
}
else {
$retry = FALSE;
do {
try {
$this->database
->insert('semaphore')
->fields([
'name' => $name,
'value' => $this
->getLockId(),
'expire' => $expire,
])
->execute();
$this->locks[$name] = TRUE;
$retry = FALSE;
} catch (IntegrityConstraintViolationException $e) {
$retry = $retry ? FALSE : $this
->lockMayBeAvailable($name);
} catch (\Exception $e) {
if ($this
->ensureTableExists()) {
$retry = !$retry;
}
else {
throw $e;
}
}
} while ($retry);
}
return isset($this->locks[$name]);
}
public function lockMayBeAvailable($name) {
$name = $this
->normalizeName($name);
try {
$lock = $this->database
->query('SELECT [expire], [value] FROM {semaphore} WHERE [name] = :name', [
':name' => $name,
])
->fetchAssoc();
} catch (\Exception $e) {
$this
->catchException($e);
$lock = FALSE;
}
if (!$lock) {
return TRUE;
}
$expire = (double) $lock['expire'];
$now = microtime(TRUE);
if ($now > $expire) {
return (bool) $this->database
->delete('semaphore')
->condition('name', $name)
->condition('value', $lock['value'])
->condition('expire', 0.0001 + $expire, '<=')
->execute();
}
return FALSE;
}
public function release($name) {
$name = $this
->normalizeName($name);
unset($this->locks[$name]);
try {
$this->database
->delete('semaphore')
->condition('name', $name)
->condition('value', $this
->getLockId())
->execute();
} catch (\Exception $e) {
$this
->catchException($e);
}
}
public function releaseAll($lock_id = NULL) {
if (!empty($this->locks)) {
$this->locks = [];
if (empty($lock_id)) {
$lock_id = $this
->getLockId();
}
$this->database
->delete('semaphore')
->condition('value', $lock_id)
->execute();
}
}
protected function ensureTableExists() {
try {
$database_schema = $this->database
->schema();
$schema_definition = $this
->schemaDefinition();
$database_schema
->createTable(static::TABLE_NAME, $schema_definition);
} catch (DatabaseException $e) {
} catch (\Exception $e) {
return FALSE;
}
return TRUE;
}
protected function catchException(\Exception $e) {
if ($this->database
->schema()
->tableExists(static::TABLE_NAME)) {
throw $e;
}
}
protected function normalizeName($name) {
$name_is_ascii = mb_check_encoding($name, 'ASCII');
if (strlen($name) <= 255 && $name_is_ascii) {
return $name;
}
$hash = Crypt::hashBase64($name);
if (!$name_is_ascii) {
return $hash;
}
return substr($name, 0, 255 - strlen($hash)) . $hash;
}
public function schemaDefinition() {
return [
'description' => 'Table for holding semaphores, locks, flags, etc. that cannot be stored as state since they must not be cached.',
'fields' => [
'name' => [
'description' => 'Primary Key: Unique name.',
'type' => 'varchar_ascii',
'length' => 255,
'not null' => TRUE,
'default' => '',
],
'value' => [
'description' => 'A value for the semaphore.',
'type' => 'varchar_ascii',
'length' => 255,
'not null' => TRUE,
'default' => '',
],
'expire' => [
'description' => 'A Unix timestamp with microseconds indicating when the semaphore should expire.',
'type' => 'float',
'size' => 'big',
'not null' => TRUE,
],
],
'indexes' => [
'value' => [
'value',
],
'expire' => [
'expire',
],
],
'primary key' => [
'name',
],
];
}
}
Classes