Skip to content

Commit

Permalink
Add Id to Backend type microsoft#9 (microsoft#157)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tratcher authored May 12, 2020
1 parent 87ab35e commit ec54755
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 7 deletions.
2 changes: 1 addition & 1 deletion samples/ReverseProxy.Sample/CustomConfigFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Microsoft.ReverseProxy.Sample
{
public class CustomConfigFilter : IProxyConfigFilter
{
public Task ConfigureBackendAsync(string id, Backend backend, CancellationToken cancel)
public Task ConfigureBackendAsync(Backend backend, CancellationToken cancel)
{
backend.HealthCheckOptions ??= new HealthCheckOptions();
// How to use custom metadata to configure backends
Expand Down
1 change: 0 additions & 1 deletion samples/ReverseProxy.Sample/ReverseProxy.Sample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

<ItemGroup>
<ProjectReference Include="..\..\src\ReverseProxy.Core\Microsoft.ReverseProxy.Core.csproj"/>
<ProjectReference Include="..\..\src\ReverseProxy.Utilities\Microsoft.ReverseProxy.Utilities.csproj"/>
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ namespace Microsoft.ReverseProxy.Core.Abstractions
/// </summary>
public sealed class Backend : IDeepCloneable<Backend>
{
/// <summary>
/// The Id for this backend. This needs to be globally unique.
/// </summary>
public string Id { get; set; }

/// <summary>
/// Circuit breaker options.
/// </summary>
Expand Down Expand Up @@ -54,6 +59,7 @@ Backend IDeepCloneable<Backend>.DeepClone()
{
return new Backend
{
Id = Id,
CircuitBreakerOptions = CircuitBreakerOptions?.DeepClone(),
QuotaOptions = QuotaOptions?.DeepClone(),
PartitioningOptions = PartitioningOptions?.DeepClone(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ public static IReverseProxyBuilder AddReverseProxy(this IServiceCollection servi
public static IReverseProxyBuilder LoadFromConfig(this IReverseProxyBuilder builder, IConfiguration config)
{
builder.Services.Configure<ProxyConfigOptions>(config);
builder.Services.AddOptions().PostConfigure<ProxyConfigOptions>(options =>
{
foreach (var (id, backend) in options.Backends)
{
// The Object style config binding puts the id as the key in the dictionary, but later we want it on the
// backend object as well.
backend.Id = id;
}
});
builder.Services.AddHostedService<ProxyConfigLoader>();

return builder;
Expand Down
1 change: 1 addition & 0 deletions src/ReverseProxy.Core/Service/Config/ConfigErrors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ internal static class ConfigErrors
internal const string ParsedRouteRuleMultiplePathMatchers = "ParsedRoute_RuleMultiplePathMatchers";
internal const string ParsedRouteRuleInvalidMatcher = "ParsedRoute_RuleInvalidMatcher";

internal const string ConfigBuilderBackendIdMismatch = "ConfigBuilder_BackendIdMismatch";
internal const string ConfigBuilderBackendException = "ConfigBuilder_BackendException";
internal const string ConfigBuilderRouteException = "ConfigBuilder_RouteException";
}
Expand Down
10 changes: 9 additions & 1 deletion src/ReverseProxy.Core/Service/Config/DynamicConfigBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
Expand Down Expand Up @@ -60,9 +61,16 @@ public async Task<IDictionary<string, Backend>> GetBackendsAsync(IConfigErrorRep
{
try
{
if (id != backend.Id)
{
errorReporter.ReportError(ConfigErrors.ConfigBuilderBackendIdMismatch, id,
$"The backend Id '{backend.Id}' and its lookup key '{id}' do not match.");
continue;
}

foreach (var filter in _filters)
{
await filter.ConfigureBackendAsync(id, backend, cancellation);
await filter.ConfigureBackendAsync(backend, cancellation);
}

configuredBackends[id] = backend;
Expand Down
2 changes: 1 addition & 1 deletion src/ReverseProxy.Core/Service/Config/IProxyConfigFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public interface IProxyConfigFilter
/// </summary>
/// <param name="id">The id for the backend.</param>
/// <param name="backend">The Backend instance to configure.</param>
Task ConfigureBackendAsync(string id, Backend backend, CancellationToken cancel);
Task ConfigureBackendAsync(Backend backend, CancellationToken cancel);

/// <summary>
/// Allows modification of a route configuration.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ private TestBackendsRepo CreateOneBackend()
{
"backend1", new Backend
{
Id = "backend1",
Destinations =
{
{ "d1", new Destination { Address = TestAddress } }
Expand Down Expand Up @@ -131,6 +132,7 @@ public async Task BuildConfigAsync_OneBackend_Works()
Assert.Single(result.Value.Backends);
var backend = result.Value.Backends["backend1"];
Assert.NotNull(backend);
Assert.Equal("backend1", backend.Id);
Assert.Single(backend.Destinations);
var destination = backend.Destinations["d1"];
Assert.NotNull(destination);
Expand Down Expand Up @@ -194,7 +196,7 @@ public async Task BuildConfigAsync_ConfigFilterRouteActions_CanFixBrokenRoute()

private class FixRouteHostFilter : IProxyConfigFilter
{
public Task ConfigureBackendAsync(string id, Backend backend, CancellationToken cancel)
public Task ConfigureBackendAsync(Backend backend, CancellationToken cancel)
{
return Task.CompletedTask;
}
Expand All @@ -208,7 +210,7 @@ public Task ConfigureRouteAsync(ProxyRoute route, CancellationToken cancel)

private class BackendAndRouteFilter : IProxyConfigFilter
{
public Task ConfigureBackendAsync(string id, Backend backend, CancellationToken cancel)
public Task ConfigureBackendAsync(Backend backend, CancellationToken cancel)
{
backend.HealthCheckOptions = new HealthCheckOptions() { Enabled = true, Interval = TimeSpan.FromSeconds(12) };
return Task.CompletedTask;
Expand Down Expand Up @@ -249,7 +251,7 @@ public async Task BuildConfigAsync_ConfigFilterConfiguresBackend_Works()

private class BackendAndRouteThrows : IProxyConfigFilter
{
public Task ConfigureBackendAsync(string id, Backend backend, CancellationToken cancel)
public Task ConfigureBackendAsync(Backend backend, CancellationToken cancel)
{
throw new NotFiniteNumberException("Test exception");
}
Expand Down

0 comments on commit ec54755

Please sign in to comment.