• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Sholtee / warehouse / 122

15 Feb 2025 10:14AM UTC coverage: 92.342% (+0.06%) from 92.283%
122

push

appveyor

Sholtee
yep, another AppVeyor hack

1459 of 1580 relevant lines covered (92.34%)

0.92 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.72
/SRC/App/Warehouse.Host/Infrastructure/Registrations/Registrations.RateLimiting.cs
1
/********************************************************************************
2
* Registrations.RateLimiting.cs                                                 *
3
*                                                                               *
4
* Author: Denes Solti                                                           *
5
* Project: Warehouse API (boilerplate)                                          *
6
* License: MIT                                                                  *
7
********************************************************************************/
8
using System.Threading.RateLimiting;
9

10
using Microsoft.AspNetCore.Builder;
11
using Microsoft.AspNetCore.Http;
12
using Microsoft.Extensions.Configuration;
13
using Microsoft.Extensions.DependencyInjection;
14
using Microsoft.Extensions.Logging;
15
using RedisRateLimiting;
16
using StackExchange.Redis;
17

18
namespace Warehouse.Host.Infrastructure.Registrations
19
{
20
    using Core.Exceptions;
21

22
    internal static partial class Registrations
23
    {
24
        public static IServiceCollection AddRateLimiter(this IServiceCollection services) => services.AddRedis().AddRateLimiter(static opts =>
1✔
25
        {
1✔
26
            opts
1✔
27
                .AddPolicy("fixed", static httpContext =>
1✔
28
                {
1✔
29
                    //
1✔
30
                    // These are global singletons so it's safe to capture them in the factory function
1✔
31
                    //
1✔
32

1✔
33
                    IConfiguration configuration = httpContext.RequestServices.GetRequiredService<IConfiguration>();
1✔
34
                    IConnectionMultiplexer connectionMultiplexer = httpContext.RequestServices.GetRequiredService<IConnectionMultiplexer>();
1✔
35

1✔
36
                    return RedisRateLimitPartition.GetFixedWindowRateLimiter("fixed", _ =>
1✔
37
                    {
1✔
38
                        RedisFixedWindowRateLimiterOptions opts = new();
1✔
39
                        configuration.GetRequiredSection("RateLimiting:Fixed").Bind(opts);
1✔
40

1✔
41
                        //
1✔
42
                        // Despite the factory pattern the library never frees the created multiplexer instances
1✔
43
                        // https://github.com/cristipufu/aspnetcore-redis-rate-limiting/issues/200
1✔
44
                        //
1✔
45

1✔
46
                        opts.ConnectionMultiplexerFactory = () => connectionMultiplexer;
1✔
47
                        return opts;
1✔
48
                    });
1✔
49
                })
1✔
50
                .AddPolicy("userBound", static httpContext =>
1✔
51
                {
1✔
52
                    string? userId = httpContext.User.Identity?.Name;
×
53

1✔
54
                    IConfiguration configuration = httpContext.RequestServices.GetRequiredService<IConfiguration>();
1✔
55
                    IConnectionMultiplexer connectionMultiplexer = httpContext.RequestServices.GetRequiredService<IConnectionMultiplexer>();
1✔
56

1✔
57
                    return string.IsNullOrEmpty(userId)
1✔
58
                        ? GetLimiter("anon", "Anon")
1✔
59
                        : GetLimiter(userId, "UserBound");
1✔
60

1✔
61
                    RateLimitPartition<string> GetLimiter(string partitionKey, string configKey) => RedisRateLimitPartition.GetTokenBucketRateLimiter(partitionKey, _ =>
1✔
62
                    {
1✔
63
                        RedisTokenBucketRateLimiterOptions opts = new();
1✔
64
                        configuration.GetRequiredSection($"RateLimiting:{configKey}").Bind(opts);
1✔
65
                        opts.ConnectionMultiplexerFactory = () => connectionMultiplexer;
1✔
66
                        return opts;
1✔
67
                    });
1✔
68
                });
1✔
69

1✔
70
            opts.OnRejected = static (context, _) =>
1✔
71
            {
1✔
72
                context
×
73
                    .HttpContext
×
74
                    .RequestServices
×
75
                    .GetRequiredService<ILoggerFactory>()
×
76
                    .CreateLogger("RateLimiter")
×
77
                    .LogWarning("Too many requests on endpoint: {endpoint}", context.HttpContext.GetEndpoint()?.DisplayName);
×
78
                throw new TooManyRequestsException();
1✔
79
            };
1✔
80
        });
1✔
81
    }
82
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc