8000 Merge Store interfaces · symfony/symfony@af96dd7 · GitHub
[go: up one dir, main page]

Skip to content

Commit af96dd7

Browse files
committed
Merge Store interfaces
1 parent dbb18b5 commit af96dd7

17 files changed

+147
-155
lines changed

src/Symfony/Component/Lock/Exception/InvalidArgumentException.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
namespace Symfony\Component\Lock\Exception;
1313

1414
/**
15-
* InvalidArgumentException is thrown when a service had been badly initialized.
16-
*
1715
* @author Jérémy Derussé <jeremy@derusse.com>
1816
*/
1917
class InvalidArgumentException extends \InvalidArgumentException implements ExceptionInterface
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Lock\Exception;
13+
14+
/**
15+
* NotSupportedException is thrown when a unsupported method is called.
16+
*
17+
* @author Jérémy Derussé <jeremy@derusse.com>
18+
*/
19+
class NotSupportedException extends \LogicException implements ExceptionInterface
20+
{
21+
}

src/Symfony/Component/Lock/Lock.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
use Symfony\Component\Lock\Exception\LockAcquiringException;
1818
use Symfony\Component\Lock\Exception\LockConflictedException;
1919
use Symfony\Component\Lock\Exception\LockReleasingException;
20-
use Symfony\Component\Lock\Store\BlockingStoreInterface;
21-
use Symfony\Component\Lock\Store\ExpirableStoreInterface;
2220

