Timeout.cs 9.6 KB

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