Skip to content

Commit

Permalink
Merge pull request #24 from Particular/ratecontrol
Browse files Browse the repository at this point in the history
Replaced High/Low/Pause with manualy control send rate with D / I keys
  • Loading branch information
WilliamBZA authored Aug 15, 2024
2 parents 4cd9290 + c130010 commit 4313d2d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 51 deletions.
16 changes: 10 additions & 6 deletions src/ClientUI/ConsoleBackgroundService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
Console.Clear();
await Console.Out.WriteLineAsync("""
Simulating customers placing orders on a website
Press T to toggle High/Low traffic mode
Press CTRL+C to quit
Simulating customers placing orders on a website
Press I to increate order rate
Press D to decrease order rate
Press CTRL+C to quit
""");
""");

simulatedCustomers.WriteState(Console.Out);

Expand All @@ -29,8 +30,11 @@ Press CTRL+C to quit

switch (input.Key)
{
case ConsoleKey.T:
simulatedCustomers.ToggleTrafficMode();
case ConsoleKey.I:
simulatedCustomers.IncreaseTraffic();
break;
case ConsoleKey.D:
simulatedCustomers.DecreaseTraffic();
break;
}
}
Expand Down
73 changes: 28 additions & 45 deletions src/ClientUI/SimulatedCustomers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,22 @@

class SimulatedCustomers(IBus _bus) : BackgroundService
{
enum TrafficModes
public void WriteState(TextWriter output)
{
Low = 0,
High = 1,
Paused = 2
output.WriteLine($"Sending {rate} orders / second");
}

public void WriteState(TextWriter output)
public void IncreaseTraffic()
{
output.WriteLine($"{highTrafficMode} traffic mode - sending {rate} orders / second");
rate++;
}

public void ToggleTrafficMode()
public void DecreaseTraffic()
{
highTrafficMode = (TrafficModes)(((int)highTrafficMode + 1) % 3);
rate = highTrafficMode switch
if (rate > 0)
{
TrafficModes.High => HighTrafficRate,
TrafficModes.Low => LowTrafficRate,
TrafficModes.Paused => 0,
_ => rate
};
rate--;
}
}

Task PlaceSingleOrder(CancellationToken cancellationToken)
Expand All @@ -40,35 +34,15 @@ Task PlaceSingleOrder(CancellationToken cancellationToken)

protected override async Task ExecuteAsync(CancellationToken cancellationToken = default)
{
nextReset = DateTime.UtcNow.AddSeconds(1);
currentIntervalCount = 0;

while (!cancellationToken.IsCancellationRequested)
{
var now = DateTime.UtcNow;
if (now > nextReset)
{
currentIntervalCount = 0;
nextReset = now.AddSeconds(1);
}

if (rate > 0)
{
await PlaceSingleOrder(cancellationToken);
}

currentIntervalCount++;

try
{
if (currentIntervalCount >= rate)
{
var delay = nextReset - DateTime.UtcNow;
if (delay > TimeSpan.Zero)
{
await Task.Delay(delay, cancellationToken);
}
}
await Task.WhenAll(
SendBatch(cancellationToken),
Task.Delay(1000, cancellationToken)
);

}
catch (TaskCanceledException)
{
Expand All @@ -77,12 +51,21 @@ protected override async Task ExecuteAsync(CancellationToken cancellationToken =
}
}

TrafficModes highTrafficMode;
async Task SendBatch(CancellationToken cancellationToken)
{
var x = rate;
if (rate > 0)
{
var tasks = new List<Task>(x);

DateTime nextReset;
int currentIntervalCount;
int rate = LowTrafficRate;
for (int i = 0; i < x; i++)
{
tasks.Add(PlaceSingleOrder(cancellationToken));
}

await Task.WhenAll(tasks);
}
}

const int HighTrafficRate = 8;
const int LowTrafficRate = 1;
int rate = 1;
}

0 comments on commit 4313d2d

Please sign in to comment.