| | | 1 | | using CounterpointCollective.Collections; |
| | | 2 | | using CounterpointCollective.Dataflow.Encapsulation; |
| | | 3 | | using CounterpointCollective.Dataflow.Fluent; |
| | | 4 | | using CounterpointCollective.Dataflow.Notifying; |
| | | 5 | | using System.Threading.Tasks.Dataflow; |
| | | 6 | | |
| | | 7 | | namespace CounterpointCollective.Dataflow |
| | | 8 | | { |
| | | 9 | | public class OrderPreservingChoiceBlock<TInput, TOutput> : AbstractEncapsulatedPropagatorBlock<TInput, TOutput>, IRe |
| | | 10 | | { |
| | 684 | 11 | | private object Lock { get; } = new(); |
| | | 12 | | |
| | 1375 | 13 | | internal Branch<TInput, TOutput>? CurrentBranch { get; private set; } |
| | 431 | 14 | | internal Branch<TInput, TOutput> ThenBranch { get; private set; } |
| | 293 | 15 | | internal Branch<TInput, TOutput> ElseBranch { get; private set; } |
| | | 16 | | |
| | 393 | 17 | | protected override ITargetBlock<TInput> TargetSide => _inner; |
| | | 18 | | |
| | 1161 | 19 | | protected override ISourceBlock<TOutput> SourceSide => _inner; |
| | | 20 | | |
| | | 21 | | private readonly NonOrderPreservingChoiceBlock<TInput, TOutput> _inner; |
| | | 22 | | |
| | 1 | 23 | | public int Count => _inner.Count; |
| | | 24 | | |
| | 2 | 25 | | public int InputCount => _inner.InputCount; |
| | | 26 | | |
| | 3 | 27 | | public int OutputCount => _inner.OutputCount; |
| | | 28 | | |
| | | 29 | | public int BoundedCapacity |
| | | 30 | | { |
| | 0 | 31 | | get => _inner.BoundedCapacity; |
| | 0 | 32 | | set => _inner.BoundedCapacity = value; |
| | | 33 | | } |
| | | 34 | | |
| | 12 | 35 | | public OrderPreservingChoiceBlock( |
| | 12 | 36 | | Predicate<TInput> predicate, |
| | 12 | 37 | | IPropagatorBlock<TInput, TOutput> thenBlock, |
| | 12 | 38 | | IPropagatorBlock<TInput, TOutput> elseBlock, |
| | 12 | 39 | | ExecutionDataflowBlockOptions? options = null |
| | 12 | 40 | | ) |
| | | 41 | | { |
| | 12 | 42 | | options ??= new(); |
| | | 43 | | |
| | 12 | 44 | | var conditionCheckBlock = new GroupAdjacentBlock<TInput, bool, TInput>( |
| | 361 | 45 | | h => predicate(h), |
| | 361 | 46 | | h => h, |
| | 12 | 47 | | new() { CancellationToken = options.CancellationToken, SingleProducerConstrained = options.SingleProduce |
| | 12 | 48 | | flushOnIdle: true |
| | 12 | 49 | | ) |
| | 12 | 50 | | .Pipeline() |
| | 12 | 51 | | .TransformMany(g => |
| | 12 | 52 | | { |
| | 312 | 53 | | OnMessagesFanningOutTo(g.Key ? ThenBranch! : ElseBranch!, g.Count()); |
| | 673 | 54 | | return g.Select(e => (g.Key, e)); |
| | 12 | 55 | | }, new() { CancellationToken = options.CancellationToken, SingleProducerConstrained = true }) |
| | 12 | 56 | | .Build(); |
| | | 57 | | |
| | | 58 | | |
| | 12 | 59 | | ThenBranch = new(BranchName.Then, options.CancellationToken); |
| | 12 | 60 | | thenBlock = thenBlock.Pipeline() |
| | 12 | 61 | | .LinkTo(ThenBranch.Gate, new() { PropagateCompletion = true }) |
| | 12 | 62 | | .WithNotification(h => { |
| | 221 | 63 | | h.OnDeliveringMessages = count => OnMessagesFannedInFrom(ThenBranch, count); |
| | 24 | 64 | | h.ConfigureDispatching = q => q.UseSynchronousDispatch(); |
| | 12 | 65 | | }) |
| | 12 | 66 | | .Build(); |
| | | 67 | | |
| | | 68 | | |
| | 12 | 69 | | ElseBranch = new(BranchName.Else, options.CancellationToken); |
| | 12 | 70 | | elseBlock = elseBlock.Pipeline() |
| | 12 | 71 | | .LinkTo(ElseBranch.Gate, new() { PropagateCompletion = true }) |
| | 12 | 72 | | .WithNotification(h => { |
| | 163 | 73 | | h.OnDeliveringMessages = count => OnMessagesFannedInFrom(ElseBranch, count); |
| | 24 | 74 | | h.ConfigureDispatching = q => q.UseSynchronousDispatch(); |
| | 24 | 75 | | }).Build(); |
| | | 76 | | |
| | | 77 | | |
| | 12 | 78 | | _inner = new(conditionCheckBlock, thenBlock, elseBlock, options); |
| | 12 | 79 | | } |
| | | 80 | | |
| | 12 | 81 | | private readonly LinkedList<(Branch<TInput, TOutput> Branch, int RequiredDeliveryQuota)> _queue = []; |
| | | 82 | | |
| | | 83 | | private void OnMessagesFanningOutTo(Branch<TInput, TOutput> branch, int count) |
| | | 84 | | { |
| | 312 | 85 | | lock (Lock) |
| | | 86 | | { |
| | 312 | 87 | | CurrentBranch ??= branch; |
| | 312 | 88 | | if (CurrentBranch == branch && _queue.Count == 0) |
| | | 89 | | { |
| | 129 | 90 | | branch.GrantDeliveryQuota(count); |
| | | 91 | | } |
| | 183 | 92 | | else if (_queue.Last != null && _queue.Last.Value.Branch == branch) |
| | | 93 | | { |
| | 49 | 94 | | _queue.Last.Value = (branch, _queue.Last.Value.RequiredDeliveryQuota + count); |
| | | 95 | | } |
| | | 96 | | else |
| | | 97 | | { |
| | 134 | 98 | | _queue.AddLast((branch, count)); |
| | | 99 | | } |
| | 134 | 100 | | } |
| | 312 | 101 | | } |
| | | 102 | | |
| | | 103 | | private void OnMessagesFannedInFrom(Branch<TInput, TOutput> b, int count) |
| | | 104 | | { |
| | 360 | 105 | | lock (Lock) |
| | | 106 | | { |
| | 360 | 107 | | if (CurrentBranch != b) |
| | | 108 | | { |
| | 0 | 109 | | throw new InvalidOperationException($"not expecting messages from {b.Name}"); |
| | | 110 | | } |
| | 360 | 111 | | b.OutstandingDeliveries -= count; |
| | 360 | 112 | | if (b.OutstandingDeliveries == 0) |
| | | 113 | | { |
| | 262 | 114 | | var f = _queue.First; |
| | 262 | 115 | | if (f != null) |
| | | 116 | | { |
| | 134 | 117 | | CurrentBranch = f.Value.Branch; |
| | 134 | 118 | | f.Value.Branch.GrantDeliveryQuota(f.Value.RequiredDeliveryQuota); |
| | 134 | 119 | | _queue.RemoveFirst(); |
| | | 120 | | } |
| | | 121 | | else |
| | | 122 | | { |
| | 128 | 123 | | CurrentBranch = null; |
| | | 124 | | } |
| | | 125 | | } |
| | 226 | 126 | | } |
| | 360 | 127 | | } |
| | | 128 | | } |
| | | 129 | | internal enum BranchName { Then, Else }; |
| | | 130 | | |
| | | 131 | | internal record Branch<I, O> |
| | | 132 | | { |
| | | 133 | | public BranchName Name { get; } |
| | | 134 | | |
| | | 135 | | private readonly TokenGateBlock<O> _gate; |
| | | 136 | | public IPropagatorBlock<O, O> Gate { get; } |
| | | 137 | | |
| | | 138 | | public int Tokens => _gate.Tokens; |
| | | 139 | | |
| | | 140 | | public Branch(BranchName name, CancellationToken cancellationToken) |
| | | 141 | | { |
| | | 142 | | Name = name; |
| | | 143 | | |
| | | 144 | | var b = new BufferBlock<O>(new() { CancellationToken = cancellationToken }); |
| | | 145 | | _gate = new TokenGateBlock<O>(b); |
| | | 146 | | Gate = DataflowBlock.Encapsulate(_gate,b); |
| | | 147 | | } |
| | | 148 | | |
| | | 149 | | public void GrantDeliveryQuota(int c) |
| | | 150 | | { |
| | | 151 | | OutstandingDeliveries += c; |
| | | 152 | | _gate.Allow(c); |
| | | 153 | | } |
| | | 154 | | |
| | | 155 | | public int OutstandingDeliveries { get; set; } |
| | | 156 | | }; |
| | | 157 | | } |