-
Notifications
You must be signed in to change notification settings - Fork 14
[identity] Add per-property partitioning to rate limiter #963
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
travlos
wants to merge
14
commits into
develop
Choose a base branch
from
feature/identity/rate-limit
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
d345ebb
[identity] Add per-property partitioning to rate limiter
travlos a26e754
Merge branch 'develop' into feature/identity/rate-limit
travlos 3fe5f90
Copilot code fixes
travlos f5a616e
Merge branch 'feature/identity/rate-limit' of https://github.com/indi…
travlos 8792948
Enhance rate limiter with flexible partition strategies
travlos db375a9
Apply suggestions from code review
travlos 581effa
Potential fix for pull request finding
travlos 15421a2
Merge branch 'develop' into feature/identity/rate-limit
travlos 6c3e5fc
Add support for multiple rate limiting policies per endpoint
travlos 07df33e
Refactor MultiRateLimiterServiceExtensions for clarity
travlos 4ded3fe
Add multi-policy rate limiting filters and attributes
travlos 7d92f12
Merge branch 'develop' into feature/identity/rate-limit
travlos 5b9fd0d
Merge branch 'develop' into feature/identity/rate-limit
travlos 1372926
Merge branch 'develop' into feature/identity/rate-limit
travlos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
src/Indice.AspNetCore/Features/MultiRateLimiter/MultiRateLimiterService.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| using Indice.AspNetCore.Configuration; | ||
| using Microsoft.AspNetCore.Http; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using System.Collections.Concurrent; | ||
| using System.Security.Claims; | ||
| using System.Threading.RateLimiting; | ||
|
|
||
| namespace Indice.AspNetCore.Features.MultiRateLimiter; | ||
|
|
||
| /// <summary> | ||
| /// Service that manages rate limiting policies and stores Rate Limiters in memory. | ||
| /// </summary> | ||
| public interface IMultiRateLimiterService | ||
| { | ||
| /// <summary> | ||
| /// Adds a rate limiting policy. | ||
| /// </summary> | ||
| /// <param name="policyName">The name of the policy.</param> | ||
| /// <param name="configurePolicy">Function that configures the rate limit partition for the policy.</param> | ||
| void AddPolicy(string policyName, Func<HttpContext, RateLimitPartition<string>> configurePolicy); | ||
|
|
||
| /// <summary> | ||
| /// Gets a rate limiting policy by name. | ||
| /// </summary> | ||
| /// <param name="policyName">The name of the policy.</param> | ||
| /// <returns>The policy function if found, otherwise null.</returns> | ||
| Func<HttpContext, RateLimitPartition<string>>? GetPolicy(string policyName); | ||
|
|
||
| /// <summary> | ||
| /// Gets all registered policy names. | ||
| /// </summary> | ||
| /// <returns>A read-only collection of policy names.</returns> | ||
| IReadOnlyCollection<string> GetPolicyNames(); | ||
|
|
||
| /// <summary> | ||
| /// Returns the Rate Limiter associated with a key. Creates it first if it doesn't exist. | ||
| /// </summary> | ||
| /// <param name="key">The identifier used to store the Rate Limiter.</param> | ||
| /// <param name="factory">Function that creates the Rate Limiter.</param> | ||
| public System.Threading.RateLimiting.RateLimiter GetOrCreateLimiter(string key, Func<System.Threading.RateLimiting.RateLimiter> factory); | ||
| } | ||
|
|
||
|
|
||
| /// <summary> | ||
| /// Service that manages rate limiting policies and stores Rate Limiters in memory. | ||
| /// </summary> | ||
| public class MultiRateLimiterService : IMultiRateLimiterService | ||
| { | ||
| private static readonly ConcurrentDictionary<string, System.Threading.RateLimiting.RateLimiter> _limiters = new(); | ||
| private readonly ConcurrentDictionary<string, Func<HttpContext, RateLimitPartition<string>>> _policies = new(); | ||
|
|
||
|
|
||
| /// <summary> | ||
| /// Helper extension to add policies to the service with a rule. | ||
| /// </summary> | ||
| public void AddPolicy(string policyName, RateLimiterEndpointRule rule, string userIdentifierClaimType = ClaimTypes.NameIdentifier) | ||
| { | ||
| AddPolicy(policyName, context => | ||
| { | ||
| if (!rule.CanLimitHttpMethod(context.Request.Method)) | ||
| { | ||
| return RateLimitPartition.GetNoLimiter("NoRateLimiting"); | ||
| } | ||
|
|
||
| return RateLimitPartition.GetFixedWindowLimiter( | ||
| partitionKey: RateLimiterExtensions.GetPartitionKey(context, userIdentifierClaimType, rule), | ||
| factory: _ => new FixedWindowRateLimiterOptions | ||
| { | ||
| PermitLimit = rule.PermitLimit ?? 10, | ||
| QueueLimit = rule.QueueLimit ?? 0, | ||
| QueueProcessingOrder = rule.QueueProcessingOrder ?? QueueProcessingOrder.OldestFirst, | ||
| Window = rule.Window ?? TimeSpan.FromMinutes(1), | ||
| AutoReplenishment = true | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Adds a rate limiting policy. | ||
| /// </summary> | ||
| /// <param name="policyName">The name of the policy.</param> | ||
| /// <param name="configurePolicy">Function that configures the rate limit partition for the policy.</param> | ||
| public void AddPolicy(string policyName, Func<HttpContext, RateLimitPartition<string>> configurePolicy) | ||
| { | ||
| _policies[policyName] = configurePolicy; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets a rate limiting policy by name. | ||
| /// </summary> | ||
| /// <param name="policyName">The name of the policy.</param> | ||
| /// <returns>The policy function if found, otherwise null.</returns> | ||
| public Func<HttpContext, RateLimitPartition<string>>? GetPolicy(string policyName) | ||
| { | ||
| return _policies.TryGetValue(policyName, out var policy) ? policy : null; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets all registered policy names. | ||
| /// </summary> | ||
| /// <returns>A read-only collection of policy names.</returns> | ||
| public IReadOnlyCollection<string> GetPolicyNames() | ||
| { | ||
| return _policies.Keys.ToList().AsReadOnly(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Returns the Rate Limiter associated with a key. Creates it first if it doesn't exist. | ||
| /// </summary> | ||
| /// <param name="key">The identifier used to store the Rate Limiter.</param> | ||
| /// <param name="factory">Function that creates the Rate Limiter.</param> | ||
| public System.Threading.RateLimiting.RateLimiter GetOrCreateLimiter(string key, Func<System.Threading.RateLimiting.RateLimiter> factory) | ||
| { | ||
| return _limiters.GetOrAdd(key, _ => factory()); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.