Skip to content

[Container] Add OutputCache configuration from file #2883

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Application/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Yarp.ReverseProxy.Configuration;


// Load configuration
Expand All @@ -26,13 +27,12 @@

// Configure YARP
builder.AddServiceDefaults();
builder.Services.AddServiceDiscovery();
builder.Services.AddServiceDiscovery()
.AddOutputCache(builder.Configuration.GetSection("OutputCache"));
builder.Services.AddReverseProxy()
.LoadFromConfig(builder.Configuration.GetSection("ReverseProxy"))
.AddServiceDiscoveryDestinationResolver();

Console.WriteLine(builder.Configuration.GetSection("ReverseProxy").Value);

var app = builder.Build();
app.MapReverseProxy();

Expand Down
6 changes: 5 additions & 1 deletion src/Application/Yarp.Application.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AssemblyName>yarp</AssemblyName>
<EnableConfigurationBindingGenerator>true</EnableConfigurationBindingGenerator>
</PropertyGroup>

<!-- Used by publishing infrastructure to get the version to use for blob publishing -->
<Target Name="ReturnPackageVersion" Returns="$(PackageVersion)" />

<ItemGroup>
<PackageReference Include="Yarp.ReverseProxy" Version="$(YarpNugetVersion)" />
<PackageReference Include="Microsoft.Extensions.ServiceDiscovery" Version="$(MicrosoftExtensionsServiceDiscovery)" />
<PackageReference Include="Microsoft.Extensions.ServiceDiscovery.Yarp" Version="$(MicrosoftExtensionsServiceDiscoveryYarp)" />

Expand All @@ -25,4 +25,8 @@
<PackageReference Include="AspNetCore.HealthChecks.ApplicationStatus" Version="$(AspNetCoreHealthChecksApplicationStatus)" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ReverseProxy\Yarp.ReverseProxy.csproj" />
</ItemGroup>

</Project>
122 changes: 122 additions & 0 deletions src/ReverseProxy/Configuration/Middlewares/OutputCacheConfig.cs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be in the container assembly instead?
Long-term this is something that should likely be in ASP.NET instead of YARP.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's something I asked myself too, but I want to be able to reference these types in Aspire later...

I coud just duplicate the code for now in Aspire, that's a possibility. It would avoid the need of publishing a nuget package for that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose it's a question of whether we see this being valuable to YARP users outside of Aspire.
I don't have a problem with it shipping from YARP initially, we could always take the breaking change eventually if ASP.NET starts to include it.

Good point re: package update, if we're treating this as more experimental, then I think it'd be more flexible for us if we copy-paste it in Aspire instead, and keep it just in the container here for now.

Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.OutputCaching;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace Yarp.ReverseProxy.Configuration;

/// <summary>
/// Configuration for <see cref="OutputCacheOptions"/>
/// </summary>
public sealed record OutputCacheConfig
{
/// <inheritdoc cref="OutputCacheOptions.SizeLimit"/>
public long SizeLimit { get; set; } = 100 * 1024 * 1024;

/// <inheritdoc cref="OutputCacheOptions.MaximumBodySize"/>
public long MaximumBodySize { get; set; } = 64 * 1024 * 1024;

/// <inheritdoc cref="OutputCacheOptions.DefaultExpirationTimeSpan"/>
public TimeSpan DefaultExpirationTimeSpan { get; set; } = TimeSpan.FromSeconds(60);

/// <inheritdoc cref="OutputCacheOptions.UseCaseSensitivePaths"/>
public bool UseCaseSensitivePaths { get; set; }

/// <summary>
/// Policies that will be added with <see cref="OutputCacheOptions.AddBasePolicy(Action{OutputCachePolicyBuilder}, bool)"/>
/// </summary>
public IDictionary<string, NamedCacheConfig> NamedPolicies { get; set; } = new Dictionary<string, NamedCacheConfig>(StringComparer.OrdinalIgnoreCase);
}

/// <summary>
/// Configuration for <see cref="OutputCachePolicyBuilder"/>
/// </summary>
public sealed record NamedCacheConfig
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should either move this to a more nested namespace, or name this NamedOutputCacheConfig, just cache config is too generic for Yarp.ReverseProxy.Configuration IMO.

{
/// <summary>
/// Flag to exclude or not the default policy
/// </summary>
public bool ExcludeDefaultPolicy { get; set; }

/// <inheritdoc cref="OutputCachePolicyBuilder.Expire(TimeSpan)"/>
public TimeSpan? ExpirationTimeSpan { get; set; }

/// <inheritdoc cref="OutputCachePolicyBuilder.NoCache"/>
public bool NoCache { get; set; }

/// <inheritdoc cref="OutputCachePolicyBuilder.SetVaryByQuery(string[])"/>
public string[]? VaryByQueryKeys { get; set; }

/// <inheritdoc cref="OutputCachePolicyBuilder.SetVaryByHeader(string[])"/>
public string[]? VaryByHeaders { get; set; }
}

/// <summary>
/// Collections of extensions to configure OutputCache
/// </summary>
public static class OutputCacheConfigExtensions
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I think we've mostly stuck with one type per file so far

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Meh, we have modern IDE that supports multiple types per file. I think when the types are small and related, I find it easier to keep it together, if the file isn't big of course.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This type specifically should likely be in namespace Microsoft.Extensions.DependencyInjection (doesn't matter if it's not public)

