|
| 1 | +// Copyright (c) 2019-2023 ReactiveUI Association Incorporated. All rights reserved. |
| 2 | +// ReactiveUI Association Incorporated licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for full license information. |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.Reactive.Disposables; |
| 8 | +using System.Reactive.Linq; |
| 9 | +using System.Threading.Tasks; |
| 10 | + |
| 11 | +namespace ReactiveMarbles.Extensions.Internal; |
| 12 | + |
| 13 | +internal class ConcurrencyLimiter<T> |
| 14 | +{ |
| 15 | + private readonly object _locker = new(); |
| 16 | + private readonly object _disposalLocker = new(); |
| 17 | + private bool _disposed; |
| 18 | + private int _outstanding; |
| 19 | + private IEnumerator<Task<T>>? _rator; |
| 20 | + |
| 21 | + /// <summary> |
| 22 | + /// Initializes a new instance of the <see cref="ConcurrencyLimiter{T}"/> class. |
| 23 | + /// </summary> |
| 24 | + /// <param name="taskFunctions">The task functions.</param> |
| 25 | + /// <param name="maxConcurrency">The maximum concurrency.</param> |
| 26 | + public ConcurrencyLimiter(IEnumerable<Task<T>> taskFunctions, int maxConcurrency) |
| 27 | + { |
| 28 | + _rator = taskFunctions.GetEnumerator(); |
| 29 | + IObservable = Observable.Create<T>(observer => |
| 30 | + { |
| 31 | + for (var i = 0; i < maxConcurrency; i++) |
| 32 | + { |
| 33 | + PullNextTask(observer); |
| 34 | + } |
| 35 | + |
| 36 | + return Disposable.Create(() => Disposed = true); |
| 37 | + }); |
| 38 | + } |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// Gets the i observable. |
| 42 | + /// </summary> |
| 43 | + public IObservable<T> IObservable { get; } |
| 44 | + |
| 45 | + /// <summary> |
| 46 | + /// Gets or sets a value indicating whether this <see cref="ConcurrencyLimiter{T}"/> is disposed. |
| 47 | + /// </summary> |
| 48 | + /// <value><c>true</c> if disposed; otherwise, <c>false</c>.</value> |
| 49 | + private bool Disposed |
| 50 | + { |
| 51 | + get |
| 52 | + { |
| 53 | + lock (_disposalLocker) |
| 54 | + { |
| 55 | + return _disposed; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + set |
| 60 | + { |
| 61 | + lock (_disposalLocker) |
| 62 | + { |
| 63 | + _disposed = value; |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + /// <summary> |
| 69 | + /// Clears the rator. |
| 70 | + /// </summary> |
| 71 | + private void ClearRator() |
| 72 | + { |
| 73 | + _rator?.Dispose(); |
| 74 | + _rator = null; |
| 75 | + } |
| 76 | + |
| 77 | + /// <summary> |
| 78 | + /// Processes the task completion. |
| 79 | + /// </summary> |
| 80 | + /// <param name="observer">The observer.</param> |
| 81 | + /// <param name="decendantTask">The decendant Task.</param> |
| 82 | + private void ProcessTaskCompletion(IObserver<T> observer, Task<T> decendantTask) |
| 83 | + { |
| 84 | + lock (_locker) |
| 85 | + { |
| 86 | + if (Disposed || decendantTask.IsFaulted || decendantTask.IsCanceled) |
| 87 | + { |
| 88 | + ClearRator(); |
| 89 | + if (!Disposed) |
| 90 | + { |
| 91 | + observer.OnError((decendantTask.Exception == null ? new OperationCanceledException() : decendantTask.Exception.InnerException)!); |
| 92 | + } |
| 93 | + } |
| 94 | + else |
| 95 | + { |
| 96 | + observer.OnNext(decendantTask.Result); |
| 97 | + if (--_outstanding == 0 && _rator == null) |
| 98 | + { |
| 99 | + observer.OnCompleted(); |
| 100 | + } |
| 101 | + else |
| 102 | + { |
| 103 | + PullNextTask(observer); |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + /// <summary> |
| 110 | + /// Pulls the next task. |
| 111 | + /// </summary> |
| 112 | + /// <param name="observer">The observer.</param> |
| 113 | + private void PullNextTask(IObserver<T> observer) |
| 114 | + { |
| 115 | + lock (_locker) |
| 116 | + { |
| 117 | + if (Disposed) |
| 118 | + { |
| 119 | + ClearRator(); |
| 120 | + } |
| 121 | + |
| 122 | + if (_rator == null) |
| 123 | + { |
| 124 | + return; |
| 125 | + } |
| 126 | + |
| 127 | + if (!_rator.MoveNext()) |
| 128 | + { |
| 129 | + ClearRator(); |
| 130 | + if (_outstanding == 0) |
| 131 | + { |
| 132 | + observer.OnCompleted(); |
| 133 | + } |
| 134 | + |
| 135 | + return; |
| 136 | + } |
| 137 | + |
| 138 | + _outstanding++; |
| 139 | + _rator?.Current?.ContinueWith(ant => ProcessTaskCompletion(observer, ant)); |
| 140 | + } |
| 141 | + } |
| 142 | +} |
0 commit comments