Skip to content

Commit

Permalink
change project to work only with .NET 8,
Browse files Browse the repository at this point in the history
add ASP.NET extensions configuration to the EndpointRouting mechanism,
update samples, making a distinction between GlobalRateLimiting and RateLimiting
  • Loading branch information
64J0 committed Oct 22, 2024
1 parent 07c66ed commit 489e937
Show file tree
Hide file tree
Showing 14 changed files with 349 additions and 126 deletions.
43 changes: 29 additions & 14 deletions Giraffe.sln
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "ResponseCachingApp", "sampl
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "NewtonsoftJson", "samples\NewtonsoftJson\NewtonsoftJson.fsproj", "{A08230F1-DA24-4059-A7F9-4743B36DD3E9}"
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "RateLimiting", "samples\RateLimiting\RateLimiting.fsproj", "{D5916FAA-2EBB-4FDD-B474-9DE37B60D641}"
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "GlobalRateLimiting", "samples\GlobalRateLimiting\GlobalRateLimiting.fsproj", "{5E88EAA6-E4C5-4577-9D66-DCAE61BBBEDC}"
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "RateLimiting", "samples\RateLimiting\RateLimiting.fsproj", "{6C27BCE1-7281-4385-BCC2-050BA6629B59}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -106,18 +108,30 @@ Global
{A08230F1-DA24-4059-A7F9-4743B36DD3E9}.Release|x64.Build.0 = Release|Any CPU
{A08230F1-DA24-4059-A7F9-4743B36DD3E9}.Release|x86.ActiveCfg = Release|Any CPU
{A08230F1-DA24-4059-A7F9-4743B36DD3E9}.Release|x86.Build.0 = Release|Any CPU
{D5916FAA-2EBB-4FDD-B474-9DE37B60D641}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D5916FAA-2EBB-4FDD-B474-9DE37B60D641}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D5916FAA-2EBB-4FDD-B474-9DE37B60D641}.Debug|x64.ActiveCfg = Debug|Any CPU
{D5916FAA-2EBB-4FDD-B474-9DE37B60D641}.Debug|x64.Build.0 = Debug|Any CPU
{D5916FAA-2EBB-4FDD-B474-9DE37B60D641}.Debug|x86.ActiveCfg = Debug|Any CPU
{D5916FAA-2EBB-4FDD-B474-9DE37B60D641}.Debug|x86.Build.0 = Debug|Any CPU
{D5916FAA-2EBB-4FDD-B474-9DE37B60D641}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D5916FAA-2EBB-4FDD-B474-9DE37B60D641}.Release|Any CPU.Build.0 = Release|Any CPU
{D5916FAA-2EBB-4FDD-B474-9DE37B60D641}.Release|x64.ActiveCfg = Release|Any CPU
{D5916FAA-2EBB-4FDD-B474-9DE37B60D641}.Release|x64.Build.0 = Release|Any CPU
{D5916FAA-2EBB-4FDD-B474-9DE37B60D641}.Release|x86.ActiveCfg = Release|Any CPU
{D5916FAA-2EBB-4FDD-B474-9DE37B60D641}.Release|x86.Build.0 = Release|Any CPU
{5E88EAA6-E4C5-4577-9D66-DCAE61BBBEDC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5E88EAA6-E4C5-4577-9D66-DCAE61BBBEDC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5E88EAA6-E4C5-4577-9D66-DCAE61BBBEDC}.Debug|x64.ActiveCfg = Debug|Any CPU
{5E88EAA6-E4C5-4577-9D66-DCAE61BBBEDC}.Debug|x64.Build.0 = Debug|Any CPU
{5E88EAA6-E4C5-4577-9D66-DCAE61BBBEDC}.Debug|x86.ActiveCfg = Debug|Any CPU
{5E88EAA6-E4C5-4577-9D66-DCAE61BBBEDC}.Debug|x86.Build.0 = Debug|Any CPU
{5E88EAA6-E4C5-4577-9D66-DCAE61BBBEDC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5E88EAA6-E4C5-4577-9D66-DCAE61BBBEDC}.Release|Any CPU.Build.0 = Release|Any CPU
{5E88EAA6-E4C5-4577-9D66-DCAE61BBBEDC}.Release|x64.ActiveCfg = Release|Any CPU
{5E88EAA6-E4C5-4577-9D66-DCAE61BBBEDC}.Release|x64.Build.0 = Release|Any CPU
{5E88EAA6-E4C5-4577-9D66-DCAE61BBBEDC}.Release|x86.ActiveCfg = Release|Any CPU
{5E88EAA6-E4C5-4577-9D66-DCAE61BBBEDC}.Release|x86.Build.0 = Release|Any CPU
{6C27BCE1-7281-4385-BCC2-050BA6629B59}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6C27BCE1-7281-4385-BCC2-050BA6629B59}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6C27BCE1-7281-4385-BCC2-050BA6629B59}.Debug|x64.ActiveCfg = Debug|Any CPU
{6C27BCE1-7281-4385-BCC2-050BA6629B59}.Debug|x64.Build.0 = Debug|Any CPU
{6C27BCE1-7281-4385-BCC2-050BA6629B59}.Debug|x86.ActiveCfg = Debug|Any CPU
{6C27BCE1-7281-4385-BCC2-050BA6629B59}.Debug|x86.Build.0 = Debug|Any CPU
{6C27BCE1-7281-4385-BCC2-050BA6629B59}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6C27BCE1-7281-4385-BCC2-050BA6629B59}.Release|Any CPU.Build.0 = Release|Any CPU
{6C27BCE1-7281-4385-BCC2-050BA6629B59}.Release|x64.ActiveCfg = Release|Any CPU
{6C27BCE1-7281-4385-BCC2-050BA6629B59}.Release|x64.Build.0 = Release|Any CPU
{6C27BCE1-7281-4385-BCC2-050BA6629B59}.Release|x86.ActiveCfg = Release|Any CPU
{6C27BCE1-7281-4385-BCC2-050BA6629B59}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -132,6 +146,7 @@ Global
{0E15F922-7A44-4116-9DAB-FAEB94392FEC} = {B9B26DDC-608C-42FE-9AB9-6CF0EE4920CD}
{FA102AC4-4608-42F9-86C1-1472B416A76E} = {9E6451FB-26E0-4AE4-A469-847F9602E999}
{A08230F1-DA24-4059-A7F9-4743B36DD3E9} = {9E6451FB-26E0-4AE4-A469-847F9602E999}
{D5916FAA-2EBB-4FDD-B474-9DE37B60D641} = {9E6451FB-26E0-4AE4-A469-847F9602E999}
{5E88EAA6-E4C5-4577-9D66-DCAE61BBBEDC} = {9E6451FB-26E0-4AE4-A469-847F9602E999}
{6C27BCE1-7281-4385-BCC2-050BA6629B59} = {9E6451FB-26E0-4AE4-A469-847F9602E999}
EndGlobalSection
EndGlobal
18 changes: 9 additions & 9 deletions samples/EndpointRoutingApp/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,20 @@ let handler3 (a: string, b: string, c: string, d: int) : HttpHandler =

