Skip to content

Commit

Permalink
strict retry
Browse files Browse the repository at this point in the history
  • Loading branch information
amuluowin committed Jun 22, 2020
1 parent 2cfc587 commit 7e1d5db
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
5 changes: 3 additions & 2 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -378,8 +378,9 @@ public function executeCommand(string $name, array $params = [])
}
App::debug("Executing Redis Command: {$name}", 'redis');
$this->open();
$retrys = $this->retries > 0 ? $this->retries : 1;
while ($retrys-- >= 0) {
$retrys = $this->getPool()->getPoolConfig()->getMaxRetry();
$retrys = $retrys > 0 ? $retrys : 1;
while ($retrys--) {
try {
$data = $this->sendCommandInternal($command, $params, $type);
if ($name === 'HGETALL' || ($name === 'CONFIG' && is_array($data))) {
Expand Down
11 changes: 6 additions & 5 deletions src/PhpRedis.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Co\System;
use rabbit\db\Exception;
use rabbit\exception\NotSupportedException;
use rabbit\helper\ArrayHelper;
use rabbit\pool\AbstractConnection;
use rabbit\pool\PoolManager;
Expand Down Expand Up @@ -45,15 +44,17 @@ public function createConnection(): void
$this->sentinel = new \RedisSentinel($parseAry['host'], $parseAry['port'], $pool->getTimeout());
}
$this->conn = new \Redis();
$retry = $pool->getPoolConfig()->getMaxRetry();
while ($retry--) {
$retrys = $pool->getPoolConfig()->getMaxRetry();
$retrys = $retrys > 0 ? $retrys : 1;
while ($retrys--) {
if (false !== $master = $this->sentinel->getMasterAddrByName(ArrayHelper::getValue($config, 'master', 'mymaster'))) {
[$host, $port] = $master;
$this->conn->connect($host, (int)$port);
break;
return;
}
System::sleep($pool->getTimeout());
$retry > 0 && System::sleep($pool->getTimeout());
}
throw new Exception("Connect to $host:$port failed!");
} else {
$this->conn = new \Redis();
$retry = $pool->getPoolConfig()->getMaxRetry();
Expand Down
10 changes: 6 additions & 4 deletions src/SentinelConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ protected function open(): bool
$connection = ($this->unixSocket ?: $this->hostname . ':' . $this->port);
App::info('Opening redis sentinel connection: ' . $connection, SentinelsManager::LOG_KEY);
$this->_socket = new Client(SWOOLE_SOCK_TCP);
$retry = $this->retry;
while ($retry--) {
$retrys = $this->retry;
$retrys = $retrys > 0 ? $retrys : 1;
while ($retrys--) {
if ($this->_socket->connect($this->hostname, $this->port, $this->connectionTimeout ?? 3) === false) {
App::warning('Failed opening redis sentinel connection: ' . $connection, SentinelsManager::LOG_KEY);
continue;
Expand Down Expand Up @@ -114,8 +115,9 @@ public function executeCommand(string $name, array $params)
$command .= '$' . mb_strlen($arg, '8bit') . "\r\n" . $arg . "\r\n";
}

$retry = $this->retry;
while ($retry--) {
$retrys = $this->retry;
$retrys = $retrys > 0 ? $retrys : 1;
while ($retrys--) {
try {
$written = $this->_socket->send($command);
if ($written === false) {
Expand Down
6 changes: 3 additions & 3 deletions src/SwooleConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ protected function getConnectRedis(
$poolConfig = PoolManager::getPool($this->poolKey)->getPoolConfig();
$serialize = $poolConfig->getSerialize();
$redis = new \Swoole\Coroutine\Redis(['timeout' => $poolConfig->getTimeout()]);
$retry = $this->getPool()->getPoolConfig()->getMaxRetry();
$retry = $retry > 0 ? $retry : 1;
while ($retry-- > 0) {
$retrys = $this->getPool()->getPoolConfig()->getMaxRetry();
$retrys = $retrys > 0 ? $retrys : 1;
while ($retrys--) {
$result = $redis->connect($host, $port, $serialize);
if ($result !== false) {
if ($password) {
Expand Down

0 comments on commit 7e1d5db

Please sign in to comment.