You are here

public function MongodbLock::lockMayBeAvailable in MongoDB 8

Checks if a lock is available for acquiring.

Parameters

string $name: Lock to acquire.

Return value

bool

Overrides LockBackendInterface::lockMayBeAvailable

1 call to MongodbLock::lockMayBeAvailable()
MongodbLock::acquire in src/MongodbLock.php
Acquires a lock.

File

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

Class

MongodbLock

Namespace

Drupal\mongodb

Code

public function lockMayBeAvailable($name) {
  $lock = $this
    ->mongoCollection()
    ->findOne([
    '_id.name' => $name,
  ]);
  if (!$lock) {
    return TRUE;
  }
  $expire = (double) $lock['expire'];
  $now = microtime(TRUE);
  if ($now > $expire) {

    // We check two conditions to prevent a race condition where another
    // request acquired the lock and set a new expire time. We add a small
    // number to $expire to avoid errors with float to string conversion.
    return (bool) $this
      ->mongoCollection()
      ->remove([
      '_id.name' => $name,
      'value' => $lock['value'],
      'expire' => [
        '$lte' => 0.0001 + $expire,
      ],
    ]);
  }
  return FALSE;
}