< Summary

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

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
PartitionKey(...)100%44100%
RejectAsync(...)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)
 17112        => httpContext.Connection.RemoteIpAddress?.ToString() ?? "unresolved";
 13
 14    internal static Task RejectAsync(HttpContext httpContext, TimeSpan retryAfter)
 615    {
 616        httpContext.Response.Headers.RetryAfter = ((int)retryAfter.TotalSeconds).ToString(InvariantCulture);
 617        return ApiProblems.Problem(
 618                StatusCodes.Status429TooManyRequests, ErrorCodes.RateLimited, ProblemMessages.TooManyRequests)
 619            .ExecuteAsync(httpContext);
 620    }
 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>
 33internal sealed class SlidingWindowPolicy(int permitLimit, TimeSpan window, int segmentsPerWindow) : IRateLimiterPolicy<
 34{
 35    public Func<OnRejectedContext, CancellationToken, ValueTask> OnRejected
 36        => (context, _) => new ValueTask(RateLimiting.RejectAsync(context.HttpContext, window));
 37
 38    public RateLimitPartition<string> GetPartition(HttpContext httpContext)
 39        => RateLimitPartition.GetSlidingWindowLimiter(
 40            partitionKey: RateLimiting.PartitionKey(httpContext),
 41            factory: _ => new SlidingWindowRateLimiterOptions
 42            {
 43                PermitLimit = permitLimit,
 44                Window = window,
 45                SegmentsPerWindow = segmentsPerWindow,
 46                QueueProcessingOrder = QueueProcessingOrder.OldestFirst,
 47                QueueLimit = 0,
 48            });
 49}