From 008e3544f2cfb8df777e0c4c042ab0668e6524c2 Mon Sep 17 00:00:00 2001 From: clowwindy Date: Sat, 25 Jul 2015 18:39:14 +0800 Subject: [PATCH] refine HA --- shadowsocks-csharp/Controller/Service/TCPRelay.cs | 2 +- .../Controller/Strategy/HighAvailabilityStrategy.cs | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/shadowsocks-csharp/Controller/Service/TCPRelay.cs b/shadowsocks-csharp/Controller/Service/TCPRelay.cs index 15c3bc34..39e98f49 100644 --- a/shadowsocks-csharp/Controller/Service/TCPRelay.cs +++ b/shadowsocks-csharp/Controller/Service/TCPRelay.cs @@ -383,7 +383,7 @@ namespace Shadowsocks.Controller { if (retryCount < 4) { - Console.WriteLine("Connection failed, retrying"); + Logging.Debug("Connection failed, retrying"); StartConnect(); retryCount++; } diff --git a/shadowsocks-csharp/Controller/Strategy/HighAvailabilityStrategy.cs b/shadowsocks-csharp/Controller/Strategy/HighAvailabilityStrategy.cs index a4173da2..1060b37c 100644 --- a/shadowsocks-csharp/Controller/Strategy/HighAvailabilityStrategy.cs +++ b/shadowsocks-csharp/Controller/Strategy/HighAvailabilityStrategy.cs @@ -90,6 +90,7 @@ namespace Shadowsocks.Controller.Strategy */ public void ChooseNewServer() { + Server oldServer = _currentServer; List servers = new List(_serverStatus.Values); DateTime now = DateTime.Now; foreach (var status in servers) @@ -98,7 +99,7 @@ namespace Shadowsocks.Controller.Strategy // 100 * failure - 2 * latency - 0.5 * (lastread - lastwrite) status.score = 100 * 1000 * Math.Min(5 * 60, (now - status.lastFailure).TotalSeconds) - -2 * 5 * (Math.Min(2000, status.latency.TotalMilliseconds) / (1 + (now - status.lastTimeDetectLatency).TotalSeconds / 30 / 2) + + -2 * 5 * (Math.Min(2000, status.latency.TotalMilliseconds) / (1 + (now - status.lastTimeDetectLatency).TotalSeconds / 30 / 10) + -0.5 * 200 * Math.Min(5, (status.lastRead - status.lastWrite).TotalSeconds)); Logging.Debug(String.Format("server: {0} latency:{1} score: {2}", status.server.FriendlyName(), status.latency, status.score)); } @@ -120,6 +121,10 @@ namespace Shadowsocks.Controller.Strategy if (max != null) { _currentServer = max.server; + if (_currentServer != oldServer) + { + Console.WriteLine("HA switching to server: {0}", _currentServer.FriendlyName()); + } Logging.Debug(String.Format("choosing server: {0}", _currentServer.FriendlyName())); } }