diff --git a/NewLife.Redis/Redis.cs b/NewLife.Redis/Redis.cs index 484991e..7268ac3 100644 --- a/NewLife.Redis/Redis.cs +++ b/NewLife.Redis/Redis.cs @@ -479,28 +479,42 @@ public virtual TResult Execute(String key, Func public virtual TResult Execute(Func 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); } /// 异步执行命令,经过管道。FullRedis中还会考虑Cluster分流