function _apdqc_lock_acquire in Asynchronous Prefetch Database Query Cache 7
Same name in this branch
- 7 apdqc.lock.apc.inc \_apdqc_lock_acquire()
- 7 apdqc.lock.db.inc \_apdqc_lock_acquire()
- 7 apdqc.lock.memcache_storage.inc \_apdqc_lock_acquire()
- 7 apdqc.lock.memcache.inc \_apdqc_lock_acquire()
- 7 apdqc.lock.redis.inc \_apdqc_lock_acquire()
Acquire (or renew) a lock, but do not block if it fails.
Parameters
string $name: The name of the lock. Limit of name's length is 255 characters.
float $timeout: A number of seconds (float) before the lock expires (minimum of 0.001).
Return value
bool TRUE if the lock was acquired, FALSE if it failed.
Related topics
1 call to _apdqc_lock_acquire()
- apdqc.lock.inc in ./
apdqc.lock.inc - A database-mediated implementation of a locking mechanism.
1 string reference to '_apdqc_lock_acquire'
- apdqc.lock.inc in ./
apdqc.lock.inc - A database-mediated implementation of a locking mechanism.
File
- ./
apdqc.lock.db.inc, line 107 - A database-mediated implementation of a locking mechanism.
Code
function _apdqc_lock_acquire($name, $timeout = 30.0) {
global $locks;
// Insure that the timeout is at least 1 ms.
$timeout = max($timeout, 0.001);
$expire = microtime(TRUE) + $timeout;
if (isset($locks[$name])) {
// Build query.
$query = \Database::getConnection()
->prefixTables("\n UPDATE {" . db_escape_table('semaphore') . "}\n SET expire = " . (double) $expire . "\n WHERE name = '" . apdqc_escape_string($name) . "'\n AND value = '" . apdqc_escape_string(_lock_id()) . "'\n ");
// Run Query.
$results = apdqc_query(array(
'semaphore',
), array(
$name,
), $query, array(
'get_affected_rows' => TRUE,
));
if (!is_string($results) || $results !== 'NO DB') {
$success = (bool) $results;
}
else {
// Try to extend the expiration of a lock we already acquired.
$success = (bool) db_update('semaphore')
->fields(array(
'expire' => $expire,
))
->condition('name', $name)
->condition('value', _lock_id())
->execute();
}
if (!$success) {
// The lock was broken.
unset($locks[$name]);
}
return $success;
}
else {
// Optimistically try to acquire the lock, then retry once if it fails.
// The first time through the loop cannot be a retry.
$retry = FALSE;
// We always want to do this code at least once.
// Build query.
$query = \Database::getConnection()
->prefixTables("\n INSERT INTO {" . db_escape_table('semaphore') . "}\n (name, value, expire)\n VALUES ('" . apdqc_escape_string($name) . "', '" . apdqc_escape_string(_lock_id()) . "', " . (double) $expire . ")\n ");
do {
// Run Query.
$results = apdqc_query(array(
'semaphore',
), array(
$name,
), $query, array(
'get_affected_rows' => TRUE,
));
if (!is_string($results) || $results !== 'NO DB') {
if ($results == 1) {
// We track all acquired locks in the global variable.
$locks[$name] = TRUE;
// We never need to try again.
$retry = FALSE;
}
else {
// If this is our first pass through the loop, then $retry is FALSE.
// In this case, the insert must have failed meaning some other
// request acquired the lock but did not release it. We decide
// whether to retry by checking lock_may_be_available() Since this
// will break the lock in case it is expired.
$retry = $retry ? FALSE : lock_may_be_available($name);
}
}
else {
try {
db_insert('semaphore')
->fields(array(
'name' => $name,
'value' => _lock_id(),
'expire' => $expire,
))
->execute();
// We track all acquired locks in the global variable.
$locks[$name] = TRUE;
// We never need to try again.
$retry = FALSE;
} catch (PDOException $e) {
// Suppress the error. If this is our first pass through the loop,
// then $retry is FALSE. In this case, the insert must have failed
// meaning some other request acquired the lock but did not release
// it. We decide whether to retry by checking
// lock_may_be_available() Since this will break the lock in case it
// is expired.
$retry = $retry ? FALSE : lock_may_be_available($name);
}
}
// We only retry in case the first attempt failed, but we then broke
// an expired lock.
} while ($retry);
}
return isset($locks[$name]);
}