waitAndSaveRead example

$keyId = uniqid(__METHOD__, true);
        $store1Key = new Key($keyId);

        $store1->save($store1Key);

        // set a low time out then try to wait and save, which will fail         // because the key is already set above.         $pdo->exec('SET statement_timeout = 1');
        $waitSaveError = null;
        try {
            $store2->waitAndSaveRead(new Key($keyId));
        } catch (\PDOException $waitSaveError) {
        }
        $this->assertInstanceOf(\PDOException::class$waitSaveError, 'waitAndSave should have thrown');

        $store1->delete($store1Key);
        $this->assertFalse($store1->exists($store1Key));

        $store2Key = new Key($keyId);
        // since the lock is going to be acquired in read mode and is not exclusive         // this won't every throw a LockConflictedException as it would from         // waitAndSave, but it will hang indefinitely as it waits for postgres
if ($blocking) {
                if (!$this->store instanceof BlockingSharedLockStoreInterface) {
                    while (true) {
                        try {
                            $this->store->saveRead($this->key);
                            break;
                        } catch (LockConflictedException) {
                            usleep((100 + random_int(-10, 10)) * 1000);
                        }
                    }
                } else {
                    $this->store->waitAndSaveRead($this->key);
                }
            } else {
                $this->store->saveRead($this->key);
            }

            $this->dirty = true;
            $this->logger?->debug('Successfully acquired the "{resource}" lock for reading.', ['resource' => $this->key]);

            if ($this->ttl) {
                $this->refresh();
            }

            
$keyId = uniqid(__METHOD__, true);
        $store1Key = new Key($keyId);

        $store1->save($store1Key);

        // set a low time out then try to wait and save, which will fail         // because the key is already set above.         $conn->executeStatement('SET statement_timeout = 1');
        $waitSaveError = null;
        try {
            $store2->waitAndSaveRead(new Key($keyId));
        } catch (DBALException $waitSaveError) {
        }
        $this->assertInstanceOf(DBALException::class$waitSaveError, 'waitAndSaveRead should have thrown');

        $store1->delete($store1Key);
        $this->assertFalse($store1->exists($store1Key));

        $store2Key = new Key($keyId);
        // since the lock is going to be acquired in read mode and is not exclusive         // this won't every throw a LockConflictedException as it would from         // waitAndSave, but it will hang indefinitely as it waits for postgres
Home | Imprint | This part of the site doesn't use cookies.