Skip to content

Commit

Permalink
[6.x] Reconnect on connection missing to recover horizon (#30778)
Browse files Browse the repository at this point in the history
* reconnect on connection missing to recover horizon

* add property
taylorotwell authored Dec 6, 2019

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 83dc8c6 commit 4834815
Showing 2 changed files with 39 additions and 4 deletions.
33 changes: 32 additions & 1 deletion src/Illuminate/Redis/Connections/PhpRedisConnection.php
Original file line number Diff line number Diff line change
@@ -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
*/
protected $connector;

/**
* Create a new PhpRedis connection.
*
* @param \Redis $client
* @param callable $connector
* @return void
*/
public function __construct($client)
public function __construct($client, callable $connector = null)
{
$this->client = $client;
$this->connector = $connector;
}

/**
@@ -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.
*
10 changes: 7 additions & 3 deletions src/Illuminate/Redis/Connectors/PhpRedisConnector.php
Original file line number Diff line number Diff line change
@@ -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);
}

/**

0 comments on commit 4834815

Please sign in to comment.