You are here

public function MongodbLock::acquire in MongoDB 8

Acquires a lock.

Parameters

string $name: Lock name. Limit of name's length is 255 characters.

float $timeout = 30.0: (optional) Lock lifetime in seconds.

Return value

bool

Overrides LockBackendInterface::acquire

File

src/MongodbLock.php, line 44
Contains \Drupal\mongodb\MongodbLock.

Class

MongodbLock

Namespace

Drupal\mongodb

Code

public function acquire($name, $timeout = 30.0) {

  // Insure that the timeout is at least 1 ms.
  $timeout = max($timeout, 0.001);
  $expire = microtime(TRUE) + $timeout;
  if (isset($this->locks[$name])) {

    // Try to extend the expiration of a lock we already acquired.
    $result = $this
      ->mongoCollection()
      ->update([
      '_id' => $this
        ->mongoId($name),
    ], [
      'expire' => $expire,
    ]);
    $success = !empty($result['n']);
    if (!$success) {

      // The lock was broken.
      unset($this->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.
    do {
      try {
        $this
          ->mongoCollection()
          ->insert([
          '_id' => $this
            ->mongoId($name),
          'expire' => $expire,
        ]);

        // We track all acquired locks in the global variable.
        $this->locks[$name] = TRUE;

        // We never need to try again.
        $retry = FALSE;
      } catch (\MongoDuplicateKeyException $e) {

        // Suppress the error. If this is our first pass through the loop,
        // then $retry is FALSE. In this case, the insert failed because some
        // other request acquired the lock but did not release it. We decide
        // whether to retry by checking lockMayBeAvailable(). This will clear
        // the offending row from the database table in case it has expired.
        $retry = $retry ? FALSE : $this
          ->lockMayBeAvailable($name);
      }

      // We only retry in case the first attempt failed, but we then broke
      // an expired lock.
    } while ($retry);
  }
  return isset($this->locks[$name]);
}