Skip to content

Commit 680c60d

Browse files
Remove getRedisIdentifier from Redis interfaces. This is not the task of our library but the task of the underlying libraries.
This also fixes that some of the Predis ConnectionInterface objects don't have a __toString() method.
1 parent d9d54b6 commit 680c60d

File tree

4 files changed

+32
-56
lines changed

4 files changed

+32
-56
lines changed

classes/mutex/PHPRedisMutex.php

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,8 @@ protected function add($redisAPI, string $key, string $value, int $expire): bool
4848
return $redisAPI->set($key, $value, ["nx", "ex" => $expire]);
4949
} catch (RedisException $e) {
5050
$message = sprintf(
51-
"Failed to acquire lock for key '%s' at %s",
52-
$key,
53-
$this->getRedisIdentifier($redisAPI)
51+
"Failed to acquire lock for key '%s'",
52+
$key
5453
);
5554
throw new LockAcquireException($message, 0, $e);
5655
}
@@ -73,20 +72,7 @@ protected function evalScript($redis, string $script, int $numkeys, array $argum
7372
try {
7473
return $redis->eval($script, $arguments, $numkeys);
7574
} catch (RedisException $e) {
76-
$message = sprintf(
77-
"Failed to release lock at %s",
78-
$this->getRedisIdentifier($redis)
79-
);
80-
throw new LockReleaseException($message, 0, $e);
75+
throw new LockReleaseException("Failed to release lock", 0, $e);
8176
}
8277
}
83-
84-
/**
85-
* @internal
86-
*/
87-
protected function getRedisIdentifier($redis)
88-
{
89-
/** @var Redis $redis */
90-
return sprintf("redis://%s:%d?database=%s", $redis->getHost(), $redis->getPort(), $redis->getDBNum());
91-
}
9278
}

classes/mutex/PredisMutex.php

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -43,42 +43,23 @@ protected function add($redisAPI, string $key, string $value, int $expire): bool
4343
return $redisAPI->set($key, $value, "EX", $expire, "NX") !== null;
4444
} catch (PredisException $e) {
4545
$message = sprintf(
46-
"Failed to acquire lock for key '%s' at %s",
47-
$key,
48-
$this->getRedisIdentifier($redisAPI)
46+
"Failed to acquire lock for key '%s'",
47+
$key
4948
);
5049
throw new LockAcquireException($message, 0, $e);
5150
}
5251
}
5352

53+
/**
54+
* @throws LockReleaseException
55+
*/
5456
protected function evalScript($client, string $script, int $numkeys, array $arguments)
5557
{
5658
/** @var ClientInterface $client */
5759
try {
5860
return $client->eval($script, $numkeys, ...$arguments);
5961
} catch (PredisException $e) {
60-
$message = sprintf(
61-
"Failed to release lock at %s",
62-
$this->getRedisIdentifier($client)
63-
);
64-
throw new LockReleaseException($message, 0, $e);
62+
throw new LockReleaseException("Failed to release lock", 0, $e);
6563
}
6664
}
67-
68-
/**
69-
* @internal
70-
*/
71-
protected function getRedisIdentifier($client)
72-
{
73-
/** @var ClientInterface $client */
74-
75-
/*
76-
* Note that there is no __toString() on the \Predis\Connection\ConnectionInterface
77-
* class. Some classes such as \Predis\Connection\Aggregate\SentinelReplication don't
78-
* have a __toString() method.
79-
*
80-
* todo fix this
81-
*/
82-
return (string) $client->getConnection();
83-
}
8465
}