{
/// <summary>
/// Add and configure OuputCache
/// </summary>
public static IServiceCollection AddOutputCache(this IServiceCollection services, IConfiguration config)
{
if (config == null)
{
return services;
}

var outputCacheConfig = config.Get<OutputCacheConfig>();

if (outputCacheConfig != null)
{
services.AddOutputCache(outputCacheConfig);
}

return services;
}

/// <summary>
/// Add and configure OuputCache
/// </summary>
public static IServiceCollection AddOutputCache(this IServiceCollection services, OutputCacheConfig config)
{
return services.AddOutputCache(options =>
{
options.SizeLimit = config.SizeLimit;
options.MaximumBodySize = config.MaximumBodySize;
options.DefaultExpirationTimeSpan = config.DefaultExpirationTimeSpan;
options.UseCaseSensitivePaths = config.UseCaseSensitivePaths;

foreach (var policy in config.NamedPolicies)
{
options.AddPolicy(policy.Key,
builder => PolicyBuilder(builder, policy.Value),
policy.Value.ExcludeDefaultPolicy);
}
});
}

private static void PolicyBuilder(OutputCachePolicyBuilder builder, NamedCacheConfig policy)
{
if (policy.ExpirationTimeSpan.HasValue)
builder.Expire(policy.ExpirationTimeSpan.Value);

if (policy.NoCache)
builder.NoCache();

if (policy.VaryByQueryKeys != null)
builder.SetVaryByQuery(policy.VaryByQueryKeys);

if (policy.VaryByHeaders != null)
builder.SetVaryByHeader(policy.VaryByHeaders);
}
}
2 changes: 2 additions & 0 deletions src/ReverseProxy/Yarp.ReverseProxy.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
<IsAotCompatible>true</IsAotCompatible>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageTags>yarp;dotnet;reverse-proxy;aspnetcore</PackageTags>
<EnableConfigurationBindingGenerator>true</EnableConfigurationBindingGenerator>
<InterceptorsNamespaces>$(InterceptorsNamespaces);Microsoft.Extensions.Configuration.Binder.SourceGeneration</InterceptorsNamespaces>
</PropertyGroup>

<ItemGroup>
Expand Down
93 changes: 93 additions & 0 deletions test/ReverseProxy.Tests/Configuration/OutputCacheConfigTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Xunit;

namespace Yarp.ReverseProxy.Configuration;

public class OutputCacheConfigTests
{
[Fact]
public async Task All_Options_Added()
{
var config = new OutputCacheConfig();
config.DefaultExpirationTimeSpan = TimeSpan.FromSeconds(1);
config.MaximumBodySize = 10;
config.SizeLimit = 20;
config.UseCaseSensitivePaths = true;
config.NamedPolicies.Add("test1", new NamedCacheConfig { ExpirationTimeSpan = TimeSpan.FromSeconds(5), ExcludeDefaultPolicy = true });
config.NamedPolicies.Add("test2", new NamedCacheConfig { ExpirationTimeSpan = TimeSpan.FromSeconds(15), ExcludeDefaultPolicy = false });
config.NamedPolicies.Add("test3", new NamedCacheConfig { ExpirationTimeSpan = TimeSpan.FromSeconds(3), ExcludeDefaultPolicy = true, VaryByHeaders = new[] { "X-SomeHeader" } });

var builder = WebApplication.CreateBuilder();
builder.Services.AddOutputCache(config)
.AddReverseProxy();

var app = builder.Build();

var policies = app.Services.GetRequiredService<IYarpOutputCachePolicyProvider>();
var test1 = await policies.GetPolicyAsync("test1");
var test2 = await policies.GetPolicyAsync("test2");
var test3 = await policies.GetPolicyAsync("test3");

Assert.NotNull(test1);
Assert.NotNull(test2);
Assert.NotNull(test3);
}

[Fact]
public async Task All_Options_Added_Json()
{
var json =
"""
{
"OutputCache": {
"DefaultExpirationTimeSpan": "00:05:00",
"MaximumBodySize": 10,
"SizeLimit": 20,
"UseCaseSensitivePaths": true,
"NamedPolicies": {
"test1": {
"ExpirationTimeSpan": "00:05:00",
"ExcludeDefaultPolicy": true
},
"test2": {
"ExpirationTimeSpan": "00:15:00",
"ExcludeDefaultPolicy": false
},
"test3": {
"ExpirationTimeSpan": "00:03:00",
"ExcludeDefaultPolicy": true,
"VaryByHeaders": [ "X-SomeHeader" ]
}
}
}
}
""";
var configBuilder = new ConfigurationBuilder();
var stream = new MemoryStream(Encoding.UTF8.GetBytes(json));
var config = configBuilder.AddJsonStream(stream).Build();

var builder = WebApplication.CreateBuilder();
builder.Services.AddOutputCache(config.GetSection("OutputCache"))
.AddReverseProxy();

var app = builder.Build();

var policies = app.Services.GetRequiredService<IYarpOutputCachePolicyProvider>();
var test1 = await policies.GetPolicyAsync("test1");
var test2 = await policies.GetPolicyAsync("test2");
var test3 = await policies.GetPolicyAsync("test3");

Assert.NotNull(test1);
Assert.NotNull(test2);
Assert.NotNull(test3);
Comment on lines +89 to +91
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I take it there's no good way to assert that the relevant properties were applied correctly?

}
}
Loading