-
Notifications
You must be signed in to change notification settings - Fork 266
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Compile Include="Program.fs" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="../../src/Giraffe/Giraffe.fsproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
open System | ||
open Microsoft.AspNetCore.Http | ||
open Microsoft.AspNetCore.Builder | ||
open Microsoft.Extensions.DependencyInjection | ||
open Microsoft.Extensions.Hosting | ||
open Giraffe | ||
open Giraffe.EndpointRouting | ||
open Microsoft.AspNetCore.RateLimiting | ||
open System.Threading.RateLimiting | ||
|
||
let endpoints: list<Endpoint> = [ GET [ route "/" (text "Hello World") ] ] | ||
|
||
let notFoundHandler = text "Not Found" |> RequestErrors.notFound | ||
|
||
let configureApp (appBuilder: IApplicationBuilder) = | ||
appBuilder | ||
.UseRouting() | ||
.UseRateLimiter() | ||
.UseGiraffe(endpoints) | ||
.UseGiraffe(notFoundHandler) | ||
|
||
let configureServices (services: IServiceCollection) = | ||
// From https://blog.maartenballiauw.be/post/2022/09/26/aspnet-core-rate-limiting-middleware.html | ||
let configureRateLimiter (options: RateLimiterOptions) = | ||
options.RejectionStatusCode <- StatusCodes.Status429TooManyRequests | ||
|
||
options.GlobalLimiter <- | ||
PartitionedRateLimiter.Create<HttpContext, string>(fun httpContext -> | ||
RateLimitPartition.GetFixedWindowLimiter( | ||
partitionKey = httpContext.Request.Headers.Host.ToString(), | ||
factory = | ||
(fun _partition -> | ||
new FixedWindowRateLimiterOptions( | ||
AutoReplenishment = true, | ||
PermitLimit = 10, | ||
QueueLimit = 0, | ||
Window = TimeSpan.FromSeconds(1) | ||
) | ||
) | ||
) | ||
) | ||
|
||
services.AddRateLimiter(configureRateLimiter).AddRouting().AddGiraffe() | ||
|> ignore | ||
|
||
[<EntryPoint>] | ||
let main args = | ||
let builder = WebApplication.CreateBuilder(args) | ||
configureServices builder.Services | ||
|
||
let app = builder.Build() | ||
|
||
configureApp app | ||
app.Run() | ||
|
||
0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Global Rate Limiting Sample | ||
|
||
This sample project shows how one can configure ASP.NET's built-in [rate limiting middleware](https://learn.microsoft.com/en-us/aspnet/core/performance/rate-limit?view=aspnetcore-8.0). | ||
|
||
Notice that this rate limiting configuration is very simple, and for real life scenarios you'll need to figure out what is the best strategy to use for your server. | ||
|
||
To make it easier to test this project locally, and see the rate limiting middleware working, you can use the `global-rate-limiting-test.fsx` script: | ||
|
||
```bash | ||
# start the server | ||
dotnet run . | ||
# if you want to keep using the same terminal, just start this process in the background | ||
|
||
# then, you can use this script to test the server, and confirm that the rate-limiting | ||
# middleware is really working | ||
dotnet fsi global-rate-limiting-test.fsx | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
open System | ||
open System.IO | ||
open System.Net.Http | ||
|
||
let request = new HttpClient(BaseAddress = new Uri("http://localhost:5000")) | ||
|
||
#time | ||
|
||
seq { 1..100 } | ||
|> Seq.map (fun _ -> request.GetAsync "/" |> Async.AwaitTask) | ||
|> Async.Parallel | ||
|> Async.RunSynchronously | ||
|> Seq.iteri (fun i response -> | ||
printfn "\nResponse %i status code: %A" i response.StatusCode | ||
|
||
let responseReader = new StreamReader(response.Content.ReadAsStream()) | ||
printfn "Response %i content: %A" i (responseReader.ReadToEnd()) | ||
) | ||
|
||
#time |