From ca27efadcca5485941f8c2a9df80c8756ab9e675 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E7=9F=B3=E5=A4=B4?= Date: Thu, 11 Apr 2024 11:26:55 +0800 Subject: [PATCH] =?UTF-8?q?[fix]=E4=BF=AE=E6=AD=A3GetInfo=E7=94=A8?= =?UTF-8?q?=E7=AC=AC=E4=B8=80=E4=B8=AA=E5=9C=B0=E5=9D=80=E6=97=A0=E6=B3=95?= =?UTF-8?q?=E8=AE=BF=E9=97=AE=E6=97=B6=E6=B2=A1=E6=9C=89=E8=BD=AE=E8=AF=A2?= =?UTF-8?q?=E5=90=8E=E7=BB=AD=E5=9C=B0=E5=9D=80=E7=9A=84=E9=97=AE=E9=A2=98?= =?UTF-8?q?=E3=80=82fix:=20https://github.com/NewLifeX/NewLife.Redis/issue?= =?UTF-8?q?s/127?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- NewLife.Redis/Redis.cs | 46 +++++++++++++++++++++++++++--------------- 1 file changed, 30 insertions(+), 16 deletions(-) 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分流