-
Notifications
You must be signed in to change notification settings - Fork 10.6k
Add IRateLimiterStatisticsFeature with default implementation and tests #46028
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
Draft
MadL1me
wants to merge
8
commits into
dotnet:main
Choose a base branch
from
MadL1me:MadL1me/features/rate-limiter-statistics
base: main
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.
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b5e7191
Added IRateLimiterStatisticsFeatures with default impl and tests
MadL1me 34b5fd8
Review fixes: namespace removed, docs improved, allocated once
MadL1me 687b94f
Fix public API + nre dereference
MadL1me edeb9a5
fixed nre error
MadL1me 0ff6624
Changed to allocating on every http request
MadL1me 58a987f
Merge branch 'main' into MadL1me/features/rate-limiter-statistics
MadL1me 6836d80
Fix merge request
MadL1me 896aabc
fix nullable enable issue
MadL1me 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
28 changes: 28 additions & 0 deletions
28
src/Middleware/RateLimiting/src/DefaultRateLimiterStatisticsFeature.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,28 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Threading.RateLimiting; | ||
| using Microsoft.AspNetCore.Http; | ||
|
|
||
| namespace Microsoft.AspNetCore.RateLimiting; | ||
|
|
||
| internal sealed class DefaultRateLimiterStatisticsFeature : IRateLimiterStatisticsFeature | ||
| { | ||
| private readonly PartitionedRateLimiter<HttpContext>? _globalLimiter; | ||
| private readonly PartitionedRateLimiter<HttpContext> _endpointLimiter; | ||
| private readonly HttpContext _httpContext; | ||
|
|
||
| public DefaultRateLimiterStatisticsFeature( | ||
| PartitionedRateLimiter<HttpContext>? globalLimiter, | ||
| PartitionedRateLimiter<HttpContext> endpointLimiter, | ||
| HttpContext context) | ||
| { | ||
| _globalLimiter = globalLimiter; | ||
| _endpointLimiter = endpointLimiter; | ||
| _httpContext = context; | ||
| } | ||
|
|
||
| public RateLimiterStatistics? GetEndpointStatistics() => _endpointLimiter.GetStatistics(_httpContext); | ||
|
|
||
| public RateLimiterStatistics? GetGlobalStatistics() => _globalLimiter?.GetStatistics(_httpContext); | ||
| } |
29 changes: 29 additions & 0 deletions
29
src/Middleware/RateLimiting/src/IRateLimiterStatisticsFeature.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,29 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Threading.RateLimiting; | ||
| using Microsoft.AspNetCore.Http; | ||
|
|
||
| namespace Microsoft.AspNetCore.RateLimiting; | ||
|
|
||
| /// <summary> | ||
| /// An Interface which is used to represent <see cref="RateLimiter"/> statistics methods for global and endpoint limiters. | ||
| /// Obtained via <see cref="HttpContext.Features"/>. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// Requires <see cref="RateLimiterOptions.TrackStatistics"/> to be true. | ||
| /// </remarks> | ||
| public interface IRateLimiterStatisticsFeature | ||
| { | ||
| /// <summary> | ||
| /// Method to fetch <see cref="RateLimiterStatistics"/> for the global <see cref="PartitionedRateLimiter"/> | ||
| /// </summary> | ||
| /// <returns><see cref="RateLimiterStatistics"/> for the global <see cref="PartitionedRateLimiter"/>.</returns> | ||
| RateLimiterStatistics? GetGlobalStatistics(); | ||
| /// <summary> | ||
| /// Method to fetch <see cref="RateLimiterStatistics"/> for the endpoints <see cref="PartitionedRateLimiter"/> | ||
| /// </summary> | ||
| /// <returns><see cref="RateLimiterStatistics"/> for the endpoints <see cref="PartitionedRateLimiter"/>.</returns> | ||
| RateLimiterStatistics? GetEndpointStatistics(); | ||
|
|
||
| } |
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 |
|---|---|---|
| @@ -1 +1,6 @@ | ||
| #nullable enable | ||
| Microsoft.AspNetCore.RateLimiting.IRateLimiterStatisticsFeature | ||
| Microsoft.AspNetCore.RateLimiting.IRateLimiterStatisticsFeature.GetEndpointStatistics() -> System.Threading.RateLimiting.RateLimiterStatistics? | ||
| Microsoft.AspNetCore.RateLimiting.IRateLimiterStatisticsFeature.GetGlobalStatistics() -> System.Threading.RateLimiting.RateLimiterStatistics? | ||
| Microsoft.AspNetCore.RateLimiting.RateLimiterOptions.TrackStatistics.get -> bool | ||
| Microsoft.AspNetCore.RateLimiting.RateLimiterOptions.TrackStatistics.set -> void |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like we can allocate this once and reuse it