| | | 1 | | using CounterpointCollective.Dataflow.Internal; |
| | | 2 | | using System.Diagnostics; |
| | | 3 | | using System.Runtime.ExceptionServices; |
| | | 4 | | using System.Threading.Tasks; |
| | | 5 | | using System.Threading.Tasks.Dataflow; |
| | | 6 | | |
| | | 7 | | namespace CounterpointCollective.Dataflow |
| | | 8 | | { |
| | | 9 | | /// <exclude /> |
| | | 10 | | public static class ISourceBlockExtensions |
| | | 11 | | { |
| | | 12 | | public static SynchronousFilterBlock<T> Filter<T>( |
| | | 13 | | this ISourceBlock<T> source, |
| | | 14 | | Predicate<T> pred, |
| | | 15 | | FilterMode mode = FilterMode.Block |
| | | 16 | | ) |
| | 0 | 17 | | => new(source, pred, mode); |
| | | 18 | | |
| | | 19 | | public static ISourceBlock<O> Transform<I, O>(this ISourceBlock<I> source, Func<I, O> f) => |
| | 0 | 20 | | Transform(source, f, new()); |
| | | 21 | | |
| | | 22 | | public static ISourceBlock<O> Transform<I, O>( |
| | | 23 | | this ISourceBlock<I> source, |
| | | 24 | | Func<I, Task<O>> f |
| | 0 | 25 | | ) => Transform(source, f, new()); |
| | | 26 | | |
| | | 27 | | public static TransformBlock<I,O> Transform<I, O>( |
| | | 28 | | this ISourceBlock<I> source, |
| | | 29 | | Func<I, O> f, |
| | | 30 | | ExecutionDataflowBlockOptions opts |
| | | 31 | | ) |
| | | 32 | | { |
| | 1 | 33 | | var t = new TransformBlock<I, O>(f, opts); |
| | 1 | 34 | | _ = source.LinkTo(t, new DataflowLinkOptions() { PropagateCompletion = true }); |
| | 1 | 35 | | return t; |
| | | 36 | | } |
| | | 37 | | |
| | | 38 | | public static ISourceBlock<O> Transform<I, O>( |
| | | 39 | | this ISourceBlock<I> source, |
| | | 40 | | Func<I, Task<O>> f, |
| | | 41 | | ExecutionDataflowBlockOptions opts |
| | | 42 | | ) |
| | | 43 | | { |
| | 0 | 44 | | var t = new TransformBlock<I, O>(f, opts); |
| | 0 | 45 | | _ = source.LinkTo(t, new DataflowLinkOptions() { PropagateCompletion = true }); |
| | 0 | 46 | | return t; |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | public static ISourceBlock<T> Flatten<T> ( |
| | | 50 | | this ISourceBlock<ISourceBlock<T>> sources, |
| | | 51 | | ExecutionDataflowBlockOptions? options = null |
| | | 52 | | ) |
| | | 53 | | { |
| | 1 | 54 | | options ??= new(); |
| | 1 | 55 | | if (options.EnsureOrdered) |
| | | 56 | | { |
| | 1 | 57 | | options.MaxDegreeOfParallelism = 1; |
| | | 58 | | } |
| | | 59 | | |
| | 1 | 60 | | BufferBlock<T> b = new(options); |
| | 1 | 61 | | var a = sources.Action(async s => { |
| | 2048 | 62 | | using var _ = s.LinkTo(b); |
| | 2048 | 63 | | await s.Completion; |
| | 2048 | 64 | | }, new ExecutionDataflowBlockOptions |
| | 1 | 65 | | { |
| | 1 | 66 | | EnsureOrdered = options.EnsureOrdered, |
| | 1 | 67 | | CancellationToken = options.CancellationToken, |
| | 1 | 68 | | SingleProducerConstrained = true, |
| | 1 | 69 | | BoundedCapacity = options.BoundedCapacity, |
| | 1 | 70 | | MaxDegreeOfParallelism = options.EnsureOrdered ? 1 : options.MaxDegreeOfParallelism |
| | 1 | 71 | | }); |
| | | 72 | | |
| | 1 | 73 | | _ = a.PropagateCompletion(b); |
| | | 74 | | |
| | 1 | 75 | | return b; |
| | | 76 | | } |
| | | 77 | | |
| | | 78 | | [Obsolete("Use Transform and Flatten instead")] |
| | | 79 | | public static ISourceBlock<O> TransformMany<I, O>( |
| | | 80 | | this ISourceBlock<I> source, |
| | | 81 | | Func<I, Task<ISourceBlock<O>>> f, |
| | | 82 | | ExecutionDataflowBlockOptions optsForProcessing, |
| | | 83 | | DataflowBlockOptions optsForBuffer |
| | | 84 | | ) |
| | | 85 | | { |
| | 0 | 86 | | if (optsForProcessing.EnsureOrdered) |
| | | 87 | | { |
| | 0 | 88 | | optsForProcessing.MaxDegreeOfParallelism = 1; |
| | | 89 | | } |
| | 0 | 90 | | BufferBlock<O> b = new(optsForBuffer); |
| | 0 | 91 | | var a = source |
| | 0 | 92 | | .Action(async msg => |
| | 0 | 93 | | { |
| | 0 | 94 | | var q = await f(msg); |
| | 0 | 95 | | _ = q.LinkTo(b); |
| | 0 | 96 | | await q.Completion; |
| | 0 | 97 | | }, |
| | 0 | 98 | | optsForProcessing |
| | 0 | 99 | | ); |
| | | 100 | | |
| | 0 | 101 | | _ = a.PropagateCompletion(b); |
| | | 102 | | |
| | 0 | 103 | | return b; |
| | | 104 | | } |
| | | 105 | | |
| | | 106 | | public static TransformManyBlock<I,O> TransformMany<I, O>( |
| | | 107 | | this ISourceBlock<I> source, |
| | | 108 | | Func<I, Task<IEnumerable<O>>> f, |
| | | 109 | | ExecutionDataflowBlockOptions dataflowBlockOptions |
| | | 110 | | ) |
| | | 111 | | { |
| | 0 | 112 | | TransformManyBlock<I, O> res = new(f, dataflowBlockOptions); |
| | 0 | 113 | | _ = source.LinkTo(res, new DataflowLinkOptions { PropagateCompletion = true }); |
| | 0 | 114 | | return res; |
| | | 115 | | } |
| | | 116 | | |
| | | 117 | | public static ISourceBlock<O> TransformMany<I, O>( |
| | | 118 | | this ISourceBlock<I> source, |
| | | 119 | | Func<I, IEnumerable<O>> f, |
| | | 120 | | ExecutionDataflowBlockOptions dataflowBlockOptions |
| | | 121 | | ) |
| | | 122 | | { |
| | 0 | 123 | | TransformManyBlock<I, O> res = new(f, dataflowBlockOptions); |
| | 0 | 124 | | _ = source.LinkTo(res, new DataflowLinkOptions { PropagateCompletion = true }); |
| | 0 | 125 | | return res; |
| | | 126 | | } |
| | | 127 | | |
| | | 128 | | public static TransformManyBlock<I, O> TransformMany<I, O>( |
| | | 129 | | this ISourceBlock<I> source, |
| | | 130 | | Func<I, IAsyncEnumerable<O>> f, |
| | | 131 | | ExecutionDataflowBlockOptions dataflowBlockOptions |
| | | 132 | | ) |
| | | 133 | | { |
| | 0 | 134 | | TransformManyBlock<I, O> res = new(f, dataflowBlockOptions); |
| | 0 | 135 | | _ = source.LinkTo(res, new DataflowLinkOptions { PropagateCompletion = true }); |
| | 0 | 136 | | return res; |
| | | 137 | | } |
| | | 138 | | |
| | | 139 | | public static ISourceBlock<IGrouping<K, V>> GroupAdjacent<T, K, V>( |
| | | 140 | | this ISourceBlock<T> source, |
| | | 141 | | Func<T, K> keySelector, |
| | | 142 | | Func<T, V> valueSelector, |
| | | 143 | | ExecutionDataflowBlockOptions options |
| | | 144 | | ) |
| | | 145 | | { |
| | 2 | 146 | | var res = new GroupAdjacentBlock<T, K, V>(keySelector, valueSelector, options); |
| | 2 | 147 | | _ = source.LinkTo(res, new DataflowLinkOptions { PropagateCompletion = true }); |
| | 2 | 148 | | return res; |
| | | 149 | | } |
| | | 150 | | |
| | | 151 | | public static BatchBlock<T> Batch<T>(this ISourceBlock<T> source, int batchSize) => |
| | 1 | 152 | | Batch(source, batchSize, new()); |
| | | 153 | | |
| | | 154 | | public static BatchBlock<T> Batch<T>( |
| | | 155 | | this ISourceBlock<T> source, |
| | | 156 | | int batchSize, |
| | | 157 | | GroupingDataflowBlockOptions opts |
| | | 158 | | ) |
| | | 159 | | { |
| | 1 | 160 | | var res = new BatchBlock<T>(batchSize, opts); |
| | 1 | 161 | | _ = source.LinkTo(res, new DataflowLinkOptions { PropagateCompletion = true }); |
| | 1 | 162 | | return res; |
| | | 163 | | } |
| | | 164 | | |
| | | 165 | | public static BufferBlock<T> Buffer<T>(this ISourceBlock<T> source) => |
| | 1 | 166 | | Buffer(source, new()); |
| | | 167 | | |
| | | 168 | | public static BufferBlock<T> Buffer<T>( |
| | | 169 | | this ISourceBlock<T> source, |
| | | 170 | | DataflowBlockOptions opts |
| | | 171 | | ) |
| | | 172 | | { |
| | 1 | 173 | | var res = new BufferBlock<T>(opts); |
| | 1 | 174 | | _ = source.LinkTo(res, new DataflowLinkOptions { PropagateCompletion = true }); |
| | 1 | 175 | | return res; |
| | | 176 | | } |
| | | 177 | | |
| | | 178 | | public static ActionBlock<T> Action<T>(this ISourceBlock<T> source, Action<T> action) => |
| | 0 | 179 | | Action(source, action, new()); |
| | | 180 | | |
| | | 181 | | public static ActionBlock<T> Action<T>( |
| | | 182 | | this ISourceBlock<T> source, |
| | | 183 | | Action<T> action, |
| | | 184 | | ExecutionDataflowBlockOptions opts |
| | | 185 | | ) |
| | | 186 | | { |
| | 0 | 187 | | var res = new ActionBlock<T>(action, opts); |
| | 0 | 188 | | _ = source.LinkTo(res, new DataflowLinkOptions { PropagateCompletion = true }); |
| | 0 | 189 | | return res; |
| | | 190 | | } |
| | | 191 | | |
| | | 192 | | public static ActionBlock<T> Action<T>(this ISourceBlock<T> source, Func<T, Task> action) => |
| | 0 | 193 | | Action(source, action, new()); |
| | | 194 | | |
| | | 195 | | public static ActionBlock<T> Action<T>( |
| | | 196 | | this ISourceBlock<T> source, |
| | | 197 | | Func<T, Task> action, |
| | | 198 | | ExecutionDataflowBlockOptions opts |
| | | 199 | | ) |
| | | 200 | | { |
| | 1 | 201 | | var res = new ActionBlock<T>(action, opts); |
| | 1 | 202 | | _ = source.LinkTo(res, new DataflowLinkOptions { PropagateCompletion = true }); |
| | 1 | 203 | | return res; |
| | | 204 | | } |
| | | 205 | | |
| | | 206 | | public static BroadcastBlock<T> BroadcastTo<T>( |
| | | 207 | | this ISourceBlock<T> source, |
| | | 208 | | Func<T, T>? clone, |
| | | 209 | | params ITargetBlock<T>[] targets |
| | 0 | 210 | | ) => BroadcastTo(source, clone, new(), targets); |
| | | 211 | | |
| | | 212 | | public static BroadcastBlock<T> BroadcastTo<T>( |
| | | 213 | | this ISourceBlock<T> source, |
| | | 214 | | Func<T, T>? clone, |
| | | 215 | | DataflowBlockOptions opts, |
| | | 216 | | params ITargetBlock<T>[] targets |
| | | 217 | | ) |
| | | 218 | | { |
| | 0 | 219 | | var res = new BroadcastBlock<T>(clone, opts); |
| | 0 | 220 | | foreach (var t in targets) |
| | | 221 | | { |
| | 0 | 222 | | _ = res.LinkTo(t, new DataflowLinkOptions { PropagateCompletion = true }); |
| | | 223 | | } |
| | 0 | 224 | | _ = source.LinkTo(res, new DataflowLinkOptions { PropagateCompletion = true }); |
| | 0 | 225 | | return res; |
| | | 226 | | } |
| | | 227 | | |
| | | 228 | | public static GuaranteedBroadcastBlock<T> GuaranteedBroadcastTo<T>( |
| | | 229 | | this ISourceBlock<T> source, |
| | | 230 | | params ITargetBlock<T>[] targets) |
| | 0 | 231 | | => GuaranteedBroadcastTo(source, new(), targets); |
| | | 232 | | |
| | | 233 | | public static GuaranteedBroadcastBlock<T> GuaranteedBroadcastTo<T>( |
| | | 234 | | this ISourceBlock<T> source, |
| | | 235 | | GuaranteedBroadcastBlockOptions opts, |
| | | 236 | | params ITargetBlock<T>[] targets) |
| | | 237 | | { |
| | 0 | 238 | | var res = new GuaranteedBroadcastBlock<T>(targets.Length, opts); |
| | 0 | 239 | | for (var i = 0; i < targets.Length; i++) |
| | | 240 | | { |
| | 0 | 241 | | _ = res[i].LinkTo(targets[i], new DataflowLinkOptions { PropagateCompletion = true }); |
| | | 242 | | } |
| | 0 | 243 | | _ = source.LinkTo(res, new DataflowLinkOptions { PropagateCompletion = true }); |
| | 0 | 244 | | return res; |
| | | 245 | | } |
| | | 246 | | |
| | | 247 | | /// <summary> |
| | | 248 | | /// Will eagerly buffer <paramref name="source"/> items from the async enumerable. |
| | | 249 | | /// </summary> |
| | | 250 | | public static IAsyncEnumerable<T> WithBuffer<T>(this IAsyncEnumerable<T> source, int bufferSize = DataflowBlockO |
| | 0 | 251 | | => source |
| | 0 | 252 | | .AsSourceBlock(options: new() { BoundedCapacity = bufferSize, CancellationToken = cancellationToken }) |
| | 0 | 253 | | .AsAsyncEnumerable(cancellationToken: cancellationToken); |
| | | 254 | | |
| | | 255 | | public static async IAsyncEnumerable<T> Finally<T>( |
| | | 256 | | this IAsyncEnumerable<T> source, |
| | | 257 | | Action finallyAction) |
| | | 258 | | { |
| | | 259 | | try |
| | | 260 | | { |
| | 131971 | 261 | | await foreach (var item in source) |
| | | 262 | | { |
| | 65972 | 263 | | yield return item; |
| | | 264 | | } |
| | 13 | 265 | | } |
| | | 266 | | finally |
| | | 267 | | { |
| | 14 | 268 | | finallyAction(); |
| | | 269 | | } |
| | 13 | 270 | | } |
| | | 271 | | |
| | | 272 | | /// <summary> |
| | | 273 | | /// Asynchronously collects all elements from an <see cref="ISourceBlock{T}"/> into a list. |
| | | 274 | | /// </summary> |
| | | 275 | | /// <typeparam name="T">The type of elements produced by the source block.</typeparam> |
| | | 276 | | /// <param name="source">The source block to read elements from.</param> |
| | | 277 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe while waiting for completion.</ |
| | | 278 | | /// <returns>A task that represents the asynchronous operation. The task result contains a list of all elements |
| | | 279 | | public static async Task<IList<T>> ToListAsync<T>(this ISourceBlock<T> source, CancellationToken cancellationTok |
| | | 280 | | { |
| | | 281 | | { |
| | 1 | 282 | | var b = new BufferBlock<T>(new() { CancellationToken = cancellationToken }); |
| | 1 | 283 | | source.LinkTo(b, new DataflowLinkOptions { PropagateCompletion = true }); |
| | 1 | 284 | | await source.Completion; |
| | 1 | 285 | | var succ = b.TryReceiveAll(out var items); |
| | | 286 | | |
| | | 287 | | // The buffer should complete; propagate any cancellation exceptions |
| | 1 | 288 | | await b.Completion; |
| | | 289 | | |
| | | 290 | | Debug.Assert(succ); |
| | 1 | 291 | | return items!; |
| | | 292 | | } |
| | 1 | 293 | | } |
| | | 294 | | |
| | | 295 | | public static IAsyncEnumerable<T> AsAsyncEnumerable<T>( |
| | | 296 | | this ISourceBlock<T> source, |
| | | 297 | | bool throwFirstExceptionOnly = true, |
| | | 298 | | bool completeSource = true, |
| | | 299 | | CancellationToken cancellationToken = default |
| | | 300 | | ) |
| | | 301 | | { |
| | 14 | 302 | | var ret = source is IReceivableSourceBlock<T> r ? Fast(r) : Slow(); |
| | | 303 | | |
| | 14 | 304 | | if (completeSource) |
| | | 305 | | { |
| | 14 | 306 | | ret = ret.Finally(() => |
| | 14 | 307 | | { |
| | 14 | 308 | | if (!source.Completion.IsCompleted) |
| | 14 | 309 | | { |
| | 1 | 310 | | source.Fault(new OperationCanceledException("Evaluation stopped prematurely")); |
| | 14 | 311 | | } |
| | 28 | 312 | | }); |
| | | 313 | | } |
| | | 314 | | |
| | 14 | 315 | | return ret; |
| | | 316 | | |
| | | 317 | | async Task<TResult> UnwrapAggregateAsync<TResult>(Func<Task<TResult>> fn) |
| | | 318 | | { |
| | | 319 | | try |
| | | 320 | | { |
| | 237 | 321 | | return await fn(); |
| | | 322 | | } |
| | 1 | 323 | | catch (AggregateException ex) when (throwFirstExceptionOnly) |
| | | 324 | | { |
| | 1 | 325 | | var inner = ex.Flatten().InnerExceptions.First(); |
| | 1 | 326 | | ExceptionDispatchInfo.Capture(inner).Throw(); |
| | 0 | 327 | | throw; // unreachable |
| | | 328 | | } |
| | 235 | 329 | | } |
| | | 330 | | |
| | | 331 | | Task<bool> OutputAvailableAsync() => UnwrapAggregateAsync(() => source.OutputAvailableAsync(cancellationToke |
| | | 332 | | Task<T> ReceiveAsync() => UnwrapAggregateAsync(() => source.ReceiveAsync(cancellationToken)); |
| | | 333 | | Task<int> CompletionAsync() => UnwrapAggregateAsync(async () => { |
| | | 334 | | await source.Completion; |
| | | 335 | | return 0; |
| | | 336 | | }); |
| | | 337 | | |
| | | 338 | | async IAsyncEnumerable<T> Fast(IReceivableSourceBlock<T> source) |
| | | 339 | | { |
| | 213 | 340 | | while (await OutputAvailableAsync()) |
| | | 341 | | { |
| | 201 | 342 | | var fastPathFails = true; |
| | 409 | 343 | | while (source.TryReceiveAll(out var l)) |
| | | 344 | | { |
| | 209 | 345 | | fastPathFails = false; |
| | 132341 | 346 | | foreach (var t in l) |
| | | 347 | | { |
| | 65962 | 348 | | yield return t; |
| | | 349 | | } |
| | 208 | 350 | | } |
| | | 351 | | |
| | 200 | 352 | | if (fastPathFails) |
| | | 353 | | { |
| | | 354 | | //Fall back to slow path |
| | 22 | 355 | | await foreach (var i in Slow()) |
| | | 356 | | { |
| | 10 | 357 | | yield return i; |
| | | 358 | | } |
| | 1 | 359 | | yield break; |
| | | 360 | | } |
| | | 361 | | } |
| | 12 | 362 | | await CompletionAsync(); |
| | | 363 | | } |
| | | 364 | | |
| | | 365 | | async IAsyncEnumerable<T> Slow() |
| | | 366 | | { |
| | | 367 | | |
| | | 368 | | while (true) |
| | | 369 | | { |
| | | 370 | | T t; |
| | | 371 | | try |
| | | 372 | | { |
| | 11 | 373 | | t = await ReceiveAsync(); |
| | 11 | 374 | | } catch (Exception) when (source.Completion.IsCompleted) |
| | | 375 | | { |
| | 1 | 376 | | break; |
| | | 377 | | } |
| | 10 | 378 | | yield return t; |
| | | 379 | | } |
| | 1 | 380 | | await CompletionAsync(); |
| | 1 | 381 | | } |
| | 13 | 382 | | } |
| | | 383 | | } |
| | | 384 | | } |