-
-
Notifications
You must be signed in to change notification settings - Fork 49
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
Feature sentinel #131
base: 3.x
Are you sure you want to change the base?
Feature sentinel #131
Conversation
Hey @sartor, thanks for opening up this ticket 👍 This is definitely a useful feature for this project. In most use cases you already have a Redis infrastructure when adding a sentinel, which means the sentinel feature in this project has to work properly. If you need anything from our side, we're happy to help to get this shipped! 🎉 |
Something wrong with sentinel tests in github environment. Local ip address is looking like invalid IPv6. I'll check it later. May be you any have ideas? |
…entinel # Conflicts: # .github/workflows/ci.yml
Hey @sartor ! I'm trying to use the code of your PR to add the redis sentinel support for my app. Can you give more details about what you created please? |
Hi. It is not http call. My code construct some kind of dsn address (connection uri with parameters) before connection to redis sentinels. After connection retrieved it calls for some commands according to https://redis.io/docs/reference/sentinel-clients/ to determine valid master "dsn". There is no http protocol here. This code working in production for 3 years for now without major issues |
Ok thanks, I understand. I'm intanciating the client as follows:
but the connection with the sentinel throws an error:
It's thrown by this method of the
because $args is looks like this, when it should be a string:
Do you have an idea why? Thanks |
More specifically, I don't understand why the |
Here is my RedisPool class for handling connections: <?php
declare(strict_types=1);
namespace App\Components;
use Clue\React\Redis\Io\StreamingClient;
use Clue\React\Redis\SentinelClient;
use function React\Async\await;
class RedisPool
{
private const REDIS_RETRY_INTERVAL = 0.5;
private int $attempts = 0;
private float $lastTime = 0;
private ?StreamingClient $client = null;
public function connection()
{
if ($this->client !== null) {
return $this->client;
}
$currentTime = microtime(true);
if ($currentTime - $this->lastTime < self::REDIS_RETRY_INTERVAL) {
return null;
}
$this->client = $this->tryConnect();
if ($this->client === null) {
$this->lastTime = $currentTime;
$this->attempts++;
echo date('Y-m-d H:i:s ') . "Redis connection attempt: $this->attempts\n";
}
return $this->client;
}
private function tryConnect()
{
try {
$sentinels = array_map('trim', explode(',', $_ENV['REDIS_HOSTS'] ?? '127.0.0.1:26379'));
$sentinelClient = new SentinelClient($sentinels, $_ENV['REDIS_MASTER'] ?? 'mymaster');
/** @var StreamingClient $client */
$client = await($sentinelClient->masterConnection('/' . $_ENV['REDIS_DB']));
} catch (\Throwable $e) {
echo "Unable to connect to redis\n{$e->getMessage()}\n";
return null;
}
$client->removeAllListeners('close');
$client->on('close', function () {
echo 'Redis closed' . PHP_EOL;
$this->client = null;
});
$client->on('error', function (\Throwable $e) {
echo 'Redis error: ' . $e->getMessage() . PHP_EOL;
$this->client = null;
});
echo date('Y-m-d H:i:s ') . "Master connection established\n";
return $client;
}
} Is use it simply: $redisPool = new RedisPool();
$redis = $redisPool->connection();
if ($redis === null) {
return (new Response(504, [], 'no redis connection'));
}
await($redis->rpush($listName, $serializedData)); |
Redis sentinel master auto discovery
This is alpha version, it may contains major bugs. Use it only on own risk!
Introduced new class
SentinelClient
with public methods:
masterAddress() - for simple master address discovery from sentinel
masterConnection() - for complete check for master connection according to https://redis.io/docs/reference/sentinel-clients/
Some tests included. I've changed ci.yml to support sentinel tests
Related to #69