From 07d91e59da4d4b84b2ec389c82ec6e45b4923f14 Mon Sep 17 00:00:00 2001 From: Niklas Keller Date: Tue, 10 Sep 2019 08:42:50 +0200 Subject: [PATCH] Fix database on reconnect --- src/Config.php | 26 +++++++++++++++++++++++++- src/RemoteExecutor.php | 4 ++-- src/Subscriber.php | 2 +- src/functions.php | 6 +++--- test/ConfigTest.php | 2 +- test/SelectTest.php | 12 ++++++++++++ 6 files changed, 44 insertions(+), 8 deletions(-) diff --git a/src/Config.php b/src/Config.php index c3d0dd9..efab543 100644 --- a/src/Config.php +++ b/src/Config.php @@ -44,7 +44,7 @@ public function __construct(string $uri) $this->applyUri($uri); } - public function getUri(): string + public function getConnectUri(): string { return $this->uri; } @@ -69,6 +69,30 @@ public function getDatabase(): int return $this->database; } + public function withTimeout(int $timeout): self + { + $clone = clone $this; + $clone->timeout = $timeout; + + return $clone; + } + + public function withPassword(string $password): self + { + $clone = clone $this; + $clone->password = $password; + + return $clone; + } + + public function withDatabase(int $database): self + { + $clone = clone $this; + $clone->database = $database; + + return $clone; + } + /** * When using the "redis" schemes the URI is parsed according to the rules defined by the provisional registration * documents approved by IANA. If the URI has a password in its "user-information" part or a database number in the diff --git a/src/RemoteExecutor.php b/src/RemoteExecutor.php index 2bde10b..fe44fc8 100644 --- a/src/RemoteExecutor.php +++ b/src/RemoteExecutor.php @@ -84,7 +84,7 @@ private function connect(): Promise return $this->connect = call(function () { /** @var RespSocket $resp */ - $resp = yield connect($this->config); + $resp = yield connect($this->config->withDatabase($this->database)); asyncCall(function () use ($resp) { try { @@ -101,7 +101,7 @@ private function connect(): Promise } } - throw new SocketException('Socket to redis instance (' . $this->config->getUri() . ') closed unexpectedly'); + throw new SocketException('Socket to redis instance (' . $this->config->getConnectUri() . ') closed unexpectedly'); } catch (\Throwable $error) { $queue = $this->queue; $this->queue = []; diff --git a/src/Subscriber.php b/src/Subscriber.php index ea87381..9613859 100644 --- a/src/Subscriber.php +++ b/src/Subscriber.php @@ -102,7 +102,7 @@ private function connect(): Promise } } - throw new SocketException('Socket to redis instance (' . $this->config->getUri() . ') closed unexpectedly'); + throw new SocketException('Socket to redis instance (' . $this->config->getConnectUri() . ') closed unexpectedly'); } catch (\Throwable $error) { $emitters = \array_merge($this->emitters, $this->patternEmitters); diff --git a/src/functions.php b/src/functions.php index 6bc2f58..203e211 100644 --- a/src/functions.php +++ b/src/functions.php @@ -63,10 +63,10 @@ function connect(Config $config): Promise return call(static function () use ($config) { try { $connectContext = (new ConnectContext)->withConnectTimeout($config->getTimeout()); - $resp = new RespSocket(yield Socket\connect($config->getUri(), $connectContext)); + $resp = new RespSocket(yield Socket\connect($config->getConnectUri(), $connectContext)); } catch (Socket\SocketException $e) { throw new SocketException( - 'Failed to connect to redis instance (' . $config->getUri() . ')', + 'Failed to connect to redis instance (' . $config->getConnectUri() . ')', 0, $e ); @@ -92,7 +92,7 @@ function connect(Config $config): Promise throw $response; } } else { - throw new RedisException('Failed to connect to redis instance (' . $config->getUri() . ')'); + throw new RedisException('Failed to connect to redis instance (' . $config->getConnectUri() . ')'); } } diff --git a/test/ConfigTest.php b/test/ConfigTest.php index efee340..7a61d87 100644 --- a/test/ConfigTest.php +++ b/test/ConfigTest.php @@ -18,7 +18,7 @@ public function test( ): void { $config = Config::fromUri($uri); - self::assertSame($expectedUri, $config->getUri()); + self::assertSame($expectedUri, $config->getConnectUri()); self::assertSame($expectedDatabase, $config->getDatabase()); self::assertSame($expectedTimeout, $config->getTimeout()); self::assertSame($expectedPassword, $config->getPassword()); diff --git a/test/SelectTest.php b/test/SelectTest.php index af2a0d6..02ed678 100644 --- a/test/SelectTest.php +++ b/test/SelectTest.php @@ -19,4 +19,16 @@ public function testSelect(): \Generator $this->assertNotSame($payload, yield $this->redis->get('foobar')); } + + public function testSelectOnReconnect(): \Generator + { + $redis1 = $this->createInstance(); + yield $redis1->select(1); + yield $redis1->quit(); + $payload = 'bar'; + yield $redis1->set('foobar', $payload); + $this->assertSame($payload, yield $redis1->get('foobar')); + + $this->assertNotSame($payload, yield $this->redis->get('foobar')); + } }