Throttle.cs 12 KB

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