public Server GetCurrentServer(int localPort, ServerSelectStrategy.FilterFunc filter, string targetAddr = null, bool cfgRandom = false, bool usingRandom = false, bool forceRandom = false) { lock (serverStrategyMap) { if (!serverStrategyMap.ContainsKey(localPort)) { serverStrategyMap[localPort] = new ServerSelectStrategy(); } ServerSelectStrategy serverStrategy = serverStrategyMap[localPort]; uricache.SetTimeout(keepVisitTime); uricache.Sweep(); if (sameHostForSameTarget && !forceRandom && targetAddr != null && uricache.ContainsKey(targetAddr)) { UriVisitTime visit = uricache.Get(targetAddr); if (visit.index < configs.Count && configs[visit.index].enable && configs[visit.index].ServerSpeedLog().ErrorContinurousTimes == 0) { uricache.Del(targetAddr); return(configs[visit.index]); } } if (forceRandom) { int index; if (filter == null && randomInGroup) { index = serverStrategy.Select(configs, this.index, balanceAlgorithm, delegate(Server server, Server selServer) { if (selServer != null) { return(selServer.group == server.group); } return(false); }, true); } else { index = serverStrategy.Select(configs, this.index, balanceAlgorithm, filter, true); } if (index == -1) { return(GetErrorServer()); } return(configs[index]); } else if (usingRandom && cfgRandom) { int index; if (filter == null && randomInGroup) { index = serverStrategy.Select(configs, this.index, balanceAlgorithm, delegate(Server server, Server selServer) { if (selServer != null) { return(selServer.group == server.group); } return(false); }); } else { index = serverStrategy.Select(configs, this.index, balanceAlgorithm, filter); } if (index == -1) { return(GetErrorServer()); } if (targetAddr != null) { UriVisitTime visit = new UriVisitTime(); visit.uri = targetAddr; visit.index = index; visit.visitTime = DateTime.Now; uricache.Set(targetAddr, visit); } return(configs[index]); } else { if (index >= 0 && index < configs.Count) { int selIndex = index; if (usingRandom) { for (int i = 0; i < configs.Count; ++i) { if (configs[selIndex].isEnable()) { break; } else { selIndex = (selIndex + 1) % configs.Count; } } } if (targetAddr != null) { UriVisitTime visit = new UriVisitTime(); visit.uri = targetAddr; visit.index = selIndex; visit.visitTime = DateTime.Now; uricache.Set(targetAddr, visit); } return(configs[selIndex]); } else { return(GetErrorServer()); } } } }
public Server GetCurrentServer(int localPort, ServerSelectStrategy.FilterFunc filter, string targetAddr = null, bool cfgRandom = false, bool usingRandom = false, bool forceRandom = false) { lock (serverStrategyMap) { if (!serverStrategyMap.ContainsKey(localPort)) { serverStrategyMap[localPort] = new ServerSelectStrategy(); } ServerSelectStrategy serverStrategy = serverStrategyMap[localPort]; foreach (KeyValuePair <UriVisitTime, string> p in time2uri) { if ((DateTime.Now - p.Key.visitTime).TotalSeconds < keepVisitTime) { break; } uri2time.Remove(p.Value); time2uri.Remove(p.Key); break; } if (sameHostForSameTarget && !forceRandom && targetAddr != null && uri2time.ContainsKey(targetAddr)) { UriVisitTime visit = uri2time[targetAddr]; if (visit.index < configs.Count && configs[visit.index].enable) { //uri2time.Remove(targetURI); time2uri.Remove(visit); visit.visitTime = DateTime.Now; uri2time[targetAddr] = visit; time2uri[visit] = targetAddr; return(configs[visit.index]); } } if (forceRandom) { int index; if (filter == null && randomInGroup) { index = serverStrategy.Select(configs, this.index, randomAlgorithm, delegate(Server server, Server selServer) { if (selServer != null) { return(selServer.group == server.group); } return(false); }, true); } else { index = serverStrategy.Select(configs, this.index, randomAlgorithm, filter, true); } if (index == -1) { return(GetErrorServer()); } return(configs[index]); } else if (usingRandom && cfgRandom) { int index; if (filter == null && randomInGroup) { index = serverStrategy.Select(configs, this.index, randomAlgorithm, delegate(Server server, Server selServer) { if (selServer != null) { return(selServer.group == server.group); } return(false); }); } else { index = serverStrategy.Select(configs, this.index, randomAlgorithm, filter); } if (index == -1) { return(GetErrorServer()); } if (targetAddr != null) { UriVisitTime visit = new UriVisitTime(); visit.uri = targetAddr; visit.index = index; visit.visitTime = DateTime.Now; if (uri2time.ContainsKey(targetAddr)) { time2uri.Remove(uri2time[targetAddr]); } uri2time[targetAddr] = visit; time2uri[visit] = targetAddr; } return(configs[index]); } else { if (index >= 0 && index < configs.Count) { int selIndex = index; if (usingRandom) { for (int i = 0; i < configs.Count; ++i) { if (configs[selIndex].isEnable()) { break; } else { selIndex = (selIndex + 1) % configs.Count; } } } if (targetAddr != null) { UriVisitTime visit = new UriVisitTime(); visit.uri = targetAddr; visit.index = selIndex; visit.visitTime = DateTime.Now; if (uri2time.ContainsKey(targetAddr)) { time2uri.Remove(uri2time[targetAddr]); } uri2time[targetAddr] = visit; time2uri[visit] = targetAddr; } return(configs[selIndex]); } else { return(GetErrorServer()); } } } }