classes/mutex/RedisMutex.php

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ protected function acquire(string $key, int $expire): bool
7777
$errored = 0;
7878
$this->token = \random_bytes(16);
7979
$exception = null;
80-
foreach ($this->redisAPIs as $redis) {
80+
foreach ($this->redisAPIs as $index => $redis) {
8181
try {
8282
if ($this->add($redis, $key, $this->token, $expire)) {
8383
$acquired++;
@@ -87,10 +87,9 @@ protected function acquire(string $key, int $expire): bool
8787
$context = [
8888
"key" => $key,
8989
"token" => $this->token,
90-
"redis" => $this->getRedisIdentifier($redis),
9190
"exception" => $exception
9291
];
93-
$this->logger->warning("Could not set {key} = {token} at {redis}.", $context);
92+
$this->logger->warning("Could not set {key} = {token} at server #{index}.", $context);
9493

9594
$errored++;
9695
}
@@ -146,10 +145,9 @@ protected function release(string $key): bool
146145
$context = [
147146
"key" => $key,
148147
"token" => $this->token,
149-
"redis" => $this->getRedisIdentifier($redis),
150148
"exception" => $e
151149
];
152-
$this->logger->warning("Could not unset {key} = {token} at {redis}.", $context);
150+
$this->logger->warning("Could not unset {key} = {token} at server #{index}.", $context);
153151
}
154152
}
155153
return $this->isMajority($released);
@@ -188,13 +186,4 @@ abstract protected function add($redisAPI, string $key, string $value, int $expi
188186
* @throws LockReleaseException An unexpected error happened.
189187
*/
190188
abstract protected function evalScript($redisAPI, string $script, int $numkeys, array $arguments);
191-
192-
/**
193-
* Returns a string representation of the Redis API.
194-
*
195-
* @param mixed $redisAPI The connected Redis API.
196-
* @return string The identifier.
197-
* @internal
198-
*/
199-
abstract protected function getRedisIdentifier($redisAPI);
200189
}

tests/mutex/PredisMutexTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PHPUnit\Framework\TestCase;
77
use Predis\ClientInterface;
88
use Predis\PredisException;
9+
use Psr\Log\LoggerInterface;
910

1011
/**
1112
* Tests for PredisMutex.
@@ -27,6 +28,11 @@ class PredisMutexTest extends TestCase
2728
*/
2829
private $mutex;
2930

31+
/**
32+
* @var LoggerInterface|MockObject
33+
*/
34+
private $logger;
35+
3036
protected function setUp()
3137
{
3238
parent::setUp();
@@ -36,6 +42,9 @@ protected function setUp()
3642
->getMock();
3743

3844
$this->mutex = new PredisMutex([$this->client], "test");
45+
46+
$this->logger = $this->createMock(LoggerInterface::class);
47+
$this->mutex->setLogger($this->logger);
3948
}
4049

4150
/**
@@ -50,6 +59,9 @@ public function testAddFailsToSetKey()
5059
->with("lock_test", $this->isType("string"), "EX", 4, "NX")
5160
->willReturn(null);
5261

62+
$this->logger->expects($this->never())
63+
->method("warning");
64+
5365
$this->mutex->synchronized(
5466
function () {
5567
$this->fail("Code execution is not expected");
@@ -69,6 +81,10 @@ public function testAddErrors()
6981
->with("lock_test", $this->isType("string"), "EX", 4, "NX")
7082
->willThrowException($this->createMock(PredisException::class));
7183

84+
$this->logger->expects($this->once())
85+
->method("warning")
86+
->with("Could not set {key} = {token} at server #{index}.", $this->anything());
87+
7288
$this->mutex->synchronized(
7389
function () {
7490
$this->fail("Code execution is not expected");
@@ -114,6 +130,10 @@ public function testEvalScriptFails()
114130
->with($this->anything(), 1, "lock_test", $this->isType("string"))
115131
->willThrowException($this->createMock(PredisException::class));
116132

133+
$this->logger->expects($this->once())
134+
->method("warning")
135+
->with("Could not unset {key} = {token} at server #{index}.", $this->anything());
136+
117137
$executed = false;
118138

119139
$this->mutex->synchronized(function () use (&$executed): void {

0 commit comments

Comments
 (0)