< Summary

Information
Class: Anichron.API.Infrastructure.SlidingWindowPolicy
Assembly: Anichron.API
File(s): /home/runner/work/anichron/anichron/src/Anichron.API/Infrastructure/RateLimiting.cs
Tag: 19_45211875487
Line coverage
100%
Covered lines: 12
Uncovered lines: 0
Coverable lines: 12
Total lines: 49
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
get_OnRejected()100%11100%
GetPartition(...)100%11100%

File(s)

/home/runner/work/anichron/anichron/src/Anichron.API/Infrastructure/RateLimiting.cs

#LineLine coverage
 1using Microsoft.AspNetCore.RateLimiting;
 2using System.Threading.RateLimiting;
 3using static System.Globalization.CultureInfo;
 4
 5namespace Anichron.API.Infrastructure;
 6
 7internal static class RateLimiting
 8{
 9    // RemoteIpAddress is populated by UseForwardedHeaders before this middleware runs.
 10    // Requests with no resolvable IP share a tight "unresolved" bucket to prevent IP-hiding abuse.
 11    internal static string PartitionKey(HttpContext httpContext)
 12        => httpContext.Connection.RemoteIpAddress?.ToString() ?? "unresolved";
 13
 14    internal static Task RejectAsync(HttpContext httpContext, TimeSpan retryAfter)
 15    {
 16        httpContext.Response.Headers.RetryAfter = ((int)retryAfter.TotalSeconds).ToString(InvariantCulture);
 17        return ApiProblems.Problem(
 18                StatusCodes.Status429TooManyRequests, ErrorCodes.RateLimited, ProblemMessages.TooManyRequests)
 19            .ExecuteAsync(httpContext);
 20    }
 21}
 22
 23internal static class AuthRateLimitPolicies
 24{
 25    internal const string Sensitive = "auth-sensitive";
 26    internal const string Refresh = "auth-refresh";
 27}
 28
 29/// <summary>A per-client sliding window whose rejection asks the client to wait one whole window.</summary>
 30/// <param name="permitLimit">The requests one client may make per window.</param>
 31/// <param name="window">The window.</param>
 32/// <param name="segmentsPerWindow">The segments the window slides by.</param>
 1833internal sealed class SlidingWindowPolicy(int permitLimit, TimeSpan window, int segmentsPerWindow) : IRateLimiterPolicy<
 34{
 35    public Func<OnRejectedContext, CancellationToken, ValueTask> OnRejected
 1836        => (context, _) => new ValueTask(RateLimiting.RejectAsync(context.HttpContext, window));
 37
 38    public RateLimitPartition<string> GetPartition(HttpContext httpContext)
 16939        => RateLimitPartition.GetSlidingWindowLimiter(
 16940            partitionKey: RateLimiting.PartitionKey(httpContext),
 16941            factory: _ => new SlidingWindowRateLimiterOptions
 16942            {
 16943                PermitLimit = permitLimit,
 16944                Window = window,
 16945                SegmentsPerWindow = segmentsPerWindow,
 16946                QueueProcessingOrder = QueueProcessingOrder.OldestFirst,
 16947                QueueLimit = 0,
 16948            });
 49}