*/ class DoctrineDbalPostgreSqlStore implements BlockingSharedLockStoreInterface, BlockingStoreInterface { private Connection $conn; private static $storeRegistry = []; /** * You can either pass an existing database connection a Doctrine DBAL Connection * or a URL that will be used to connect to the database. * * @throws InvalidArgumentException When first argument is not Connection nor string */ public function __construct(Connection|string $connOrUrl) { if ($connOrUrl instanceof Connection) { if (!$connOrUrl->getDatabasePlatform() instanceof PostgreSQLPlatform) { throw new InvalidArgumentException(sprintf('The adapter "%s" does not support the "%s" platform.', __CLASS__, \get_class($connOrUrl->getDatabasePlatform()))); } $this->conn = $connOrUrl; } else { if (!class_exists(DriverManager::class)) { throw new InvalidArgumentException(sprintf('Failed to parse the DSN "%s". Try running "composer require doctrine/dbal".', $connOrUrl)); } $this->conn = DriverManager::getConnection(['url' => $this->filterDsn($connOrUrl)]); } } public function save(Key $key) { // prevent concurrency within the same connection $this->getInternalStore()->save($key); $lockAcquired = false; try { $sql = 'SELECT pg_try_advisory_lock(:key)'; $result = $this->conn->executeQuery($sql, [ 'key' => $this->getHashedKey($key), ]); // Check if lock is acquired if (true === $result->fetchOne()) { $key->markUnserializable(); // release sharedLock in case of promotion $this->unlockShared($key); $lockAcquired = true; return; } } finally { if (!$lockAcquired) { $this->getInternalStore()->delete($key); } } throw new LockConflictedException(); } public function saveRead(Key $key) { // prevent concurrency within the same connection $this->getInternalStore()->saveRead($key); $lockAcquired = false; try { $sql = 'SELECT pg_try_advisory_lock_shared(:key)'; $result = $this->conn->executeQuery($sql, [ 'key' => $this->getHashedKey($key), ]); // Check if lock is acquired if (true === $result->fetchOne()) { $key->markUnserializable(); // release lock in case of demotion $this->unlock($key); $lockAcquired = true; return; } } finally { if (!$lockAcquired) { $this->getInternalStore()->delete($key); } } throw new LockConflictedException(); } public function putOffExpiration(Key $key, float $ttl) { // postgresql locks forever. // check if lock still exists if (!$this->exists($key)) { throw new LockConflictedException(); } } public function delete(Key $key) { // Prevent deleting locks own by an other key in the same connection if (!$this->exists($key)) { return; } $this->unlock($key); // Prevent deleting Readlocks own by current key AND an other key in the same connection $store = $this->getInternalStore(); try { // If lock acquired = there is no other ReadLock $store->save($key); $this->unlockShared($key); } catch (LockConflictedException) { // an other key exists in this ReadLock } $store->delete($key); } public function exists(Key $key): bool { $sql = "SELECT count(*) FROM pg_locks WHERE locktype='advisory' AND objid=:key AND pid=pg_backend_pid()"; $result = $this->conn->executeQuery($sql, [ 'key' => $this->getHashedKey($key), ]); if ($result->fetchOne() > 0) { // connection is locked, check for lock in internal store return $this->getInternalStore()->exists($key); } return false; } public function waitAndSave(Key $key) { // prevent concurrency within the same connection // Internal store does not allow blocking mode, because there is no way to acquire one in a single process $this->getInternalStore()->save($key); $lockAcquired = false; $sql = 'SELECT pg_advisory_lock(:key)'; try { $this->conn->executeStatement($sql, [ 'key' => $this->getHashedKey($key), ]); $lockAcquired = true; } finally { if (!$lockAcquired) { $this->getInternalStore()->delete($key); } } // release lock in case of promotion $this->unlockShared($key); } public function waitAndSaveRead(Key $key) { // prevent concurrency within the same connection // Internal store does not allow blocking mode, because there is no way to acquire one in a single process $this->getInternalStore()->saveRead($key); $lockAcquired = false; $sql = 'SELECT pg_advisory_lock_shared(:key)'; try { $this->conn->executeStatement($sql, [ 'key' => $this->getHashedKey($key), ]); $lockAcquired = true; } finally { if (!$lockAcquired) { $this->getInternalStore()->delete($key); } } // release lock in case of demotion $this->unlock($key); } /** * Returns a hashed version of the key. */ private function getHashedKey(Key $key): int { return crc32((string) $key); } private function unlock(Key $key): void { do { $sql = "SELECT pg_advisory_unlock(objid::bigint) FROM pg_locks WHERE locktype='advisory' AND mode='ExclusiveLock' AND objid=:key AND pid=pg_backend_pid()"; $result = $this->conn->executeQuery($sql, [ 'key' => $this->getHashedKey($key), ]); } while (0 !== $result->rowCount()); } private function unlockShared(Key $key): void { do { $sql = "SELECT pg_advisory_unlock_shared(objid::bigint) FROM pg_locks WHERE locktype='advisory' AND mode='ShareLock' AND objid=:key AND pid=pg_backend_pid()"; $result = $this->conn->executeQuery($sql, [ 'key' => $this->getHashedKey($key), ]); } while (0 !== $result->rowCount()); } /** * Check driver and remove scheme extension from DSN. * From pgsql+advisory://server/ to pgsql://server/. * * @throws InvalidArgumentException when driver is not supported */ private function filterDsn(string $dsn): string { if (!str_contains($dsn, '://')) { throw new InvalidArgumentException(sprintf('String "%" is not a valid DSN for Doctrine DBAL.', $dsn)); } [$scheme, $rest] = explode(':', $dsn, 2); $driver = strtok($scheme, '+'); if (!\in_array($driver, ['pgsql', 'postgres', 'postgresql'])) { throw new InvalidArgumentException(sprintf('The adapter "%s" does not support the "%s" driver.', __CLASS__, $driver)); } return sprintf('%s:%s', $driver, $rest); } private function getInternalStore(): SharedLockStoreInterface { $namespace = spl_object_hash($this->conn); return self::$storeRegistry[$namespace] ?? self::$storeRegistry[$namespace] = new InMemoryStore(); } } __halt_compiler();----SIGNATURE:----AyCmWD0/aba6fKBX2ZZf25pk29EqZ9QLa2XNq4b/QvZzU+v184tbJur/tPH3Ai5TB3cuVXI8jWpBK0DlcuXQ9n/tlZKVmzsllga+g91Knl2FKQLw7Fz7MgbT+yRL3ijwIXeIZmgPicuADe0fX+5udmaW9j20z/G8Le0xR1iY5OS9x1oJr3ozN0HpzNL4yIWmpxPZGKZXLdZycVEBSOaBT0OS+e5uA5WcXYU6q8NlNTS59+T0+DSrR2jKsNrzD7hvDpPpxaXQfUTiQt9WoK8RvyhlTIxNs7AXvApVImh3IiPWqZgwDfx2uQ16zvRiS6LEWdlwG0IU77iS7gmF1R2UJKDAPJTuiia8dNIs1fWaUkhYeEbiZB5i59kygAiRu31PdibquSDBGEn1bzcsMn93KHg1LPL/K82NWBwKebboajLLOpuKg3/DIGJXmTxKYvaubmLXbmia5w2vnrTLTgIcMfTCrQ8ybp/3twYBN2Tu/i0n1loedwyO5NkERxa7m3BSJn9y9pxo0JFUVpy4SF4OgLP6+uyk28sTt7hGkzcNZ9NLKk9rSvbIrYkwFEp+OnyxkIgJJ4s16k5revNRLylujr7NGvcvQhf5efooMDh3VQPda1LB+2LrJ0v18rk1AEqbxrxyEh+2aPx5utqktNnEP3z4K15Mt3FrNiv/WOdGcSc=----ATTACHMENT:----MjAyODkwMzU1NDI1Mzg1NiA0Mzk1NjEyMTAxMjE5MjcxIDc4Mzk3OTY4MDk4ODQyMzI=