Skip to content

Commit 3e63fa0

Browse files
committed
chore: extract rate limiting settings to configuration
1 parent e5f80ea commit 3e63fa0

File tree

5 files changed

+51
-6
lines changed

5 files changed

+51
-6
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
namespace Dotnet.Samples.AspNetCore.WebApi.Configurations;
2+
3+
/// <summary>
4+
/// Configuration options for the Fixed Window Rate Limiter.
5+
/// </summary>
6+
public class RateLimiterConfiguration
7+
{
8+
/// <summary>
9+
/// Gets or sets the maximum number of permits that can be leased per window.
10+
/// Default value is 60 requests.
11+
/// </summary>
12+
public int PermitLimit { get; set; } = 60;
13+
14+
/// <summary>
15+
/// Gets or sets the time window in seconds for rate limiting.
16+
/// Default value is 60 seconds (1 minute).
17+
/// </summary>
18+
public int WindowSeconds { get; set; } = 60;
19+
20+
/// <summary>
21+
/// Gets or sets the maximum number of requests that can be queued when the permit limit is exceeded.
22+
/// A value of 0 means no queuing (default).
23+
/// </summary>
24+
public int QueueLimit { get; set; } = 0;
25+
}

src/Dotnet.Samples.AspNetCore.WebApi/Extensions/ServiceCollectionExtensions.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,9 @@ IConfiguration configuration
105105
{
106106
services.AddSwaggerGen(options =>
107107
{
108-
options.SwaggerDoc("v1", configuration.GetSection("OpenApiInfo").Get<OpenApiInfo>());
108+
var openApiInfo = configuration.GetSection("OpenApiInfo").Get<OpenApiInfo>();
109+
110+
options.SwaggerDoc("v1", openApiInfo);
109111
options.IncludeXmlComments(SwaggerUtilities.ConfigureXmlCommentsFilePath());
110112
options.AddSecurityDefinition("Bearer", SwaggerUtilities.ConfigureSecurityDefinition());
111113
options.OperationFilter<AuthorizeCheckOperationFilter>();
@@ -159,9 +161,17 @@ public static IServiceCollection RegisterPlayerRepository(this IServiceCollectio
159161
/// <see href="https://learn.microsoft.com/en-us/aspnet/core/performance/rate-limit"/>
160162
/// </summary>
161163
/// <param name="services">The IServiceCollection instance.</param>
164+
/// <param name="configuration">The application configuration instance.</param>
162165
/// <returns>The IServiceCollection for method chaining.</returns>
163-
public static IServiceCollection AddFixedWindowRateLimiter(this IServiceCollection services)
166+
public static IServiceCollection AddFixedWindowRateLimiter(
167+
this IServiceCollection services,
168+
IConfiguration configuration
169+
)
164170
{
171+
var settings =
172+
configuration.GetSection("RateLimiter").Get<RateLimiterConfiguration>()
173+
?? new RateLimiterConfiguration();
174+
165175
services.AddRateLimiter(options =>
166176
{
167177
options.GlobalLimiter = PartitionedRateLimiter.Create<HttpContext, string>(
@@ -173,10 +183,10 @@ public static IServiceCollection AddFixedWindowRateLimiter(this IServiceCollecti
173183
partitionKey: partitionKey,
174184
factory: _ => new FixedWindowRateLimiterOptions
175185
{
176-
PermitLimit = 60,
177-
Window = TimeSpan.FromSeconds(60),
186+
PermitLimit = settings.PermitLimit,
187+
Window = TimeSpan.FromSeconds(settings.WindowSeconds),
178188
QueueProcessingOrder = QueueProcessingOrder.OldestFirst,
179-
QueueLimit = 0
189+
QueueLimit = settings.QueueLimit
180190
}
181191
);
182192
}

src/Dotnet.Samples.AspNetCore.WebApi/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
builder.Services.AddControllers();
2727
builder.Services.AddValidators();
2828
builder.Services.AddCorsDefaultPolicy(builder.Environment);
29-
builder.Services.AddFixedWindowRateLimiter();
29+
builder.Services.AddFixedWindowRateLimiter(builder.Configuration);
3030

3131
if (builder.Environment.IsDevelopment())
3232
{

src/Dotnet.Samples.AspNetCore.WebApi/appsettings.Development.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,10 @@
3333
"Name": "MIT License",
3434
"Url": "https://opensource.org/license/mit"
3535
}
36+
},
37+
"RateLimiter": {
38+
"PermitLimit": 60,
39+
"WindowSeconds": 60,
40+
"QueueLimit": 0
3641
}
3742
}

src/Dotnet.Samples.AspNetCore.WebApi/appsettings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,10 @@
3333
"Name": "MIT License",
3434
"Url": "https://opensource.org/license/mit"
3535
}
36+
},
37+
"RateLimiter": {
38+
"PermitLimit": 60,
39+
"WindowSeconds": 60,
40+
"QueueLimit": 0
3641
}
3742
}

0 commit comments

Comments
 (0)