Skip to content

Commit 112a4c6

Browse files
committed
Implement richer extensibility mechanisms for routes and clusters in configuration
1 parent afab63c commit 112a4c6

16 files changed

+769
-54
lines changed

reverse-proxy.sln

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Prometheus", "Prometheus",
100100
EndProject
101101
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HttpLoadApp", "samples\Prometheus\HttpLoadApp\HttpLoadApp.csproj", "{BD73A038-8F3D-4BB2-A5C4-C8D077969DED}"
102102
EndProject
103+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ReverseProxy.Config.Extensions", "samples\ReverseProxy.Config.Extensions\ReverseProxy.Config.Extensions.csproj", "{26386632-EF27-45B2-8098-BFFD5AC182D5}"
104+
EndProject
103105
Global
104106
GlobalSection(SolutionConfigurationPlatforms) = preSolution
105107
Debug|Any CPU = Debug|Any CPU
@@ -364,6 +366,14 @@ Global
364366
{BD73A038-8F3D-4BB2-A5C4-C8D077969DED}.Release|Any CPU.Build.0 = Release|Any CPU
365367
{BD73A038-8F3D-4BB2-A5C4-C8D077969DED}.Release|x64.ActiveCfg = Release|Any CPU
366368
{BD73A038-8F3D-4BB2-A5C4-C8D077969DED}.Release|x64.Build.0 = Release|Any CPU
369+
{26386632-EF27-45B2-8098-BFFD5AC182D5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
370+
{26386632-EF27-45B2-8098-BFFD5AC182D5}.Debug|Any CPU.Build.0 = Debug|Any CPU
371+
{26386632-EF27-45B2-8098-BFFD5AC182D5}.Debug|x64.ActiveCfg = Debug|Any CPU
372+
{26386632-EF27-45B2-8098-BFFD5AC182D5}.Debug|x64.Build.0 = Debug|Any CPU
373+
{26386632-EF27-45B2-8098-BFFD5AC182D5}.Release|Any CPU.ActiveCfg = Release|Any CPU
374+
{26386632-EF27-45B2-8098-BFFD5AC182D5}.Release|Any CPU.Build.0 = Release|Any CPU
375+
{26386632-EF27-45B2-8098-BFFD5AC182D5}.Release|x64.ActiveCfg = Release|Any CPU
376+
{26386632-EF27-45B2-8098-BFFD5AC182D5}.Release|x64.Build.0 = Release|Any CPU
367377
EndGlobalSection
368378
GlobalSection(SolutionProperties) = preSolution
369379
HideSolutionNode = FALSE
@@ -407,6 +417,7 @@ Global
407417
{AC0EF892-7D32-4EAF-BE99-3696181E889F} = {149C61A2-D9F8-49B9-9F9B-3C953FEF53AA}
408418
{78A83196-53F3-444B-84BF-F0FDC2CD0466} = {149C61A2-D9F8-49B9-9F9B-3C953FEF53AA}
409419
{BD73A038-8F3D-4BB2-A5C4-C8D077969DED} = {78A83196-53F3-444B-84BF-F0FDC2CD0466}
420+
{26386632-EF27-45B2-8098-BFFD5AC182D5} = {149C61A2-D9F8-49B9-9F9B-3C953FEF53AA}
410421
EndGlobalSection
411422
GlobalSection(ExtensibilityGlobals) = postSolution
412423
SolutionGuid = {31F6924A-E427-4830-96E9-B47CEB7BFE78}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using System.Text.Json;
2+
using Yarp.ReverseProxy.Configuration;
3+
using Yarp.ReverseProxy.Model;
4+
5+
var builder = WebApplication.CreateBuilder(args);
6+
7+
builder.Services.AddReverseProxy()
8+
.LoadFromConfig(builder.Configuration.GetSection("ReverseProxy"))
9+
.AddRouteExtensions<UserModel>("User")
10+
.AddRouteExtensions<ABTest>("ABTest")
11+
.AddClusterExtensions<Service>("Service")
12+
//Error type
13+
.AddRouteExtensions<More>("More");
14+
15+
var app = builder.Build();
16+
17+
app.MapReverseProxy();
18+
19+
app.UseRouting();
20+
app.UseEndpoints(endpoints =>
21+
{
22+
23+
endpoints.MapReverseProxy(proxyPipeline =>
24+
{
25+
proxyPipeline.Use((context, next) =>
26+
{
27+
var proxyFeature = context.Features.Get<IReverseProxyFeature>();
28+
29+
var user = proxyFeature?.Route.Config.GetExtension<UserModel>();
30+
Console.WriteLine(user?.Name);
31+
32+
var ab = proxyFeature?.Route.Config.GetExtension<ABTest>();
33+
Console.WriteLine(JsonSerializer.Serialize(ab));
34+
35+
var more = proxyFeature?.Route.Config.GetExtension<More>();
36+
Console.WriteLine(more?.Information);
37+
38+
var service = proxyFeature?.Cluster.Config.GetExtension<Service>();
39+
Console.WriteLine(service?.State);
40+
41+
return next();
42+
});
43+
});
44+
});
45+
46+
app.Run();
47+
48+
49+
public class UserModel:IConfigExtension
50+
{
51+
public string Name { get; set; }
52+
}
53+
54+
public class More :IConfigExtension
55+
{
56+
public string Information { get; set; }
57+
}
58+
59+
public class ABTest : IConfigExtension
60+
{
61+
public Dictionary<string, double> ABTests { get; set; }
62+
}
63+
64+
public class Service : IConfigExtension
65+
{
66+
public string State { get; set; }
67+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"$schema": "https://json.schemastore.org/launchsettings.json",
3+
"profiles": {
4+
"http": {
5+
"commandName": "Project",
6+
"dotnetRunMessages": true,
7+
"launchBrowser": true,
8+
"applicationUrl": "http://localhost:5193",
9+
"environmentVariables": {
10+
"ASPNETCORE_ENVIRONMENT": "Development"
11+
}
12+
},
13+
"https": {
14+
"commandName": "Project",
15+
"dotnetRunMessages": true,
16+
"launchBrowser": true,
17+
"applicationUrl": "https://localhost:7023;http://localhost:5193",
18+
"environmentVariables": {
19+
"ASPNETCORE_ENVIRONMENT": "Development"
20+
}
21+
}
22+
}
23+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>$(ReleaseTFMs)</TargetFrameworks>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
<ItemGroup>
9+
<ProjectReference Include="..\..\src\ReverseProxy\Yarp.ReverseProxy.csproj" />
10+
</ItemGroup>
11+
12+
</Project>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
}
8+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
},
8+
"AllowedHosts": "*",
9+
"ReverseProxy": {
10+
"Routes": {
11+
"route1": {
12+
"ClusterId": "cluster1",
13+
"Match": {
14+
"Path": "{**catch-all}"
15+
},
16+
"Extensions": {
17+
"User": {
18+
"Name": "admin"
19+
},
20+
"More": "string",
21+
"ABTest": {
22+
"ABTests": {
23+
"C1": 1,
24+
"C2": 2
25+
}
26+
}
27+
}
28+
}
29+
},
30+
"Clusters": {
31+
"cluster1": {
32+
"Destinations": {
33+
"destination1": {
34+
"Address": "https://example.com/"
35+
}
36+
},
37+
"Extensions": {
38+
"Service": {
39+
"State": "Error"
40+
}
41+
}
42+
}
43+
}
44+
}
45+
}

