Created
February 1, 2026 12:41
-
-
Save wullemsb/0f18b28752d730aac487b57fa74b1dea to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| public class Order | |
| { | |
| public int Id { get; set; } | |
| public decimal Amount { get; set; } | |
| public string Priority { get; set; } | |
| public string Status { get; set; } | |
| } | |
| public class OrderProcessor | |
| { | |
| private readonly BufferBlock<Order> _orderSource; | |
| private readonly ActionBlock<Order> _highPriorityBlock; | |
| private readonly ActionBlock<Order> _largeOrderBlock; | |
| private readonly ActionBlock<Order> _standardOrderBlock; | |
| public OrderProcessor() | |
| { | |
| _orderSource = new BufferBlock<Order>(); | |
| _highPriorityBlock = new ActionBlock<Order>( | |
| order => ProcessHighPriority(order), | |
| new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = 4 }); | |
| _largeOrderBlock = new ActionBlock<Order>( | |
| order => ProcessLargeOrder(order), | |
| new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = 2 }); | |
| _standardOrderBlock = new ActionBlock<Order>( | |
| order => ProcessStandardOrder(order), | |
| new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = 8 }); | |
| SetupDataflow(); | |
| } | |
| private void SetupDataflow() | |
| { | |
| var linkOptions = new DataflowLinkOptions { PropagateCompletion = true }; | |
| // High priority orders go first | |
| _orderSource.LinkTo(_highPriorityBlock, linkOptions, | |
| order => order.Priority == "High"); | |
| // Large orders (>$10,000) that aren't high priority | |
| _orderSource.LinkTo(_largeOrderBlock, linkOptions, | |
| order => order.Amount > 10000); | |
| // Everything else goes to standard processing | |
| _orderSource.LinkTo(_standardOrderBlock, linkOptions); | |
| } | |
| private void ProcessHighPriority(Order order) | |
| { | |
| Console.WriteLine($"Processing high priority order {order.Id}"); | |
| // Fast-track processing logic | |
| } | |
| private void ProcessLargeOrder(Order order) | |
| { | |
| Console.WriteLine($"Processing large order {order.Id} (${order.Amount})"); | |
| // Additional verification for large orders | |
| } | |
| private void ProcessStandardOrder(Order order) | |
| { | |
| Console.WriteLine($"Processing standard order {order.Id}"); | |
| // Standard processing logic | |
| } | |
| public void SubmitOrder(Order order) | |
| { | |
| _orderSource.Post(order); | |
| } | |
| public async Task CompleteAsync() | |
| { | |
| _orderSource.Complete(); | |
| await Task.WhenAll( | |
| _highPriorityBlock.Completion, | |
| _largeOrderBlock.Completion, | |
| _standardOrderBlock.Completion); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment