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. async Task<(IAsyncObserver<TSource>, IAsyncDisposable)> CoreAsync()
  114. {
  115. var gate = new AsyncLock();
  116. var switched = false;
  117. var id = 0UL;
  118. var timer = new SerialAsyncDisposable();
  119. var subscription = new SerialAsyncDisposable();
  120. var d = StableCompositeAsyncDisposable.Create(timer, subscription);
  121. await subscription.AssignAsync(sourceSubscription).ConfigureAwait(false);
  122. async Task<bool> OnAsync()
  123. {
  124. var hasWon = false;
  125. using (await gate.LockAsync().ConfigureAwait(false))
  126. {
  127. if (!switched)
  128. {
  129. unchecked
  130. {
  131. ++id;
  132. }
  133. hasWon = true;
  134. }
  135. }
  136. return hasWon;
  137. }
  138. async Task CreateTimerAsync()
  139. {
  140. var timerId = id;
  141. var timeout = await scheduler.ScheduleAsync(async ct =>
  142. {
  143. var hasWon = false;
  144. using (await gate.LockAsync().ConfigureAwait(false))
  145. {
  146. hasWon = switched = timerId == id;
  147. }
  148. if (hasWon)
  149. {
  150. var otherSubscription = await other.SubscribeSafeAsync(observer).RendezVous(scheduler, ct);
  151. await subscription.AssignAsync(otherSubscription).RendezVous(scheduler, ct);
  152. }
  153. }, dueTime).ConfigureAwait(false);
  154. await timer.AssignAsync(timeout).ConfigureAwait(false);
  155. }
  156. var sink = Create<TSource>(
  157. async x =>
  158. {
  159. if (await OnAsync().ConfigureAwait(false))
  160. {
  161. await observer.OnNextAsync(x).ConfigureAwait(false);
  162. await CreateTimerAsync().ConfigureAwait(false);
  163. }
  164. },
  165. async ex =>
  166. {
  167. if (await OnAsync().ConfigureAwait(false))
  168. {
  169. await observer.OnErrorAsync(ex).ConfigureAwait(false);
  170. }
  171. },
  172. async () =>
  173. {
  174. if (await OnAsync().ConfigureAwait(false))
  175. {
  176. await observer.OnCompletedAsync().ConfigureAwait(false);
  177. }
  178. }
  179. );
  180. await CreateTimerAsync().ConfigureAwait(false);
  181. return (sink, d);
  182. }
  183. return CoreAsync();
  184. }
  185. }
  186. }