Commit cd1acd9b by ruyun.zhang

并发限制

parent 40851855
using AspNetCoreRateLimit;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace Performance.Api.Configurations
{
public static class RateLimitConfig
{
public static IServiceCollection AddCustomRateLimit(this IServiceCollection services, IConfiguration configuration)
{
services.Configure<IpRateLimitOptions>(configuration.GetSection("IpRateLimiting"));
services.Configure<IpRateLimitPolicies>(configuration.GetSection("IpRateLimitPolicies"));
services.AddInMemoryRateLimiting();
services.AddSingleton<IRateLimitConfiguration, RateLimitConfiguration>();
return services;
}
}
}
......@@ -35,7 +35,7 @@ public static void Main(string[] args)
var env = context.HostingEnvironment;
config.AddJsonFile("appsettings.json", true, true);
config.AddJsonFile($"appsettings.{env.EnvironmentName}.json", true, true);
config.AddJsonFile($"RateLimitConfig.json", true, true);
config.AddJsonFile("limit.json", true, true);
})
.UseUrls("http://*:5001")
.UseStartup<Startup>()
......
......@@ -2,6 +2,7 @@
using System.Globalization;
using System.Reflection;
using System.Text;
using AspNetCoreRateLimit;
using FluentScheduler;
using FluentValidation;
using FluentValidation.AspNetCore;
......@@ -84,6 +85,7 @@ public void ConfigureServices(IServiceCollection services)
// service repository
services.AddDependencyInjectionConfiguration();
services.AddCustomRateLimit(Configuration);
// signalr
services.AddSignalR(hubOptions =>
......@@ -119,8 +121,8 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseStatusCodePagesWithReExecute("/error/{0}");
}
app.UseExpirationLimit();
app.UseIpRateLimiting();
app.UseRouting();
......
{
"IpRateLimiting": {
"EnableEndpointRateLimiting": true,
"StackBlockedRequests": false,
"RealIpHeader": "X-Real-IP",
"ClientIdHeader": "X-ClientId",
"HttpStatusCode": 429,
// 本地测试需要取消白名单IP
"IpWhitelist": [ "127.0.0.1", "::1/10", "192.168.0.0/24" ],
"EndpointWhitelist": [ "get:/api/license" ],
"ClientWhitelist": [ "dev-id-1", "dev-id-2" ],
"QuotaExceededResponse": {
"Content": "{{ \"code\":429, \"message\": \"您访问过于频繁,请稍后重试...\"}}",
"ContentType": "application/json",
"StatusCode": 429
},
"GeneralRules": [
{
"Endpoint": "post:/api/assess/category/issue/confirm",
"Period": "1s",
"Limit": 1
},
{
"Endpoint": "post:*",
"Period": "1s",
"Limit": 10
},
{
"Endpoint": "get:*",
"Period": "1s",
"Limit": 100
},
{
"Endpoint": "*",
"Period": "15m",
"Limit": 100
},
{
"Endpoint": "*",
"Period": "12h",
"Limit": 1000
},
{
"Endpoint": "*",
"Period": "7d",
"Limit": 10000
}
]
}
}
\ No newline at end of file
......@@ -24,6 +24,7 @@
<PackageReference Include="NPOI" Version="2.5.5" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="6.2.2" />
<PackageReference Include="AspNetCoreRateLimit" Version="4.0.2" />
</ItemGroup>
<ItemGroup>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment