public static NodeData.Types.NodeService LeastConnections(List <NodeData.Types.NodeService> services, ConcurrentDictionary <string, SessionContext> keyContext, out SessionContext hitSessionContext) { NodeData.Types.NodeService result = null; hitSessionContext = null; var availableServices = services.Where(s => s.ExpireTimestamp > DateTimeOffset.UtcNow.ToUnixTimeSeconds()).Select(s => s); var count = availableServices.Count(); if (count != 0) { int index = 0; int leastConnectionsIndex = 0; int leastConnections = int.MaxValue; foreach (NodeData.Types.NodeService service in availableServices) { SessionContext currentContext = GetSessionContext(keyContext, service.Hash); int currentRequests = currentContext.GetCurrentRequests(); if (currentRequests < leastConnections) { hitSessionContext = currentContext; leastConnectionsIndex = index; leastConnections = currentRequests; } index++; } if (leastConnections != int.MaxValue) { result = availableServices.ElementAt(leastConnectionsIndex); } } return(result); }
public static NodeData.Types.NodeService NoLoadBalance(List <NodeData.Types.NodeService> services, ConcurrentDictionary <string, SessionContext> keyContext, out SessionContext hitSessionContext) { NodeData.Types.NodeService result = services.Where(s => s.ExpireTimestamp > DateTimeOffset.UtcNow.ToUnixTimeSeconds()).FirstOrDefault(); if (result == null) { hitSessionContext = null; } else { hitSessionContext = GetSessionContext(keyContext, result.Hash); } return(result); }
public static NodeData.Types.NodeService SmoothWeightRoundRobin(List <NodeData.Types.NodeService> services, ConcurrentDictionary <string, SessionContext> keyContext, out SessionContext hitSessionContext) { NodeData.Types.NodeService result = null; hitSessionContext = null; var availableServices = services.Where(s => s.ExpireTimestamp > DateTimeOffset.UtcNow.ToUnixTimeSeconds()).Select(s => s); var count = availableServices.Count(); if (count != 0) { // 调整权重 foreach (NodeData.Types.NodeService service in availableServices) { SessionContext currentContext = GetSessionContext(keyContext, service.Hash); currentContext.SetCurrentLoadBalanceWeight(currentContext.GetCurrentLoadBalanceWeight() + service.Weight); } int index = 0; // 原始权重之和 int weightSum = 0; // 最大当前权重 int maxCurrentWeight = 0; // 最大当前权重对象下标 int maxCurrentWeightIndex = 0; foreach (NodeData.Types.NodeService service in availableServices) { weightSum += service.Weight; SessionContext currentContext = GetSessionContext(keyContext, service.Hash); int currentWeight = currentContext.GetCurrentLoadBalanceWeight(); if (currentWeight >= maxCurrentWeight) { hitSessionContext = currentContext; maxCurrentWeightIndex = index; maxCurrentWeight = currentWeight; } index++; } // 对选中的后端再次设置权重 hitSessionContext.SetCurrentLoadBalanceWeight(maxCurrentWeight - weightSum); result = availableServices.ElementAt(maxCurrentWeightIndex); } return(result); }
public static NodeData.Types.NodeService Random(List <NodeData.Types.NodeService> services, ConcurrentDictionary <string, SessionContext> keyContext, out SessionContext hitSessionContext) { var availableServices = services.Where(s => s.ExpireTimestamp > DateTimeOffset.UtcNow.ToUnixTimeSeconds()).Select(s => s); var count = availableServices.Count(); NodeData.Types.NodeService result = null; if (count == 0) { hitSessionContext = null; } else { result = availableServices.ElementAt(random.Next(count)); hitSessionContext = GetSessionContext(keyContext, result.Hash); } return(result); }
public static NodeData.Types.NodeService SourceIpHash(int sourceIpAddressHashCode, List <NodeData.Types.NodeService> services, ConcurrentDictionary <string, SessionContext> keyContext, out SessionContext hitSessionContext) { var availableServices = services.Where(s => s.ExpireTimestamp > DateTimeOffset.UtcNow.ToUnixTimeSeconds()).Select(s => s); var count = availableServices.Count(); NodeData.Types.NodeService result = null; if (count == 0) { hitSessionContext = null; } else { int length = availableServices.Count(); int hashCode = Math.Abs(sourceIpAddressHashCode); int index = hashCode % length; result = availableServices.ElementAt(index); hitSessionContext = GetSessionContext(keyContext, result.Hash); } return(result); }