Skip to content

Commit

Permalink
Simplified backpressure example
Browse files Browse the repository at this point in the history
  • Loading branch information
Emil Koutanov committed Jan 30, 2024
1 parent 6d09307 commit 074f410
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 12 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -263,4 +263,5 @@ FakesAssemblies/
*.opt

.idea
BenchmarkDotNet.Artifacts
BenchmarkDotNet.Artifacts
.DS_Store
16 changes: 5 additions & 11 deletions Examples/Backpressure/Backpressure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,31 +40,25 @@ public static async Task RunAsync()
const int MaxPending = 10;

var sinkActor = new SinkActor();
var pendingWork = new Dictionary<int, Task>();
var pendingWork = new List<Task>();
for (int i = 0; i < Messages; i++)
{
var workItem = new WorkItem(i);
await FreeCapacityAsync(pendingWork, MaxPending);
pendingWork[workItem.Id] = workItem.Completion.Task;
pendingWork.Add(workItem.Completion.Task);
Console.WriteLine("submitting work item {0}", workItem.Id);
sinkActor.Send(workItem);
}
await sinkActor.Drain();
}

private static async Task FreeCapacityAsync(Dictionary<int, Task> pendingWork, int maxPending)
private static async Task FreeCapacityAsync(List<Task> pendingWork, int maxPending)
{
if (pendingWork.Count == maxPending)
{
Console.WriteLine("waiting for backlog to subside");
await Task.WhenAny(pendingWork.Values);
foreach (var entry in pendingWork)
{
if (entry.Value.IsCompleted)
{
pendingWork.Remove(entry.Key);
}
}
await Task.WhenAny(pendingWork);
pendingWork.RemoveAll(task => task.IsCompleted);
Console.WriteLine("reduced to {0}", pendingWork.Count);
}
}
Expand Down

0 comments on commit 074f410

Please sign in to comment.