Skip to content

Changed the Rate Limiter API to action-based #41657

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

Closed
wants to merge 3 commits into from
Closed
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
2 changes: 1 addition & 1 deletion src/Middleware/RateLimiting/src/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ Microsoft.AspNetCore.RateLimiting.RateLimiterOptions.OnRejected.set -> void
Microsoft.AspNetCore.RateLimiting.RateLimiterOptions.RateLimiterOptions() -> void
Microsoft.AspNetCore.RateLimiting.RateLimitingApplicationBuilderExtensions
static Microsoft.AspNetCore.RateLimiting.RateLimitingApplicationBuilderExtensions.UseRateLimiter(this Microsoft.AspNetCore.Builder.IApplicationBuilder! app) -> Microsoft.AspNetCore.Builder.IApplicationBuilder!
static Microsoft.AspNetCore.RateLimiting.RateLimitingApplicationBuilderExtensions.UseRateLimiter(this Microsoft.AspNetCore.Builder.IApplicationBuilder! app, Microsoft.AspNetCore.RateLimiting.RateLimiterOptions! options) -> Microsoft.AspNetCore.Builder.IApplicationBuilder!
static Microsoft.AspNetCore.RateLimiting.RateLimitingApplicationBuilderExtensions.UseRateLimiter(this Microsoft.AspNetCore.Builder.IApplicationBuilder! app, System.Action<Microsoft.AspNetCore.RateLimiting.RateLimiterOptions!>! options) -> Microsoft.AspNetCore.Builder.IApplicationBuilder!
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,14 @@ public static IApplicationBuilder UseRateLimiter(this IApplicationBuilder app)
/// <param name="app"></param>
/// <param name="options"></param>
/// <returns></returns>
public static IApplicationBuilder UseRateLimiter(this IApplicationBuilder app, RateLimiterOptions options)
public static IApplicationBuilder UseRateLimiter(this IApplicationBuilder app, Action<RateLimiterOptions> options)
Copy link
Member

Choose a reason for hiding this comment

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

This'd probably be called configure now rather than options, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch, I missed this. configure seems to be the common naming convention for this so I'll change it. Currently pending on some back and forth in #41655 regarding whether this overload is needed or not. Once that's settled I'll update it with the latest changes

{
ArgumentNullException.ThrowIfNull(app, nameof(app));
ArgumentNullException.ThrowIfNull(options, nameof(options));

return app.UseMiddleware<RateLimitingMiddleware>(Options.Create(options));
var rateLimiterOptions = new RateLimiterOptions();
options.Invoke(rateLimiterOptions);

return app.UseMiddleware<RateLimitingMiddleware>(Options.Create(rateLimiterOptions));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ public void UseRateLimiter_ThrowsOnNullOptions()
public void UseRateLimiter_RespectsOptions()
{
// These are the options that should get used
var options = new RateLimiterOptions();
options.DefaultRejectionStatusCode = 429;
options.Limiter = new TestPartitionedRateLimiter<HttpContext>(new TestRateLimiter(false));
var configureOptions = new Action<RateLimiterOptions>(opt =>
{
opt.DefaultRejectionStatusCode = 429;
opt.Limiter = new TestPartitionedRateLimiter<HttpContext>(new TestRateLimiter(false));
});

// These should not get used
var services = new ServiceCollection();
Expand All @@ -44,7 +46,7 @@ public void UseRateLimiter_RespectsOptions()
var appBuilder = new ApplicationBuilder(serviceProvider);

// Act
appBuilder.UseRateLimiter(options);
appBuilder.UseRateLimiter(configureOptions);
var app = appBuilder.Build();
var context = new DefaultHttpContext();
app.Invoke(context);
Expand Down