2321
/**
2422
* Lock is the default implementation of the LockInterface.
@@ -55,10 +53,8 @@ public function acquire($blocking = false)
5553
try {
5654
if (!$blocking) {
5755
$this->store->save($this->key);
58-
} elseif ($this->store instanceof BlockingStoreInterface) {
59-
$this->store->waitAndSave($this->key);
6056
} else {
61-
throw new InvalidArgumentException(sprintf('Unable to acquire a blocking lock on a instance of "%s".', get_class($this->store)));
57+
$this->store->waitAndSave($this->key);
6258
}
6359

6460
$this->logger->info('Lock successfully acquired for "{resource}"', array('resource' => $this->key));
@@ -80,16 +76,12 @@ public function acquire($blocking = false)
8076
*/
8177
public function refresh()
8278
{
83-
if (!$this->store instanceof ExpirableStoreInterface) {
84-
return;
85-
}
86-
8779
if (!$this->ttl) {
8880
throw new InvalidArgumentException('You have to define an expiration duration');
8981
}
9082

9183
try {
92-
$this->store->expire($this->key, $this->ttl);
84+
$this->store->putOffExpiration($this->key, $this->ttl);
9385
$this->logger->info('Expiration defined for "{resource}" lock for "{ttl}" seconds', array('resource' => $this->key, 'ttl' => $this->ttl));
9486
} catch (LockConflictedException $e) {
9587
$this->logger->warning('Failed to define an expiration for the "{resource}" lock, someone else acquired the lock', array('resource' => $this->key));

src/Symfony/Component/Lock/Store/BlockingStoreInterface.php

Lines changed: 0 additions & 33 deletions
This file was deleted.

src/Symfony/Component/Lock/Store/CombinedStore.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Psr\Log\NullLogger;
1616
use Symfony\Component\Lock\Exception\InvalidArgumentException;
1717
use Symfony\Component\Lock\Exception\LockConflictedException;
18+
use Symfony\Component\Lock\Exception\NotSupportedException;
1819
use Symfony\Component\Lock\Key;
1920
use Symfony\Component\Lock\QuorumInterface;
2021
use Symfony\Component\Lock\StoreInterface;
@@ -24,7 +25,7 @@
2425
*
2526
* @author Jérémy Derussé <jeremy@derusse.com>
2627
*/
27-
class CombinedStore extends AbstractStore implements ExpirableStoreInterface
28+
class CombinedStore extends AbstractStore
2829
{
2930
use LoggerAwareTrait;
3031

@@ -86,23 +87,23 @@ public function save(Key $key)
8687
throw new LockConflictedException();
8788
}
8889

90+
public function waitAndSave(Key $key)
91+
{
92+
throw new NotSupportedException(sprintf('The store "%s" does not supports blocking locks', get_class($this)));
93+
}
94+
8995
/**
9096
* {@inheritdoc}
9197
*/
92-
public function expire(Key $key, $ttl)
98+
public function putOffExpiration(Key $key, $ttl)
9399
{
94100
$successCount = 0;
95101
$failureCount = 0;
96102
$storesCount = count($this->stores);
97103

98104
foreach ($this->stores as $store) {
99-
if (!$store instanceof ExpirableStoreInterface) {
100-
++$successCount;
101-
continue;
102-
}
103-
104105
try {
105-
$store->expire($key, $ttl);
106+
$store->putOffExpiration($key, $ttl);
106107
++$successCount;
107108
} catch (\Exception $e) {
108109
++$failureCount;

src/Symfony/Component/Lock/Store/ExpirableStoreInterface.php

Lines changed: 0 additions & 34 deletions
This file was deleted.

src/Symfony/Component/Lock/Store/FlockStore.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
* @author Romain Neutron <imprec@gmail.com>
2828
* @author Nicolas Grekas <p@tchwork.com>
2929
*/
30-
class FlockStore extends AbstractStore implements BlockingStoreInterface
30+
class FlockStore extends AbstractStore
3131
{
3232
private $lockPath;
3333

@@ -102,6 +102,14 @@ private function lock(Key $key, $blocking)
102102
$key->setState(__CLASS__, $handle);
103103
}
104104

105+
/**
106+
* {@inheritdoc}
107+
*/
108+
public function putOffExpiration(Key $key, $ttl)
109+
{
110+
// do nothing, the flock locks forever.
111+
}
112+
105113
/**
106114
* {@inheritdoc}
107115
*/

src/Symfony/Component/Lock/Store/RedisStore.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*
2121
* @author Jérémy Derussé <jeremy@derusse.com>
2222
*/
23-
class RedisStore extends AbstractStore implements ExpirableStoreInterface
23+
class RedisStore extends AbstractStore
2424
{
2525
private $redis;
2626
private $defaultTtl;
@@ -58,10 +58,15 @@ public function save(Key $key, $blocking = false)
5858
}
5959
}
6060

61+
public function waitAndSave(Key $key)
62+
{
63+
throw new InvalidArgumentException(sprintf('The store "%s" does not supports blocking locks', get_class($this)));
64+
}
65+
6166
/**
6267
* {@inheritdoc}
6368
*/
64-
public function expire(Key $key, $ttl)
69+
public function putOffExpiration(Key $key, $ttl)
6570
{
6671
$script = '
6772
if redis.call("GET", KEYS[1]) == ARGV[1] then

src/Symfony/Component/Lock/Store/RetryTillSaveStore.php

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use Psr\Log\LoggerAwareTrait;
1515
use Psr\Log\NullLogger;
16-
use Symfony\Component\Lock\Exception\InvalidArgumentException;
1716
use Symfony\Component\Lock\Exception\LockConflictedException;
1817
use Symfony\Component\Lock\Key;
1918
use Symfony\Component\Lock\StoreInterface;
@@ -24,7 +23,7 @@
2423
*
25< 10000 /td>24
* @author Jérémy Derussé <jeremy@derusse.com>
2625
*/
27-
class RetryTillSaveStore extends AbstractStore implements ExpirableStoreInterface, BlockingStoreInterface
26+
class RetryTillSaveStore extends AbstractStore
2827
{
2928
use LoggerAwareTrait;
3029

@@ -59,12 +58,6 @@ public function save(Key $key)
5958
*/
6059
public function waitAndSave(Key $key)
6160
{
62-
if ($this->decorated instanceof BlockingStoreInterface) {
63-
$this->decorated->waitAndSave($key);
64-
65-
return;
66-
}
67-
6861
$retry = 0;
6962
$sleepRandomness = (int) ($this->retrySleep / 10);
7063
do {
@@ -85,12 +78,9 @@ public function waitAndSave(Key $key)
8578
/**
8679
* {@inheritdoc}
8780
*/
88-
public function expire(Key $key, $ttl)
81+
public function putOffExpiration(Key $key, $ttl)
8982
{
90-
if (!$this->decorated instanceof ExpirableStoreInterface) {
91-
throw new InvalidArgumentException(sprintf('Can not sets the expiration to an instance of "%s".', get_class($this->decorated)));
92-
}
93-
$this->decorated->expire($key, $ttl);
83+
$this->decorated->putOffExpiration($key, $ttl);
9484
}
9585

9686
/**

src/Symfony/Component/Lock/StoreInterface.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Lock;
1313

1414
use Symfony\Component\Lock\Exception\LockConflictedException;
15+
use Symfony\Component\Lock\Exception\NotSupportedException;
1516

1617
/**
1718
* StoreInterface defines an interface to manipulate a lock store.
@@ -29,6 +30,31 @@ interface StoreInterface
2930
*/
3031
public function save(Key $key);
3132

33+
/**
34+
* Wait a key becomes free, then stores the resource.
35+
*
36+
* If the store does not supports this feature it should thrown an NotSupportedException.
37+
*
38+
* @param Key $key key to lock
39+
*
40+
* @throws LockConflictedException
41+
* @throws NotSupportedException
42+
*/
43+
public function waitAndSave(Key $key);
44+
45+
/**
46+
* Extends the ttl of a resource.
47+
*
48+
* If the store does not supports this feature it should thrown an NotSupportedException.
49+
*
50+
* @param Key $key key to lock
51+
* @param float $ttl amount of second to keep the lock in the store
52+
*
53+
* @throws LockConflictedException
54+
* @throws NotSupportedException
55+
*/
56+
public function putOffExpiration(Key $key, $ttl);
57+
3258
/**
3359
* Removes a resource from the storage.
3460
*

src/Symfony/Component/Lock/Tests/LockTest.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313

1414
use Symfony\Component\Lock\Key;
1515
use Symfony\Component\Lock\Lock;
16-
use Symfony\Component\Lock\Store\BlockingStoreInterface;
17-
use Symfony\Component\Lock\Store\ExpirableStoreInterface;
1816
use Symfony\Component\Lock\StoreInterface;
1917

2018
/**
@@ -25,7 +23,7 @@ class LockTest extends \PHPUnit_Framework_TestCase
2523
public function testAcquireNoBlocking()
2624
{
2725
$key = new Key(uniqid(__METHOD__, true));
28-
$store = $this->getMockBuilder(BlockingStoreInterface::class)->getMock();
26+
$store = $this->getMockBuilder(StoreInterface::class)->getMock();
2927
$lock = new Lock($key, $store);
3028

3129
$store
@@ -38,7 +36,7 @@ public function testAcquireNoBlocking()
3836
public function testAcquireBlocking()
3937
{
4038
$key = new Key(uniqid(__METHOD__, true));
41-
$store = $this->getMockBuilder(BlockingStoreInterface::class)->getMock();
39+
$store = $this->getMockBuilder(StoreInterface::class)->getMock();
4240
$lock = new Lock($key, $store);
4341

4442
$store
@@ -54,15 +52,15 @@ public function testAcquireBlocking()
5452
public function testAcquireSetsTtl()
5553
{
5654
$key = new Key(uniqid(__METHOD__, true));
57-
$store = $this->getMockBuilder(ExpirableStoreInterface::class)->getMock();
55+
$store = $this->getMockBuilder(StoreInterface::class)->getMock();
5856
$lock = new Lock($key, $store, 10);
5957

6058
$store
6159
->expects($this->once())
6260
->method('save');
6361
$store
6462
->expects($this->once())
65-
->method('expire')
63+
->method('putOffExpiration')
6664
->with($key, 10);
6765

6866
$lock->acquire();
@@ -71,12 +69,12 @@ public function testAcquireSetsTtl()
7169
public function testRefresh()
7270
{
7371
$key = new Key(uniqid(__METHOD__, true));
74-
$store = $this->getMockBuilder(ExpirableStoreInterface::class)->getMock();
72+
$store = $this->getMockBuilder(StoreInterface::class)->getMock();
7573
$lock = new Lock($key, $store, 10);
7674

7775
$store
7876
->expects($this->once())
79-
->method('expire')
77+
->method('putOffExpiration')
8078
->with($key, 10);
8179

8280
$lock->refresh();

src/Symfony/Component/Lock/Tests/Store/AbstractRedisStoreTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
/**
1717
* @author Jérémy Derussé <jeremy@derusse.com>
1818
*/
19-
abstract class AbstractRedisStoreTest extends AbstractExpirableStoreTest
19+
abstract class AbstractRedisStoreTest extends AbstractStoreTest
2020
{
21+
use ExpirableStoreTestTrait;
22+
2123
/**
2224
* Return a RedisConnection.
2325
*

0 commit comments

Comments
 (0)
0