stefanprodan / AspNetCoreRateLimit

ASP.NET Core rate limiting middleware

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to order rules so that more specific rules override more general rules

jslaybaugh opened this issue · comments

I have a more complicated setup, but I've simplified it for the purposes of this question.

Imagine that I want every endpoint to be limited to 5 per 20s. I configure as follows:

options.GeneralRules = new List<RateLimitRule>
{
	new RateLimitRule { Endpoint = "*", Period = "20s", Limit = 5 }
};

And that works fine. However, now I want a specific endpoint to be limited either more or less than that. I presumed I would either place it before or after it in the list. However, I cannot seem to get any combination of all/some of these to work. They all result in "Quota exceeded. Maximum allowed: 5 per 20s. Please try again in 11 second(s)." because it appears it is always applying the * rule:

options.GeneralRules = new List<RateLimitRule>
{
	new RateLimitRule { Endpoint = "get:foo/bar", 	Period = "20s", Limit = 1 },
	new RateLimitRule { Endpoint = "get:foo/bar", 	Period = "20s", Limit = 10 },
	new RateLimitRule { Endpoint = "*", 			Period = "20s", Limit = 5 },
	new RateLimitRule { Endpoint = "get:foo/bar", 	Period = "20s", Limit = 1 },
	new RateLimitRule { Endpoint = "get:foo/bar", 	Period = "20s", Limit = 10 },
};

How can I have a general catch-all rule, and then provide exceptions that are either more strict or more loose?

Anyone have any suggestions on how to accomplish this?