Throttle.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Reactive.Concurrency;
  5. using System.Reactive.Disposables;
  6. using System.Threading;
  7. namespace System.Reactive.Linq
  8. {
  9. public partial class AsyncObservable
  10. {
  11. public static IAsyncObservable<TSource> Throttle<TSource>(this IAsyncObservable<TSource> source, TimeSpan dueTime)
  12. {
  13. if (source == null)
  14. throw new ArgumentNullException(nameof(source));
  15. if (dueTime < TimeSpan.Zero)
  16. throw new ArgumentOutOfRangeException(nameof(dueTime));
  17. return CreateAsyncObservable<TSource>.From(
  18. source,
  19. dueTime,
  20. static async (source, dueTime, observer) =>
  21. {
  22. var d = new CompositeAsyncDisposable();
  23. var (sink, throttler) = AsyncObserver.Throttle(observer, dueTime);
  24. await d.AddAsync(throttler).ConfigureAwait(false);
  25. var sourceSubscription = await source.SubscribeSafeAsync(sink).ConfigureAwait(false);
  26. await d.AddAsync(sourceSubscription).ConfigureAwait(false);
  27. return d;
  28. });
  29. }
  30. public static IAsyncObservable<TSource> Throttle<TSource>(this IAsyncObservable<TSource> source, TimeSpan dueTime, IAsyncScheduler scheduler)
  31. {
  32. if (source == null)
  33. throw new ArgumentNullException(nameof(source));
  34. if (dueTime < TimeSpan.Zero)
  35. throw new ArgumentOutOfRangeException(nameof(dueTime));
  36. if (scheduler == null)
  37. throw new ArgumentNullException(nameof(scheduler));
  38. return CreateAsyncObservable<TSource>.From(
  39. source,
  40. (dueTime, scheduler),
  41. static async (source, state, observer) =>
  42. {
  43. var d = new CompositeAsyncDisposable();
  44. var (sink, throttler) = AsyncObserver.Throttle(observer, state.dueTime, state.scheduler);
  45. await d.AddAsync(throttler).ConfigureAwait(false);
  46. var sourceSubscription = await source.SubscribeSafeAsync(sink).ConfigureAwait(false);
  47. await d.AddAsync(sourceSubscription).ConfigureAwait(false);
  48. return d;
  49. });
  50. }
  51. public static IAsyncObservable<TSource> Throttle<TSource, TThrottle>(this IAsyncObservable<TSource> source, Func<TSource, IAsyncObservable<TThrottle>> throttleSelector)
  52. {
  53. if (source == null)
  54. throw new ArgumentNullException(nameof(source));
  55. if (throttleSelector == null)
  56. throw new ArgumentNullException(nameof(throttleSelector));
  57. return CreateAsyncObservable<TSource>.From(
  58. source,
  59. throttleSelector,
  60. static async (source, throttleSelector, observer) =>
  61. {
  62. var d = new CompositeAsyncDisposable();
  63. var (sink, throttler) = AsyncObserver.Throttle(observer, throttleSelector);
  64. await d.AddAsync(throttler).ConfigureAwait(false);
  65. var sourceSubscription = await source.SubscribeSafeAsync(sink).ConfigureAwait(false);
  66. await d.AddAsync(sourceSubscription).ConfigureAwait(false);
  67. return d;
  68. });
  69. }
  70. }
  71. public partial class AsyncObserver
  72. {
  73. public static (IAsyncObserver<TSource>, IAsyncDisposable) Throttle<TSource>(IAsyncObserver<TSource> observer, TimeSpan dueTime) => Throttle(observer, dueTime, TaskPoolAsyncScheduler.Default);
  74. public static (IAsyncObserver<TSource>, IAsyncDisposable) Throttle<TSource>(IAsyncObserver<TSource> observer, TimeSpan dueTime, IAsyncScheduler scheduler)
  75. {
  76. if (observer == null)
  77. throw new ArgumentNullException(nameof(observer));
  78. if (dueTime < TimeSpan.Zero)
  79. throw new ArgumentOutOfRangeException(nameof(dueTime));
  80. if (scheduler == null)
  81. throw new ArgumentNullException(nameof(scheduler));
  82. var gate = new AsyncGate();
  83. var timer = new SerialAsyncDisposable();
  84. var hasValue = false;
  85. var value = default(TSource);
  86. var id = 0UL;
  87. return
  88. (
  89. Create<TSource>(
  90. async x =>
  91. {
  92. var myId = default(ulong);
  93. using (await gate.LockAsync().ConfigureAwait(false))
  94. {
  95. hasValue = true;
  96. value = x;
  97. myId = ++id;
  98. }
  99. var d = new SingleAssignmentAsyncDisposable();
  100. await timer.AssignAsync(d).ConfigureAwait(false);
  101. var t = await scheduler.ScheduleAsync(async ct =>
  102. {
  103. if (!ct.IsCancellationRequested)
  104. {
  105. using (await gate.LockAsync().ConfigureAwait(false))
  106. {
  107. if (hasValue && id == myId)
  108. {
  109. await observer.OnNextAsync(value).ConfigureAwait(false);
  110. }
  111. hasValue = false;
  112. }
  113. }
  114. }, dueTime).ConfigureAwait(false);
  115. await d.AssignAsync(t).ConfigureAwait(false);
  116. },
  117. async ex =>
  118. {
  119. await timer.DisposeAsync().ConfigureAwait(false);
  120. using (await gate.LockAsync().ConfigureAwait(false))
  121. {
  122. await observer.OnErrorAsync(ex).ConfigureAwait(false);
  123. hasValue = false;
  124. id++;
  125. }
  126. },
  127. async () =>
  128. {
  129. await timer.DisposeAsync().ConfigureAwait(false);
  130. using (await gate.LockAsync().ConfigureAwait(false))
  131. {
  132. if (hasValue)
  133. {
  134. await observer.OnNextAsync(value).ConfigureAwait(false);
  135. }
  136. await observer.OnCompletedAsync().ConfigureAwait(false);
  137. hasValue = false;
  138. id++;
  139. }
  140. }
  141. ),
  142. timer
  143. );
  144. }
  145. public static (IAsyncObserver<TSource>, IAsyncDisposable) Throttle<TSource, TThrottle>(IAsyncObserver<TSource> observer, Func<TSource, IAsyncObservable<TThrottle>> throttleSelector)
  146. {
  147. if (observer == null)
  148. throw new ArgumentNullException(nameof(observer));
  149. if (throttleSelector == null)
  150. throw new ArgumentNullException(nameof(throttleSelector));
  151. var gate = new AsyncGate();
  152. var throttler = new SerialAsyncDisposable();
  153. var hasValue = false;
  154. var value = default(TSource);
  155. var id = 0UL;
  156. return
  157. (
  158. Create<TSource>(
  159. async x =>
  160. {
  161. var throttleSource = default(IAsyncObservable<TThrottle>);
  162. try
  163. {
  164. throttleSource = throttleSelector(x); // REVIEW: Do we need an async variant?
  165. }
  166. catch (Exception ex)
  167. {
  168. using (await gate.LockAsync().ConfigureAwait(false))
  169. {
  170. await observer.OnErrorAsync(ex).ConfigureAwait(false);
  171. }
  172. return;
  173. }
  174. var myId = default(ulong);
  175. using (await gate.LockAsync().ConfigureAwait(false))
  176. {
  177. hasValue = true;
  178. value = x;
  179. myId = ++id;
  180. }
  181. var d = new SingleAssignmentAsyncDisposable();
  182. await throttler.AssignAsync(d).ConfigureAwait(false);
  183. var throttleObserver = Create<TThrottle>(
  184. async y =>
  185. {
  186. using (await gate.LockAsync().ConfigureAwait(false))
  187. {
  188. if (hasValue && myId == id)
  189. {
  190. await observer.OnNextAsync(x).ConfigureAwait(false);
  191. }
  192. hasValue = false;
  193. await d.DisposeAsync().ConfigureAwait(false);
  194. }
  195. },
  196. async ex =>
  197. {
  198. using (await gate.LockAsync().ConfigureAwait(false))
  199. {
  200. await observer.OnErrorAsync(ex).ConfigureAwait(false);
  201. }
  202. },
  203. async () =>
  204. {
  205. using (await gate.LockAsync().ConfigureAwait(false))
  206. {
  207. if (hasValue && myId == id)
  208. {
  209. await observer.OnNextAsync(x).ConfigureAwait(false);
  210. }
  211. hasValue = false;
  212. await d.DisposeAsync().ConfigureAwait(false);
  213. }
  214. }
  215. );
  216. var t = await throttleSource.SubscribeSafeAsync(throttleObserver).ConfigureAwait(false);
  217. await d.AssignAsync(t).ConfigureAwait(false);
  218. },
  219. async ex =>
  220. {
  221. await throttler.DisposeAsync().ConfigureAwait(false);
  222. using (await gate.LockAsync().ConfigureAwait(false))
  223. {
  224. await observer.OnErrorAsync(ex).ConfigureAwait(false);
  225. hasValue = false;
  226. id++;
  227. }
  228. },
  229. async () =>
  230. {
  231. await throttler.DisposeAsync().ConfigureAwait(false);
  232. using (await gate.LockAsync().ConfigureAwait(false))
  233. {
  234. if (hasValue)
  235. {
  236. await observer.OnNextAsync(value).ConfigureAwait(false);
  237. }
  238. await observer.OnCompletedAsync().ConfigureAwait(false);
  239. hasValue = false;
  240. id++;
  241. }
  242. }
  243. ),
  244. throttler
  245. );
  246. }
  247. }
  248. }