Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[6.x] Reconnect on connection missing to recover horizon #30778

Merged
merged 2 commits into from
Dec 6, 2019
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
33 changes: 32 additions & 1 deletion src/Illuminate/Redis/Connections/PhpRedisConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,34 @@

use Closure;
use Illuminate\Contracts\Redis\Connection as ConnectionContract;
use Illuminate\Support\Str;
use Redis;
use RedisCluster;
use RedisException;

/**
* @mixin \Redis
*/
class PhpRedisConnection extends Connection implements ConnectionContract
{
/**
* The connection creation callback.
*
* @var callable
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

callable|null

*/
protected $connector;

/**
* Create a new PhpRedis connection.
*
* @param \Redis $client
* @param callable $connector
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

callable|null

* @return void
*/
public function __construct($client)
public function __construct($client, callable $connector = null)
{
$this->client = $client;
$this->connector = $connector;
}

/**
Expand Down Expand Up @@ -416,6 +427,26 @@ public function executeRaw(array $parameters)
return $this->command('rawCommand', $parameters);
}

/**
* Run a command against the Redis database.
*
* @param string $method
* @param array $parameters
* @return mixed
*/
public function command($method, array $parameters = [])
{
try {
return parent::command($method, $parameters);
} catch (RedisException $e) {
if (Str::contains($e->getMessage(), 'went away')) {
$this->client = $this->connector ? call_user_func($this->connector) : $this->client;
}

throw $e;
}
}

/**
* Disconnects from the Redis instance.
*
Expand Down
10 changes: 7 additions & 3 deletions src/Illuminate/Redis/Connectors/PhpRedisConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@ class PhpRedisConnector implements Connector
*/
public function connect(array $config, array $options)
{
return new PhpRedisConnection($this->createClient(array_merge(
$config, $options, Arr::pull($config, 'options', [])
)));
$connector = function () use ($config, $options) {
return $this->createClient(array_merge(
$config, $options, Arr::pull($config, 'options', [])
));
};

return new PhpRedisConnection($connector(), $connector);
}

/**
Expand Down