Skip to content

Commit

Permalink
Added demo
Browse files Browse the repository at this point in the history
  • Loading branch information
ramonsmits committed Jul 16, 2023
1 parent 6794614 commit 2ed1e11
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
14 changes: 14 additions & 0 deletions src/Demo/Demo.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\NServiceBus.RateLimiter\NServiceBus.RateLimiter.csproj" />
</ItemGroup>

</Project>
39 changes: 39 additions & 0 deletions src/Demo/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using NServiceBus;

var cfg = new EndpointConfiguration("RateLimiterDemo");
cfg.UseTransport(new LearningTransport());
cfg.LimitMessageProcessingConcurrencyTo(100); // Increase concurrency to the transport will fetch many messages

cfg.ApplyRateLimiting(c =>
{
// One message every 250ms
c.Duration = TimeSpan.FromMilliseconds(250);
c.Limit = 1;
c.StartDurationThreshold = TimeSpan.FromSeconds(1);
});

var instance = await Endpoint.Start(cfg);

var tasks = new List<Task>();
for (int i = 0; i < 25; i++)
{
tasks.Add(instance.SendLocal(new MyMessage()));
}
await Task.WhenAll(tasks);

Console.ReadKey();

await instance.Stop();


class MyMessage : IMessage
{
}

class MyHandler : IHandleMessages<MyMessage>
{
public Task Handle(MyMessage message, IMessageHandlerContext context)
{
return Console.Out.WriteLineAsync("Received a message");
}
}
10 changes: 8 additions & 2 deletions src/NServiceBus.RateLimiter.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27004.2006
# Visual Studio Version 17
VisualStudioVersion = 17.7.33808.371
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NServiceBus.RateLimiter", "NServiceBus.RateLimiter\NServiceBus.RateLimiter.csproj", "{B7F783CF-EE17-4487-8516-BC135D3F4EA6}"
EndProject
Expand All @@ -10,6 +10,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
..\README.md = ..\README.md
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Demo", "Demo\Demo.csproj", "{57C3EF54-90F2-414B-A3C9-828453755F68}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -20,6 +22,10 @@ Global
{B7F783CF-EE17-4487-8516-BC135D3F4EA6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B7F783CF-EE17-4487-8516-BC135D3F4EA6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B7F783CF-EE17-4487-8516-BC135D3F4EA6}.Release|Any CPU.Build.0 = Release|Any CPU
{57C3EF54-90F2-414B-A3C9-828453755F68}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{57C3EF54-90F2-414B-A3C9-828453755F68}.Debug|Any CPU.Build.0 = Debug|Any CPU
{57C3EF54-90F2-414B-A3C9-828453755F68}.Release|Any CPU.ActiveCfg = Release|Any CPU
{57C3EF54-90F2-414B-A3C9-828453755F68}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit 2ed1e11

Please # to comment.