Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This design uses a Rate Limiter Policy abstraction to represent a given rate-limiter implementation.
I borrowed the
/WeatherForecast
controller as a familiar example API endpoint to apply the rate limiter policies against.The
x-api-key
header acts as the access token for determining which rate limiter policies should be applied to a given request. TheApiKeyMiddleware
class contains hard-coded API key values corresponding to different policies. Note that it's possible to configure this such that a single API key can be subject to multiple rate limiter policies if desired.Since I took inspiration from the examples in the README, there is a "North American" policy that implements a sliding window rate limiter, as well as a "European" policy that implements a fixed window rate limiter. Unit tests for the rate limiter policies (implemented with
xUnit
) are stored in theRateLimiter.Tests
project, and ensure the policies behave in ways that are expected.The rate limiter is extendable: to add new rules, create a class that implements
IRateLimiterPolicy
and call it from theRateLimiterMiddleware
. Also, the rate limiter and in-memory cache settings are configured in theappsettings.json
. These values can be updated with minimal code changes.For convenience, you can use these GET requests to trigger the rate limiter policies: