From 53bd4e1af12a045aeb5c138c8352522c56c1281d Mon Sep 17 00:00:00 2001 From: George Wainaina Date: Fri, 31 May 2024 20:52:34 +0300 Subject: [PATCH 1/2] chore: .net upgrade to .net8 --- ReverseProxy/Dockerfile | 22 ------------------ .../ExtendedPolicies/IPAddressPolicy.cs | 23 ++++--------------- .../PartitionKeyQueryValuePolicy.cs | 2 +- .../PartitionKeyRouteValuePolicy.cs | 4 +--- ReverseProxy/ReverseProxy.csproj | 6 ++--- ReverseProxy/Startup.cs | 5 ++-- .../Utilities/{Partitioner.cs => Common.cs} | 10 +++----- ReverseProxy/appsettings.json | 2 +- 8 files changed, 15 insertions(+), 59 deletions(-) delete mode 100644 ReverseProxy/Dockerfile rename ReverseProxy/Utilities/{Partitioner.cs => Common.cs} (71%) diff --git a/ReverseProxy/Dockerfile b/ReverseProxy/Dockerfile deleted file mode 100644 index 0d7b08c..0000000 --- a/ReverseProxy/Dockerfile +++ /dev/null @@ -1,22 +0,0 @@ -#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging. - -FROM mcr.microsoft.com/dotnet/aspnet:3.1 AS base -WORKDIR /app -EXPOSE 80 -EXPOSE 443 - -FROM mcr.microsoft.com/dotnet/sdk:3.1 AS build -WORKDIR /src -COPY ["ReverseProxy.csproj", "./"] -RUN dotnet restore "ReverseProxy.csproj" -COPY . . -WORKDIR "/src/." -RUN dotnet build "ReverseProxy.csproj" -c Release -o /app/build - -FROM build AS publish -RUN dotnet publish "ReverseProxy.csproj" -c Release -o /app/publish - -FROM base AS final -WORKDIR /app -COPY --from=publish /app/publish . -ENTRYPOINT ["dotnet", "ReverseProxy.dll"] \ No newline at end of file diff --git a/ReverseProxy/ExtendedPolicies/IPAddressPolicy.cs b/ReverseProxy/ExtendedPolicies/IPAddressPolicy.cs index a73d7f3..546c05c 100644 --- a/ReverseProxy/ExtendedPolicies/IPAddressPolicy.cs +++ b/ReverseProxy/ExtendedPolicies/IPAddressPolicy.cs @@ -1,9 +1,9 @@ using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Logging; +using ReverseProxy.Utilities; using System; using System.Collections.Generic; using System.Linq; -using System.Security.Cryptography; using Yarp.ReverseProxy.LoadBalancing; using Yarp.ReverseProxy.Model; @@ -24,7 +24,7 @@ public DestinationState PickDestination(HttpContext context, ClusterState cluste try { string ipAddress = context.Connection.RemoteIpAddress.ToString(); - var partitionIndex = CalculatePartitionIndex(ipAddress, availableDestinations.Count); + int partitionIndex = Common.CalculatePartitionIndex(ipAddress, availableDestinations.Count); //Some Tests /* var test = CalculatePartitionIndex("192.168.1.10", availableDestinations.Count); @@ -37,8 +37,8 @@ public DestinationState PickDestination(HttpContext context, ClusterState cluste test = CalculatePartitionIndex("41.90.64.33", availableDestinations.Count); test = CalculatePartitionIndex("41.90.64.34", availableDestinations.Count); */ - logger.LogInformation($"PartitioninLoadbalancer chose partition {partitionIndex} for IP Address {ipAddress}"); - logger.LogInformation($"Request will be routed to: {availableDestinations[partitionIndex].Model.Config.Address}"); + logger.LogInformation($"partitionigLoadbalancer chose partition {partitionIndex} for IP Address {ipAddress}"); + logger.LogInformation($"request will be routed to: {availableDestinations[partitionIndex].Model.Config.Address}"); return availableDestinations.OrderBy(d => d.Model.Config.Address).ElementAt(partitionIndex); } catch (Exception ex) @@ -46,20 +46,5 @@ public DestinationState PickDestination(HttpContext context, ClusterState cluste return new DestinationState($"could-not-partition-request-by-ip|{ex.Message}"); } } - - private int CalculatePartitionIndex(string valueToPartitionOn, int maxNumberOfPartitions) - { - using (MD5 md5 = MD5.Create()) - { - byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(valueToPartitionOn); - byte[] hashedBytes = md5.ComputeHash(inputBytes); - //Use First Byte - byte firstByte = hashedBytes[0]; - int unMappedPartitionNr = firstByte; - unMappedPartitionNr = Math.Abs(unMappedPartitionNr); - int partitionIndex = unMappedPartitionNr % maxNumberOfPartitions; - return partitionIndex; - } - } } } diff --git a/ReverseProxy/ExtendedPolicies/PartitionKeyQueryValuePolicy.cs b/ReverseProxy/ExtendedPolicies/PartitionKeyQueryValuePolicy.cs index 119407f..63be6dd 100644 --- a/ReverseProxy/ExtendedPolicies/PartitionKeyQueryValuePolicy.cs +++ b/ReverseProxy/ExtendedPolicies/PartitionKeyQueryValuePolicy.cs @@ -28,7 +28,7 @@ public DestinationState PickDestination(HttpContext context, ClusterState cluste { if (context.Request.Query.TryGetValue("partitionKey", out var value)) { - int partitionIndex = Partitioner.CalculatePartitionIndex(value.ToString(), availableDestinations.Count); + int partitionIndex = Common.CalculatePartitionIndex(value.ToString(), availableDestinations.Count); logger.LogInformation($"Request will be routed to: {availableDestinations[partitionIndex].Model.Config.Address}"); return availableDestinations.OrderBy(d => d.Model.Config.Address).ElementAt(partitionIndex); } diff --git a/ReverseProxy/ExtendedPolicies/PartitionKeyRouteValuePolicy.cs b/ReverseProxy/ExtendedPolicies/PartitionKeyRouteValuePolicy.cs index c5d84f9..fd92367 100644 --- a/ReverseProxy/ExtendedPolicies/PartitionKeyRouteValuePolicy.cs +++ b/ReverseProxy/ExtendedPolicies/PartitionKeyRouteValuePolicy.cs @@ -33,7 +33,7 @@ public DestinationState PickDestination(HttpContext context, ClusterState cluste if (routeDictionary.ContainsKey(partitionBasedOn)) { var valueToPartitionOn = routeDictionary[partitionBasedOn].ToString(); //Unique Value of Partition Key e.g. 9999 \\use unique data like deviceCode, Machine Code etc - int partitionIndex = Partitioner.CalculatePartitionIndex(valueToPartitionOn, availableDestinations.Count); + int partitionIndex = Common.CalculatePartitionIndex(valueToPartitionOn, availableDestinations.Count); logger.LogInformation($"Request will be routed to: {availableDestinations[partitionIndex].Model.Config.Address}"); return availableDestinations.OrderBy(d => d.Model.Config.Address).ElementAt(partitionIndex); } @@ -64,7 +64,5 @@ private RouteValueDictionary GetDefaults(RouteTemplate parsedTemplate) result.Add(parameter.Name, parameter.DefaultValue); return result; } - - } } diff --git a/ReverseProxy/ReverseProxy.csproj b/ReverseProxy/ReverseProxy.csproj index 1159e42..159ce6f 100644 --- a/ReverseProxy/ReverseProxy.csproj +++ b/ReverseProxy/ReverseProxy.csproj @@ -1,14 +1,14 @@ - netcoreapp3.1 + net8.0 4b7aec56-23f4-4985-951a-8ee9587d2546 Linux - - + + diff --git a/ReverseProxy/Startup.cs b/ReverseProxy/Startup.cs index e06b077..1c26be2 100644 --- a/ReverseProxy/Startup.cs +++ b/ReverseProxy/Startup.cs @@ -22,9 +22,8 @@ public void ConfigureServices(IServiceCollection services) services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); - services - .AddReverseProxy() - .LoadFromConfig(_configuration.GetSection("ReverseProxy")); + services.AddReverseProxy() + .LoadFromConfig(_configuration.GetSection("ReverseProxy")); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) diff --git a/ReverseProxy/Utilities/Partitioner.cs b/ReverseProxy/Utilities/Common.cs similarity index 71% rename from ReverseProxy/Utilities/Partitioner.cs rename to ReverseProxy/Utilities/Common.cs index 290038e..f7d136e 100644 --- a/ReverseProxy/Utilities/Partitioner.cs +++ b/ReverseProxy/Utilities/Common.cs @@ -1,12 +1,9 @@ using System; -using System.Collections.Generic; -using System.Linq; using System.Security.Cryptography; -using System.Threading.Tasks; namespace ReverseProxy.Utilities { - public static class Partitioner + public static class Common { public static int CalculatePartitionIndex(string valueToPartitionOn, int maxNumberOfPartitions) { @@ -14,9 +11,8 @@ public static int CalculatePartitionIndex(string valueToPartitionOn, int maxNumb { byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(valueToPartitionOn); byte[] hashedBytes = md5.ComputeHash(inputBytes); - //Use First Byte - byte firstByte = hashedBytes[0]; - int unMappedPartitionNr = firstByte; + //use first byte + int unMappedPartitionNr = hashedBytes[0]; unMappedPartitionNr = Math.Abs(unMappedPartitionNr); int partitionIndex = unMappedPartitionNr % maxNumberOfPartitions; return partitionIndex; diff --git a/ReverseProxy/appsettings.json b/ReverseProxy/appsettings.json index da76089..9ba6d52 100644 --- a/ReverseProxy/appsettings.json +++ b/ReverseProxy/appsettings.json @@ -18,7 +18,7 @@ }, "Clusters": { "cluster1": { - "LoadBalancingPolicy": "IPAddress", //IPAddress,PartitionKeyQueryValue,PartitionKeyRouteValue,RoundRobin,PowerOfTwoChoices,Random,First,LeastRequests + "LoadBalancingPolicy": "RoundRobin", //IPAddress,PartitionKeyQueryValue,PartitionKeyRouteValue,RoundRobin,PowerOfTwoChoices,Random,First,LeastRequests "Destinations": { "cluster1/destination1": { "Address": "https://www.google.com/" From 318a3adf81f7516ed25f1c6d5898449b668b5070 Mon Sep 17 00:00:00 2001 From: George Wainaina Date: Fri, 31 May 2024 20:58:22 +0300 Subject: [PATCH 2/2] chore: removed dev appsettings --- ReverseProxy/appsettings.Development.json | 39 ----------------------- 1 file changed, 39 deletions(-) delete mode 100644 ReverseProxy/appsettings.Development.json diff --git a/ReverseProxy/appsettings.Development.json b/ReverseProxy/appsettings.Development.json deleted file mode 100644 index ee992dd..0000000 --- a/ReverseProxy/appsettings.Development.json +++ /dev/null @@ -1,39 +0,0 @@ -{ - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft": "Warning", - "Microsoft.Hosting.Lifetime": "Information", - "PartitioningLoadbalancer.LoadbalancerPolicy": "Information" - } - }, - "ReverseProxy": { - "Routes": { - "route1": { - "ClusterId": "cluster1", - "Match": { - "Path": "{**catch-all}" - } - } - }, - "Clusters": { - "cluster1": { - "LoadBalancingPolicy": "Random", //IPAddress,PartitionKeyQueryValue,PartitionKeyRouteValue,RoundRobin,PowerOfTwoChoices,Random,First,LeastRequests - "Destinations": { - "cluster1/destination1": { - "Address": "http://localhost:9090/" - }, - "cluster1/destination2": { - "Address": "http://localhost:9091/" - }, - "cluster1/destination3": { - "Address": "http://localhost:9092/" - }, - "cluster1/destination4": { - "Address": "http://localhost:9093/" - } - } - } - } - } -}