Timeout.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. using System.Threading.Tasks;
  8. namespace System.Reactive.Linq
  9. {
  10. public partial class AsyncObservable
  11. {
  12. public static IAsyncObservable<TSource> Timeout<TSource>(this IAsyncObservable<TSource> source, TimeSpan dueTime)
  13. {
  14. if (source == null)
  15. throw new ArgumentNullException(nameof(source));
  16. return Create(
  17. source,
  18. dueTime,
  19. default(TSource),
  20. async (source, dueTime, observer) =>
  21. {
  22. var sourceSubscription = new SingleAssignmentAsyncDisposable();
  23. var (sink, disposable) = await AsyncObserver.Timeout(observer, sourceSubscription, dueTime).ConfigureAwait(false);
  24. var sourceSubscriptionInner = await source.SubscribeSafeAsync(sink).ConfigureAwait(false);
  25. await sourceSubscription.AssignAsync(sourceSubscriptionInner).ConfigureAwait(false);
  26. return disposable;
  27. });
  28. }
  29. public static IAsyncObservable<TSource> Timeout<TSource>(this IAsyncObservable<TSource> source, TimeSpan dueTime, IAsyncScheduler scheduler)
  30. {
  31. if (source == null)
  32. throw new ArgumentNullException(nameof(source));
  33. if (scheduler == null)
  34. throw new ArgumentNullException(nameof(scheduler));
  35. return Create(
  36. source,
  37. (dueTime, scheduler),
  38. default(TSource),
  39. async (source, state, observer) =>
  40. {
  41. var sourceSubscription = new SingleAssignmentAsyncDisposable();
  42. var (sink, disposable) = await AsyncObserver.Timeout(observer, sourceSubscription, state.dueTime, state.scheduler).ConfigureAwait(false);
  43. var sourceSubscriptionInner = await source.SubscribeSafeAsync(sink).ConfigureAwait(false);
  44. await sourceSubscription.AssignAsync(sourceSubscriptionInner).ConfigureAwait(false);
  45. return disposable;
  46. });
  47. }
  48. public static IAsyncObservable<TSource> Timeout<TSource>(this IAsyncObservable<TSource> source, TimeSpan dueTime, IAsyncObservable<TSource> other)
  49. {
  50. if (source == null)
  51. throw new ArgumentNullException(nameof(source));
  52. if (other == null)
  53. throw new ArgumentNullException(nameof(other));
  54. return Create(
  55. source,
  56. (dueTime, other),
  57. default(TSource),
  58. async (source, state, observer) =>
  59. {
  60. var sourceSubscription = new SingleAssignmentAsyncDisposable();
  61. var (sink, disposable) = await AsyncObserver.Timeout(observer, sourceSubscription, state.dueTime, state.other).ConfigureAwait(false);
  62. var sourceSubscriptionInner = await source.SubscribeSafeAsync(sink).ConfigureAwait(false);
  63. await sourceSubscription.AssignAsync(sourceSubscriptionInner).ConfigureAwait(false);
  64. return disposable;
  65. });
  66. }
  67. public static IAsyncObservable<TSource> Timeout<TSource>(this IAsyncObservable<TSource> source, TimeSpan dueTime, IAsyncObservable<TSource> other, IAsyncScheduler scheduler)
  68. {
  69. if (source == null)
  70. throw new ArgumentNullException(nameof(source));
  71. if (other == null)
  72. throw new ArgumentNullException(nameof(other));
  73. if (scheduler == null)
  74. throw new ArgumentNullException(nameof(scheduler));
  75. return Create(
  76. source,
  77. (dueTime, other, scheduler),
  78. default(TSource),
  79. async (source, state, observer) =>
  80. {
  81. var sourceSubscription = new SingleAssignmentAsyncDisposable();
  82. var (sink, disposable) = await AsyncObserver.Timeout(observer, sourceSubscription, state.dueTime, state.other, state.scheduler).ConfigureAwait(false);
  83. var sourceSubscriptionInner = await source.SubscribeSafeAsync(sink).ConfigureAwait(false);
  84. await sourceSubscription.AssignAsync(sourceSubscriptionInner).ConfigureAwait(false);
  85. return disposable;
  86. });
  87. }
  88. }
  89. public partial class AsyncObserver
  90. {
  91. public static Task<(IAsyncObserver<TSource>, IAsyncDisposable)> Timeout<TSource>(IAsyncObserver<TSource> observer, IAsyncDisposable sourceSubscription, TimeSpan dueTime)
  92. {
  93. if (observer == null)
  94. throw new ArgumentNullException(nameof(observer));
  95. if (sourceSubscription == null)
  96. throw new ArgumentNullException(nameof(sourceSubscription));
  97. return Timeout(observer, sourceSubscription, dueTime, AsyncObservable.Throw<TSource>(new TimeoutException()), TaskPoolAsyncScheduler.Default);
  98. }
  99. public static Task<(IAsyncObserver<TSource>, IAsyncDisposable)> Timeout<TSource>(IAsyncObserver<TSource> observer, IAsyncDisposable sourceSubscription, TimeSpan dueTime, IAsyncScheduler scheduler)
  100. {
  101. if (observer == null)
  102. throw new ArgumentNullException(nameof(observer));
  103. if (sourceSubscription == null)
  104. throw new ArgumentNullException(nameof(sourceSubscription));
  105. if (scheduler == null)
  106. throw new ArgumentNullException(nameof(scheduler));
  107. return Timeout(observer, sourceSubscription, dueTime, AsyncObservable.Throw<TSource>(new TimeoutException()), scheduler);
  108. }
  109. public static Task<(IAsyncObserver<TSource>, IAsyncDisposable)> Timeout<TSource>(IAsyncObserver<TSource> observer, IAsyncDisposable sourceSubscription, TimeSpan dueTime, IAsyncObservable<TSource> other)
  110. {
  111. if (observer == null)
  112. throw new ArgumentNullException(nameof(observer));
  113. if (sourceSubscription == null)
  114. throw new ArgumentNullException(nameof(sourceSubscription));
  115. if (other == null)
  116. throw new ArgumentNullException(nameof(other));
  117. return Timeout(observer, sourceSubscription, dueTime, other, TaskPoolAsyncScheduler.Default);
  118. }
  119. public static Task<(IAsyncObserver<TSource>, IAsyncDisposable)> Timeout<TSource>(IAsyncObserver<TSource> observer, IAsyncDisposable sourceSubscription, TimeSpan dueTime, IAsyncObservable<TSource> other, IAsyncScheduler scheduler)
  120. {
  121. if (observer == null)
  122. throw new ArgumentNullException(nameof(observer));
  123. if (sourceSubscription == null)
  124. throw new ArgumentNullException(nameof(sourceSubscription));
  125. if (other == null)
  126. throw new ArgumentNullException(nameof(other));
  127. if (scheduler == null)
  128. throw new ArgumentNullException(nameof(scheduler));
  129. return CoreAsync();
  130. async Task<(IAsyncObserver<TSource>, IAsyncDisposable)> CoreAsync()
  131. {
  132. var gate = new AsyncLock();
  133. var switched = false;
  134. var id = 0UL;
  135. var timer = new SerialAsyncDisposable();
  136. var subscription = new SerialAsyncDisposable();
  137. var d = StableCompositeAsyncDisposable.Create(timer, subscription);
  138. await subscription.AssignAsync(sourceSubscription).ConfigureAwait(false);
  139. async Task<bool> OnAsync()
  140. {
  141. var hasWon = false;
  142. using (await gate.LockAsync().ConfigureAwait(false))
  143. {
  144. if (!switched)
  145. {
  146. unchecked
  147. {
  148. ++id;
  149. }
  150. hasWon = true;
  151. }
  152. }
  153. return hasWon;
  154. }
  155. async Task CreateTimerAsync()
  156. {
  157. var timerId = id;
  158. var timeout = await scheduler.ScheduleAsync(async ct =>
  159. {
  160. var hasWon = false;
  161. using (await gate.LockAsync().ConfigureAwait(false))
  162. {
  163. hasWon = switched = timerId == id;
  164. }
  165. if (hasWon)
  166. {
  167. var otherSubscription = await other.SubscribeSafeAsync(observer).RendezVous(scheduler, ct);
  168. await subscription.AssignAsync(otherSubscription).RendezVous(scheduler, ct);
  169. }
  170. }, dueTime).ConfigureAwait(false);
  171. await timer.AssignAsync(timeout).ConfigureAwait(false);
  172. }
  173. var sink = Create<TSource>(
  174. async x =>
  175. {
  176. if (await OnAsync().ConfigureAwait(false))
  177. {
  178. await observer.OnNextAsync(x).ConfigureAwait(false);
  179. await CreateTimerAsync().ConfigureAwait(false);
  180. }
  181. },
  182. async ex =>
  183. {
  184. if (await OnAsync().ConfigureAwait(false))
  185. {
  186. await observer.OnErrorAsync(ex).ConfigureAwait(false);
  187. }
  188. },
  189. async () =>
  190. {
  191. if (await OnAsync().ConfigureAwait(false))
  192. {
  193. await observer.OnCompletedAsync().ConfigureAwait(false);
  194. }
  195. }
  196. );
  197. await CreateTimerAsync().ConfigureAwait(false);
  198. return (sink, d);
  199. }
  200. }
  201. }
  202. }