Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ jobs:
php: ['7.2', '7.4']

services:
postgres:
image: postgres:9.6-alpine
ports:
- 5432:5432
env:
POSTGRES_PASSWORD: 'password'
redis:
image: redis:6.0.0
ports:
Expand Down Expand Up @@ -144,6 +150,7 @@ jobs:
MEMCACHED_HOST: localhost
MONGODB_HOST: localhost
KAFKA_BROKER: localhost:9092
POSTGRES_HOST: localhost

- name: Run HTTP push tests
if: matrix.php == '7.4'
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/Lock/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ CHANGELOG
* added `NoLock`
* deprecated `NotSupportedException`, it shouldn't be thrown anymore.
* deprecated `RetryTillSaveStore`, logic has been moved in `Lock` and is not needed anymore.
* added `InMemoryStore`
* added `PostgreSqlStore`

5.1.0
-----
Expand Down
114 changes: 114 additions & 0 deletions src/Symfony/Component/Lock/Store/InMemoryStore.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Lock\Store;

use Symfony\Component\Lock\Exception\LockConflictedException;
use Symfony\Component\Lock\Key;
use Symfony\Component\Lock\SharedLockStoreInterface;

/**
* InMemoryStore is a PersistingStoreInterface implementation using
* php-array to manage locks.
*
* @author Jérémy Derussé <jeremy@derusse.com>
*/
class InMemoryStore implements SharedLockStoreInterface
{
private $locks = [];
private $readLocks = [];

public function save(Key $key)
{
$hashKey = (string) $key;
$token = $this->getUniqueToken($key);
if (isset($this->locks[$hashKey])) {
// already acquired
if ($this->locks[$hashKey] === $token) {
return;
}

throw new LockConflictedException();
}

// check for promotion
if (isset($this->readLocks[$hashKey][$token]) && 1 === \count($this->readLocks[$hashKey])) {
unset($this->readLocks[$hashKey]);
$this->locks[$hashKey] = $token;

return;
}

if (\count($this->readLocks[$hashKey] ?? []) > 0) {
throw new LockConflictedException();
}

$this->locks[$hashKey] = $token;
}

public function saveRead(Key $key)
{
$hashKey = (string) $key;
$token = $this->getUniqueToken($key);

// check if lock is already acquired in read mode
if (isset($this->readLocks[$hashKey])) {
$this->readLocks[$hashKey][$token] = true;

return;
}

// check for demotion
if (isset($this->locks[$hashKey])) {
if ($this->locks[$hashKey] !== $token) {
throw new LockConflictedException();
}

unset($this->locks[$hashKey]);
}

$this->readLocks[$hashKey][$token] = true;
}

public function putOffExpiration(Key $key, float $ttl)
{
// do nothing, memory locks forever.
}

public function delete(Key $key)
{
$hashKey = (string) $key;
$token = $this->getUniqueToken($key);

unset($this->readLocks[$hashKey][$token]);
if (($this->locks[$hashKey] ?? null) === $token) {
unset($this->locks[$hashKey]);
}
}

public function exists(Key $key)
{
$hashKey = (string) $key;
$token = $this->getUniqueToken($key);

return isset($this->readLocks[$hashKey][$token]) || ($this->locks[$hashKey] ?? null) === $token;
}

private function getUniqueToken(Key $key): string
{
if (!$key->hasState(__CLASS__)) {
$token = base64_encode(random_bytes(32));
$key->setState(__CLASS__, $token);
}

return $key->getState(__CLASS__);
}
}
1 change: 0 additions & 1 deletion src/Symfony/Component/Lock/Store/PdoStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ class PdoStore implements PersistingStoreInterface
*
* @throws InvalidArgumentException When first argument is not PDO nor Connection nor string
* @throws InvalidArgumentException When PDO error mode is not PDO::ERRMODE_EXCEPTION
* @throws InvalidArgumentException When namespace contains invalid characters
* @throws InvalidArgumentException When the initial ttl is not valid
*/
public function __construct($connOrDsn, array $options = [], float $gcProbability = 0.01, int $initialTtl = 300)
Expand Down
Loading