RetryWhen.cs 7.3 KB

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