let endpoints =
[
subRoute "/foo" [ GET [ route "/bar" (text "Aloha!") ] ]
subRoute "/foo" [] [ GET [ route "/bar" [] (text "Aloha!") ] ]
GET [
route "/" (text "Hello World")
routef "/%s/%i" handler2
routef "/%s/%s/%s/%i" handler3
route "/" [] (text "Hello World")
routef "/%s/%i" [] handler2
routef "/%s/%s/%s/%i" [] handler3
]
GET_HEAD [
route "/foo" (text "Bar")
route "/x" (text "y")
route "/abc" (text "def")
route "/123" (text "456")
route "/foo" [] (text "Bar")
route "/x" [] (text "y")
route "/abc" [] (text "def")
route "/123" [] (text "456")
]
// Not specifying a http verb means it will listen to all verbs
subRoute "/sub" [ route "/test" handler1 ]
subRoute "/sub" [] [ route "/test" [] handler1 ]
]

let notFoundHandler = "Not Found" |> text |> RequestErrors.notFound
Expand Down
16 changes: 16 additions & 0 deletions samples/GlobalRateLimiting/GlobalRateLimiting.fsproj
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>
56 changes: 56 additions & 0 deletions samples/GlobalRateLimiting/Program.fs
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
17 changes: 17 additions & 0 deletions samples/GlobalRateLimiting/README.md
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 `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 rate-limiting-test.fsx
```
20 changes: 20 additions & 0 deletions samples/GlobalRateLimiting/global-rate-limiting-test.fsx
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
2 changes: 1 addition & 1 deletion samples/NewtonsoftJson/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ module NewtonsoftJson =
type JsonResponse = { Foo: string; Bar: string; Age: int }

