-
Notifications
You must be signed in to change notification settings - Fork 14
feat: rate limiting configuration with IP-based partitioning #269
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
98f133c
feat: rate limiting configuration with IP-based partitioning
nanotaboada e5f80ea
chore: adjust runtime and development dependencies
nanotaboada 3e63fa0
chore: extract rate limiting settings to configuration
nanotaboada 2e72cdc
fix: correctly format fallback IP with Guid in rate limiter partition…
nanotaboada b63e916
Merge branch 'master' of github.com:nanotaboada/Dotnet.Samples.AspNet…
nanotaboada 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
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
src/Dotnet.Samples.AspNetCore.WebApi/Configurations/RateLimiterConfiguration.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,25 @@ | ||
| namespace Dotnet.Samples.AspNetCore.WebApi.Configurations; | ||
|
|
||
| /// <summary> | ||
| /// Configuration options for the Fixed Window Rate Limiter. | ||
| /// </summary> | ||
| public class RateLimiterConfiguration | ||
| { | ||
| /// <summary> | ||
| /// Gets or sets the maximum number of permits that can be leased per window. | ||
| /// Default value is 60 requests. | ||
| /// </summary> | ||
| public int PermitLimit { get; set; } = 60; | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the time window in seconds for rate limiting. | ||
| /// Default value is 60 seconds (1 minute). | ||
| /// </summary> | ||
| public int WindowSeconds { get; set; } = 60; | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the maximum number of requests that can be queued when the permit limit is exceeded. | ||
| /// A value of 0 means no queuing (default). | ||
| /// </summary> | ||
| public int QueueLimit { get; set; } = 0; | ||
| } |
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
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
47 changes: 47 additions & 0 deletions
47
src/Dotnet.Samples.AspNetCore.WebApi/Utilities/HttpContextUtilities.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,47 @@ | ||
| using System.Net; | ||
|
|
||
| namespace Dotnet.Samples.AspNetCore.WebApi.Utilities; | ||
|
|
||
| /// <summary> | ||
| /// Utility class for HTTP context operations. | ||
| /// </summary> | ||
| public static class HttpContextUtilities | ||
| { | ||
| /// <summary> | ||
| /// This method checks for the "X-Forwarded-For" and "X-Real-IP" headers, | ||
| /// which are commonly used by proxies to forward the original client IP address. | ||
| /// If these headers are not present or the IP address cannot be parsed, | ||
| /// it falls back to the remote IP address from the connection. | ||
| /// If no valid IP address can be determined, it returns "unknown". | ||
| /// </summary> | ||
| /// <param name="httpContext">The HTTP context.</param> | ||
| /// <returns>The client IP address or "unknown" if not available.</returns> | ||
| public static string ExtractIpAddress(HttpContext httpContext) | ||
| { | ||
| ArgumentNullException.ThrowIfNull(httpContext); | ||
|
|
||
| var headers = httpContext.Request.Headers; | ||
| IPAddress? ipAddress; | ||
|
|
||
| if (headers.TryGetValue("X-Forwarded-For", out var xForwardedFor)) | ||
| { | ||
| var clientIp = xForwardedFor | ||
| .ToString() | ||
| .Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries) | ||
| .FirstOrDefault(); | ||
|
|
||
| if (!string.IsNullOrWhiteSpace(clientIp) && IPAddress.TryParse(clientIp, out ipAddress)) | ||
| return ipAddress.ToString(); | ||
| } | ||
|
|
||
| if ( | ||
| headers.TryGetValue("X-Real-IP", out var xRealIp) | ||
| && IPAddress.TryParse(xRealIp.ToString(), out ipAddress) | ||
| ) | ||
| { | ||
| return ipAddress.ToString(); | ||
| } | ||
|
|
||
| return httpContext.Connection.RemoteIpAddress?.ToString() ?? "unknown-{Guid.NewGuid()}"; | ||
| } | ||
| } | ||
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
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
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.