RetryWhen.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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.Collections.Concurrent;
  5. using System.Reactive.Disposables;
  6. using System.Reactive.Subjects;
  7. using System.Threading;
  8. namespace System.Reactive.Linq.ObservableImpl
  9. {
  10. internal sealed class RetryWhen<T, U> : IObservable<T>
  11. {
  12. private readonly IObservable<T> _source;
  13. private readonly Func<IObservable<Exception>, IObservable<U>> _handler;
  14. internal RetryWhen(IObservable<T> source, Func<IObservable<Exception>, IObservable<U>> handler)
  15. {
  16. _source = source;
  17. _handler = handler;
  18. }
  19. public IDisposable Subscribe(IObserver<T> observer)
  20. {
  21. if (observer == null)
  22. {
  23. throw new ArgumentNullException(nameof(observer));
  24. }
  25. var errorSignals = new Subject<Exception>();
  26. var redo = default(IObservable<U>);
  27. try
  28. {
  29. redo = _handler(errorSignals);
  30. if (redo == null)
  31. {
  32. throw new NullReferenceException("The handler returned a null IObservable");
  33. }
  34. }
  35. catch (Exception ex)
  36. {
  37. observer.OnError(ex);
  38. return Disposable.Empty;
  39. }
  40. var parent = new MainObserver(observer, _source, new RedoSerializedObserver<Exception>(errorSignals));
  41. var d = redo.SubscribeSafe(parent.HandlerConsumer);
  42. Disposable.SetSingle(ref parent.HandlerUpstream, d);
  43. parent.HandlerNext();
  44. return parent;
  45. }
  46. private sealed class MainObserver : Sink<T>, IObserver<T>
  47. {
  48. private readonly IObserver<Exception> _errorSignal;
  49. internal readonly HandlerObserver HandlerConsumer;
  50. private readonly IObservable<T> _source;
  51. private IDisposable _upstream;
  52. internal IDisposable HandlerUpstream;
  53. private int _trampoline;
  54. private int _halfSerializer;
  55. private Exception _error;
  56. internal MainObserver(IObserver<T> downstream, IObservable<T> source, IObserver<Exception> errorSignal) : base(downstream)
  57. {
  58. _source = source;
  59. _errorSignal = errorSignal;
  60. HandlerConsumer = new HandlerObserver(this);
  61. }
  62. protected override void Dispose(bool disposing)
  63. {
  64. if (disposing)
  65. {
  66. Disposable.TryDispose(ref _upstream);
  67. Disposable.TryDispose(ref HandlerUpstream);
  68. }
  69. base.Dispose(disposing);
  70. }
  71. public void OnCompleted()
  72. {
  73. HalfSerializer.ForwardOnCompleted(this, ref _halfSerializer, ref _error);
  74. }
  75. public void OnError(Exception error)
  76. {
  77. if (Disposable.TrySetSerial(ref _upstream, null))
  78. {
  79. _errorSignal.OnNext(error);
  80. }
  81. }
  82. public void OnNext(T value)
  83. {
  84. HalfSerializer.ForwardOnNext(this, value, ref _halfSerializer, ref _error);
  85. }
  86. private void HandlerError(Exception error)
  87. {
  88. HalfSerializer.ForwardOnError(this, error, ref _halfSerializer, ref _error);
  89. }
  90. private void HandlerComplete()
  91. {
  92. HalfSerializer.ForwardOnCompleted(this, ref _halfSerializer, ref _error);
  93. }
  94. internal void HandlerNext()
  95. {
  96. if (Interlocked.Increment(ref _trampoline) == 1)
  97. {
  98. do
  99. {
  100. var sad = new SingleAssignmentDisposable();
  101. if (Disposable.TrySetSingle(ref _upstream, sad) != TrySetSingleResult.Success)
  102. {
  103. return;
  104. }
  105. sad.Disposable = _source.SubscribeSafe(this);
  106. }
  107. while (Interlocked.Decrement(ref _trampoline) != 0);
  108. }
  109. }
  110. internal sealed class HandlerObserver : IObserver<U>
  111. {
  112. private readonly MainObserver _main;
  113. internal HandlerObserver(MainObserver main)
  114. {
  115. _main = main;
  116. }
  117. public void OnCompleted()
  118. {
  119. _main.HandlerComplete();
  120. }
  121. public void OnError(Exception error)
  122. {
  123. _main.HandlerError(error);
  124. }
  125. public void OnNext(U value)
  126. {
  127. _main.HandlerNext();
  128. }
  129. }
  130. }
  131. }
  132. internal sealed class RedoSerializedObserver<X> : IObserver<X>
  133. {
  134. private readonly IObserver<X> _downstream;
  135. private int _wip;
  136. private Exception _terminalException;
  137. private static readonly Exception SignaledIndicator = new Exception();
  138. private readonly ConcurrentQueue<X> _queue;
  139. internal RedoSerializedObserver(IObserver<X> downstream)
  140. {
  141. _downstream = downstream;
  142. _queue = new ConcurrentQueue<X>();
  143. }
  144. public void OnCompleted()
  145. {
  146. if (Interlocked.CompareExchange(ref _terminalException, ExceptionHelper.Terminated, null) == null)
  147. {
  148. Drain();
  149. }
  150. }
  151. public void OnError(Exception error)
  152. {
  153. if (Interlocked.CompareExchange(ref _terminalException, error, null) == null)
  154. {
  155. Drain();
  156. }
  157. }
  158. public void OnNext(X value)
  159. {
  160. _queue.Enqueue(value);
  161. Drain();
  162. }
  163. private void Clear()
  164. {
  165. while (_queue.TryDequeue(out _))
  166. {
  167. }
  168. }
  169. private void Drain()
  170. {
  171. if (Interlocked.Increment(ref _wip) != 1)
  172. {
  173. return;
  174. }
  175. var missed = 1;
  176. for (; ; )
  177. {
  178. var ex = Volatile.Read(ref _terminalException);
  179. if (ex != null)
  180. {
  181. if (ex != SignaledIndicator)
  182. {
  183. Interlocked.Exchange(ref _terminalException, SignaledIndicator);
  184. if (ex != ExceptionHelper.Terminated)
  185. {
  186. _downstream.OnError(ex);
  187. }
  188. else
  189. {
  190. _downstream.OnCompleted();
  191. }
  192. }
  193. Clear();
  194. }
  195. else
  196. {
  197. while (_queue.TryDequeue(out var item))
  198. {
  199. _downstream.OnNext(item);
  200. }
  201. }
  202. missed = Interlocked.Add(ref _wip, -missed);
  203. if (missed == 0)
  204. {
  205. break;
  206. }
  207. }
  208. }
  209. }
  210. }