src/ReverseProxy/Configuration/ClusterConfig.cs

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,21 @@ public sealed record ClusterConfig
5454
/// </summary>
5555
public IReadOnlyDictionary<string, string>? Metadata { get; init; }
5656

57+
/// <summary>
58+
/// Arbitrary type-value pairs that further extend this cluster.
59+
/// </summary>
60+
public IReadOnlyDictionary<Type, IConfigExtension>? Extensions { get; init; }
61+
62+
public T? GetExtension<T>() where T : IConfigExtension
63+
{
64+
if (Extensions?.TryGetValue(typeof(T), out var extension) ?? false)
65+
{
66+
return (T)extension;
67+
}
68+
69+
return default;
70+
}
71+
5772
public bool Equals(ClusterConfig? other)
5873
{
5974
if (other is null)
@@ -73,25 +88,30 @@ internal bool EqualsExcludingDestinations(ClusterConfig other)
7388
}
7489

7590
return string.Equals(ClusterId, other.ClusterId, StringComparison.OrdinalIgnoreCase)
76-
&& string.Equals(LoadBalancingPolicy, other.LoadBalancingPolicy, StringComparison.OrdinalIgnoreCase)
77-
// CS0252 warning only shows up in VS https://github.com/dotnet/roslyn/issues/49302
78-
&& SessionAffinity == other.SessionAffinity
79-
&& HealthCheck == other.HealthCheck
80-
&& HttpClient == other.HttpClient
81-
&& HttpRequest == other.HttpRequest
82-
&& CaseSensitiveEqualHelper.Equals(Metadata, other.Metadata);
91+
&& string.Equals(LoadBalancingPolicy, other.LoadBalancingPolicy, StringComparison.OrdinalIgnoreCase)
92+
// CS0252 warning only shows up in VS https://github.com/dotnet/roslyn/issues/49302
93+
&& SessionAffinity == other.SessionAffinity
94+
&& HealthCheck == other.HealthCheck
95+
&& HttpClient == other.HttpClient
96+
&& HttpRequest == other.HttpRequest
97+
&& CaseSensitiveEqualHelper.Equals(Metadata, other.Metadata)
98+
&& CollectionEqualityHelper.Equals(Extensions, other.Extensions);
8399
}
84100

85101
public override int GetHashCode()
86102
{
87-
return HashCode.Combine(
88-
ClusterId?.GetHashCode(StringComparison.OrdinalIgnoreCase),
89-
LoadBalancingPolicy?.GetHashCode(StringComparison.OrdinalIgnoreCase),
90-
SessionAffinity,
91-
HealthCheck,
92-
HttpClient,
93-
HttpRequest,
94-
CollectionEqualityHelper.GetHashCode(Destinations),
95-
CaseSensitiveEqualHelper.GetHashCode(Metadata));
103+
// HashCode.Combine(...) takes only 8 arguments
104+
var hash = new HashCode();
105+
hash.Add(ClusterId?.GetHashCode(StringComparison.OrdinalIgnoreCase));
106+
hash.Add(LoadBalancingPolicy?.GetHashCode(StringComparison.OrdinalIgnoreCase));
107+
hash.Add(SessionAffinity);
108+
hash.Add(HealthCheck);
109+
hash.Add(HttpClient);
110+
hash.Add(HttpRequest);
111+
hash.Add(CollectionEqualityHelper.GetHashCode(Destinations));
112+
hash.Add(CaseSensitiveEqualHelper.GetHashCode(Metadata));
113+
hash.Add(CollectionEqualityHelper.GetHashCode(Extensions));
114+
return hash.ToHashCode();
115+
96116
}
97117
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace Yarp.ReverseProxy.Configuration;
5+
6+
public class ConfigExtensionsOptions
7+
{
8+
public Dictionary<string, Type> RouteExtensions { get; } = new();
9+
public Dictionary<string, Type> ClusterExtensions { get; } = new();
10+
}

0 commit comments

Comments
 (0)