Timeout.cs 10 KB

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