Skip to content

RateLimitingMiddleware updates - #43053

Merged
wtgodbe merged 11 commits into
dotnet:mainfrom
wtgodbe:wtgodbe/RateLimit4
Aug 15, 2022
Merged

wtgodbe merged 11 commits into
dotnet:mainfrom
wtgodbe:wtgodbe/RateLimit4

Conversation

@wtgodbe

@wtgodbe wtgodbe commented Aug 2, 2022 •

Copy link
Copy Markdown
Member

Resolves #42667

  • Change convenience methods to take Action<Options> Rather than an Options instance
  • Add public attributes for enabling/disabling rate limiting
  • Add IServiceCollection extension method
  • Add extension method to put a policy instance directly on an endpoint

Still need to update the sample to use an MVC controller, which I'll do in this PR, but wanted to open it now to get eyes on it.

/// <summary>
/// The name of the policy which needs to be applied.
/// </summary>
public string? PolicyName { get; }

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This feels super weird, from a consumer perspective there is no way this can be null.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Sure it can. Add an inline policy using the RequireRateLimiting extension method and then look at the endpoint metadata. I get the point about not being able to trivially construct such an attribute, but hopefully we can add more first-class support for inline policies later.

/// <summary>
/// The policy which needs to be applied, if present.
/// </summary>
internal DefaultRateLimiterPolicy? Policy { get; }

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

And this is sad, someone who wants to write their own rate limiting middleware (maybe they disagree with some design decision, like want to run global and endpoint specific limits no matter if one fails) with our options and attributes won't be able to access this.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is also the case with named polices. It's not like the policy maps on RateLimiterOptions are public. If we want to design for people replacing the middleware but not the options/metadata, we have to consider that way up front and not now. Maybe we could do it in a different major release if it's important. I'm not convinced yet.

@wtgodbe wtgodbe added the blog-candidate Consider mentioning this in the release blog post label Aug 9, 2022
@ghost

ghost commented Aug 9, 2022

Copy link
Copy Markdown

@wtgodbe, this change will be considered for inclusion in the blog post for the release it'll ship in. Nice work!

Please ensure that the original comment in this thread contains a clear explanation of what the change does, why it's important (what problem does it solve?), and, if relevant, include things like code samples and/or performance numbers.

This content may not be exactly what goes into the blog post, but it will help the team putting together the announcement.

Thanks!

var options = CreateOptionsAccessor();
// Policy will disallow
var policy = new TestRateLimiterPolicy("myKey1", 404, false);
var defaultRateLimiterPolicy = new DefaultRateLimiterPolicy(RateLimiterOptions.ConvertPartitioner<string>(null, policy.GetPartition), policy.OnRejected);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'd prefer if this test used RequireRateLimiting and passed in TestRateLimiterPolicy instead of calling internal only code.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Will do this in a follow-up PR: #43298

@wtgodbe
wtgodbe merged commit 0438e7e into dotnet:main Aug 15, 2022
@wtgodbe
wtgodbe deleted the wtgodbe/RateLimit4 branch August 15, 2022 15:30
@ghost ghost added this to the 7.0-rc1 milestone Aug 15, 2022
@amcasey amcasey added area-middleware Includes: URL rewrite, redirect, response cache/compression, session, and other general middlewares and removed area-runtime labels Jun 6, 2023
@wtgodbe
wtgodbe restored the wtgodbe/RateLimit4 branch November 15, 2023 18:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-middleware Includes: URL rewrite, redirect, response cache/compression, session, and other general middlewares blog-candidate Consider mentioning this in the release blog post

Projects

None yet

Development

Successfully merging this pull request may close these issues.

API additions for RateLimitingMiddleware

6 participants