Skip to content

Commit

Permalink
[fix]修正GetInfo用第一个地址无法访问时没有轮询后续地址的问题。fix: #127
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Apr 11, 2024
1 parent e2335bc commit ca27efa
Showing 1 changed file with 30 additions and 16 deletions.
46 changes: 30 additions & 16 deletions NewLife.Redis/Redis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -479,28 +479,42 @@ public virtual TResult Execute<TResult>(String key, Func<RedisClient, String, TR
/// <returns></returns>
public virtual TResult Execute<TResult>(Func<RedisClient, TResult> func)
{
// 每次重试都需要重新从池里借出连接
var pool = Pool;
var client = pool.Get();
try
{
client.Reset();
return func(client);
}
catch (Exception ex)
// 统计性能
var sw = Counter?.StartCount();

var i = 0;
var delay = 500;
do
{
if (ex is SocketException or IOException)
// 每次重试都需要重新从池里借出连接
var pool = Pool;
var client = pool.Get();
try
{
client.Reset();
return func(client);
}
catch (RedisException) { throw; }
catch (Exception ex)
{
if (++i >= Retry) throw;

// 销毁连接
client.TryDispose();

// 网络异常时,自动切换到其它节点
if (ex is SocketException or IOException && _servers != null && i < _servers.Length)
_idxServer++;
else
Thread.Sleep(delay *= 2);
}
finally
{
pool.Put(client);

throw;
}
finally
{
pool.Put(client);
}
Counter?.StopCount(sw);
}
} while (true);
}

/// <summary>异步执行命令,经过管道。FullRedis中还会考虑Cluster分流</summary>
Expand Down

0 comments on commit ca27efa

Please sign in to comment.