-
Notifications
You must be signed in to change notification settings - Fork 1.7k
#2138 Introduce ASP.NET rate limiting #2188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from 12 commits
c7c4c6b
b6c1577
e6359cb
39a4934
2833359
852fac1
7dbb578
cd3075a
bbe1931
032e7f2
e8987ad
d5e3a44
f6274bb
2c01ab1
e5555ed
fce8f5b
be8d67b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net8.0</TargetFramework> | ||
| <Nullable>enable</Nullable> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\..\src\Ocelot\Ocelot.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| @RateLimiterSample_HostAddress = http://localhost:5202 | ||
|
|
||
| GET {{RateLimiterSample_HostAddress}}/laura | ||
| Accept: application/json | ||
|
|
||
| ### | ||
|
|
||
| GET {{RateLimiterSample_HostAddress}}/tom | ||
| Accept: application/json | ||
|
|
||
| ### |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| using Microsoft.AspNetCore.RateLimiting; | ||
| using Ocelot.DependencyInjection; | ||
| using Ocelot.Middleware; | ||
| using System.Threading.RateLimiting; | ||
|
|
||
| var builder = WebApplication.CreateBuilder(args); | ||
| builder.Configuration.AddJsonFile("ocelot.json"); | ||
| builder.Services.AddOcelot(); | ||
|
|
||
| builder.Services.AddRateLimiter(op => | ||
| { | ||
| op.AddFixedWindowLimiter(policyName: "fixed", options => | ||
| { | ||
| options.PermitLimit = 2; | ||
| options.Window = TimeSpan.FromSeconds(12); | ||
| options.QueueProcessingOrder = QueueProcessingOrder.OldestFirst; | ||
| options.QueueLimit = 0; | ||
| }); | ||
| }); | ||
|
Comment on lines
+10
to
+19
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I disagree! In this case, we haven't proposed anything new.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @raman-m it's basically what i mentioned:
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can't mix policies, but we can define as many as needed, right?
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it's global and they can't be modified. |
||
|
|
||
| var app = builder.Build(); | ||
| app.UseHttpsRedirection(); | ||
|
|
||
| await app.UseOcelot(); | ||
|
|
||
| app.Run(); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| { | ||
| "$schema": "http://json.schemastore.org/launchsettings.json", | ||
| "iisSettings": { | ||
| "windowsAuthentication": false, | ||
| "anonymousAuthentication": true, | ||
| "iisExpress": { | ||
| "applicationUrl": "http://localhost:12083", | ||
| "sslPort": 44358 | ||
| } | ||
| }, | ||
| "profiles": { | ||
| "http": { | ||
| "commandName": "Project", | ||
| "dotnetRunMessages": true, | ||
| "launchBrowser": true, | ||
| "launchUrl": "weatherforecast", | ||
| "applicationUrl": "http://localhost:5202", | ||
| "environmentVariables": { | ||
| "ASPNETCORE_ENVIRONMENT": "Development" | ||
| } | ||
| }, | ||
| "https": { | ||
| "commandName": "Project", | ||
| "dotnetRunMessages": true, | ||
| "launchBrowser": true, | ||
| "launchUrl": "weatherforecast", | ||
| "applicationUrl": "https://localhost:7116;http://localhost:5202", | ||
| "environmentVariables": { | ||
| "ASPNETCORE_ENVIRONMENT": "Development" | ||
| } | ||
| }, | ||
| "IIS Express": { | ||
| "commandName": "IISExpress", | ||
| "launchBrowser": true, | ||
| "launchUrl": "weatherforecast", | ||
| "environmentVariables": { | ||
| "ASPNETCORE_ENVIRONMENT": "Development" | ||
| } | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| { | ||
| "Logging": { | ||
| "LogLevel": { | ||
| "Default": "Information", | ||
| "Microsoft.AspNetCore": "Warning" | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| { | ||
| "Logging": { | ||
| "LogLevel": { | ||
| "Default": "Information", | ||
| "Microsoft.AspNetCore": "Warning" | ||
| } | ||
| }, | ||
| "AllowedHosts": "*" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| { | ||
| "Routes": [ | ||
| { | ||
| "UpstreamHttpMethod": [ "Get" ], | ||
| "UpstreamPathTemplate": "/laura", | ||
| "DownstreamPathTemplate": "/fact", | ||
| "DownstreamScheme": "https", | ||
| "DownstreamHostAndPorts": [ | ||
| { "Host": "catfact.ninja", "Port": 443 } | ||
| ], | ||
| "Key": "Laura", | ||
| "RateLimitOptions": { | ||
| "EnableRateLimiting": true, | ||
| "Period": "5s", | ||
| "PeriodTimespan": 1, | ||
| "Limit": 1 | ||
| } | ||
| }, | ||
| { | ||
| "UpstreamHttpMethod": [ "Get" ], | ||
| "UpstreamPathTemplate": "/tom", | ||
| "DownstreamPathTemplate": "/fact", | ||
| "DownstreamScheme": "https", | ||
| "DownstreamHostAndPorts": [ | ||
| { "Host": "catfact.ninja", "Port": 443 } | ||
| ], | ||
| "Key": "Tom", | ||
| "RateLimitOptions": { | ||
| "EnableRateLimiting": true, | ||
| "Policy": "fixed" | ||
| } | ||
| } | ||
| ], | ||
| "Aggregates": [ | ||
| { | ||
| "UpstreamPathTemplate": "/", | ||
| "RouteKeys": [ "Tom", "Laura" ] | ||
| } | ||
| ], | ||
| "GlobalConfiguration": { | ||
| "RateLimitOptions": { | ||
| "QuotaExceededMessage": "Customize Tips!", | ||
| "HttpStatusCode": 418 // I'm a teapot | ||
| } | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.