Skip to content

Commit

Permalink
Fix database on reconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
kelunik committed Sep 10, 2019
1 parent 04543ab commit 07d91e5
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 8 deletions.
26 changes: 25 additions & 1 deletion src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function __construct(string $uri)
$this->applyUri($uri);
}

public function getUri(): string
public function getConnectUri(): string
{
return $this->uri;
}
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/RemoteExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 = [];
Expand Down
2 changes: 1 addition & 1 deletion src/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
6 changes: 3 additions & 3 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand All @@ -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() . ')');
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
12 changes: 12 additions & 0 deletions test/SelectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
}

0 comments on commit 07d91e5

Please sign in to comment.