let endpoints: Endpoint list =
[ GET [ route "/json" (json { Foo = "john"; Bar = "doe"; Age = 30 }) ] ]
[ GET [ route "/json" [] (json { Foo = "john"; Bar = "doe"; Age = 30 }) ] ]

let notFoundHandler = "Not Found" |> text |> RequestErrors.notFound

Expand Down
42 changes: 23 additions & 19 deletions samples/RateLimiting/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@ open Giraffe.EndpointRouting
open Microsoft.AspNetCore.RateLimiting
open System.Threading.RateLimiting

let endpoints: list<Endpoint> = [ GET [ route "/" (text "Hello World") ] ]
let MY_RATE_LIMITER = "fixed"

let endpoints: list<Endpoint> =
[
GET [
route "/rate-limit" [ AspNetExtension.RateLimiting MY_RATE_LIMITER ] (text "Hello World")
route "/no-rate-limit" [] (text "Hello World: No Rate Limit!")
]
]

let notFoundHandler = text "Not Found" |> RequestErrors.notFound

Expand All @@ -20,25 +28,21 @@ let configureApp (appBuilder: IApplicationBuilder) =
.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)
)
)
// From https://learn.microsoft.com/en-us/aspnet/core/performance/rate-limit?view=aspnetcore-8.0#fixed-window-limiter
let configureRateLimiter (rateLimiterOptions: RateLimiterOptions) =
rateLimiterOptions.RejectionStatusCode <- StatusCodes.Status429TooManyRequests

rateLimiterOptions.AddFixedWindowLimiter(
policyName = MY_RATE_LIMITER,
configureOptions =
(fun (options: FixedWindowRateLimiterOptions) ->
options.PermitLimit <- 10
options.Window <- TimeSpan.FromSeconds 12
options.QueueProcessingOrder <- QueueProcessingOrder.OldestFirst
options.QueueLimit <- 1
)
)
)
|> ignore

services.AddRateLimiter(configureRateLimiter).AddRouting().AddGiraffe()
|> ignore
Expand Down
5 changes: 4 additions & 1 deletion samples/RateLimiting/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Rate Limiting Sample
# 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).

Expand All @@ -14,4 +14,7 @@ dotnet run .
# then, you can use this script to test the server, and confirm that the rate-limiting
# middleware is really working
dotnet fsi rate-limiting-test.fsx

# to run with the DEBUG flag active
dotnet fsi --define:DEBUG rate-limiting-test.fsx
```
31 changes: 30 additions & 1 deletion samples/RateLimiting/rate-limiting-test.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,43 @@ let request = new HttpClient(BaseAddress = new Uri("http://localhost:5000"))
#time

seq { 1..100 }
|> Seq.map (fun _ -> request.GetAsync "/" |> Async.AwaitTask)
|> Seq.map (fun _ -> request.GetAsync "/no-rate-limit" |> Async.AwaitTask)
|> Async.Parallel
|> Async.RunSynchronously
#if DEBUG
|> 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())
)
#else
|> Seq.groupBy (fun response -> response.StatusCode)
|> Seq.iter (fun (group) ->
let key, seqRes = group
printfn "Quantity of requests with status code %A: %i" (key) (Seq.length seqRes)
)
#endif

printfn "\nWith rate limit now...\n"

seq { 1..100 }
|> Seq.map (fun _ -> request.GetAsync "/rate-limit" |> Async.AwaitTask)
|> Async.Parallel
|> Async.RunSynchronously
#if DEBUG
|> 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())
)
#else
|> Seq.groupBy (fun response -> response.StatusCode)
|> Seq.iter (fun (group) ->
let key, seqRes = group
printfn "Quantity of requests with status code %A: %i\n" (key) (Seq.length seqRes)
)
#endif

#time
Loading

0 comments on commit 489e937

Please sign in to comment.