Skip to content

Commit

Permalink
Merge pull request #4 from swagfin/feature/net-upgrade
Browse files Browse the repository at this point in the history
Feature/net upgrade
  • Loading branch information
swagfin authored Jun 8, 2024
2 parents d8d8af0 + 318a3ad commit 2e709fd
Show file tree
Hide file tree
Showing 9 changed files with 15 additions and 98 deletions.
22 changes: 0 additions & 22 deletions ReverseProxy/Dockerfile

This file was deleted.

23 changes: 4 additions & 19 deletions ReverseProxy/ExtendedPolicies/IPAddressPolicy.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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);
Expand All @@ -37,29 +37,14 @@ 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)
{
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;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -64,7 +64,5 @@ private RouteValueDictionary GetDefaults(RouteTemplate parsedTemplate)
result.Add(parameter.Name, parameter.DefaultValue);
return result;
}


}
}
6 changes: 3 additions & 3 deletions ReverseProxy/ReverseProxy.csproj
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<UserSecretsId>4b7aec56-23f4-4985-951a-8ee9587d2546</UserSecretsId>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.10.14" />
<PackageReference Include="Yarp.ReverseProxy" Version="1.0.0-preview.12.21328.2" />
<PackageReference Include="System.IO.Hashing" Version="8.0.0" />
<PackageReference Include="Yarp.ReverseProxy" Version="2.1.0" />
</ItemGroup>

</Project>
5 changes: 2 additions & 3 deletions ReverseProxy/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ public void ConfigureServices(IServiceCollection services)
services.AddSingleton<ILoadBalancingPolicy, IPAddressPolicy>();
services.AddSingleton<ILoadBalancingPolicy, PartitionKeyRouteValuePolicy>();
services.AddSingleton<ILoadBalancingPolicy, PartitionKeyQueryValuePolicy>();
services
.AddReverseProxy()
.LoadFromConfig(_configuration.GetSection("ReverseProxy"));
services.AddReverseProxy()
.LoadFromConfig(_configuration.GetSection("ReverseProxy"));
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
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)
{
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;
//use first byte
int unMappedPartitionNr = hashedBytes[0];
unMappedPartitionNr = Math.Abs(unMappedPartitionNr);
int partitionIndex = unMappedPartitionNr % maxNumberOfPartitions;
return partitionIndex;
Expand Down
39 changes: 0 additions & 39 deletions ReverseProxy/appsettings.Development.json

This file was deleted.

2 changes: 1 addition & 1 deletion ReverseProxy/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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/"
Expand Down

0 comments on commit 2e709fd

Please sign in to comment.