RetryWhen.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.Reactive.Disposables;
  5. using System.Reactive.Subjects;
  6. using System.Text;
  7. using System.Threading;
  8. namespace System.Reactive.Linq.ObservableImpl
  9. {
  10. internal sealed class RetryWhen<T, U> : IObservable<T>
  11. {
  12. readonly IObservable<T> source;
  13. readonly Func<IObservable<Exception>, IObservable<U>> handler;
  14. internal RetryWhen(IObservable<T> source, Func<IObservable<Exception>, IObservable<U>> handler)
  15. {
  16. this.source = source;
  17. this.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 SerializedObserver(errorSignals));
  41. var d = redo.SubscribeSafe(parent.handlerObserver);
  42. parent.handlerObserver.OnSubscribe(d);
  43. parent.HandlerNext();
  44. return parent;
  45. }
  46. sealed class MainObserver : IObserver<T>, IDisposable
  47. {
  48. readonly IObserver<T> downstream;
  49. readonly IObserver<Exception> errorSignal;
  50. internal readonly HandlerObserver handlerObserver;
  51. readonly IObservable<T> source;
  52. IDisposable upstream;
  53. int trampoline;
  54. int halfSerializer;
  55. Exception error;
  56. internal MainObserver(IObserver<T> downstream, IObservable<T> source, IObserver<Exception> errorSignal)
  57. {
  58. this.downstream = downstream;
  59. this.source = source;
  60. this.errorSignal = errorSignal;
  61. this.handlerObserver = new HandlerObserver(this);
  62. }
  63. public void Dispose()
  64. {
  65. Disposable.TryDispose(ref upstream);
  66. handlerObserver.Dispose();
  67. }
  68. public void OnCompleted()
  69. {
  70. if (Interlocked.Increment(ref halfSerializer) == 1)
  71. {
  72. downstream.OnCompleted();
  73. Dispose();
  74. }
  75. }
  76. public void OnError(Exception error)
  77. {
  78. for (; ; )
  79. {
  80. var d = Volatile.Read(ref upstream);
  81. if (d == BooleanDisposable.True)
  82. {
  83. break;
  84. }
  85. if (Interlocked.CompareExchange(ref upstream, null, d) == d)
  86. {
  87. errorSignal.OnNext(error);
  88. d.Dispose();
  89. break;
  90. }
  91. }
  92. }
  93. public void OnNext(T value)
  94. {
  95. if (Interlocked.CompareExchange(ref halfSerializer, 1, 0) == 0)
  96. {
  97. downstream.OnNext(value);
  98. if (Interlocked.Decrement(ref halfSerializer) != 0)
  99. {
  100. var ex = error;
  101. if (ex == null)
  102. {
  103. downstream.OnCompleted();
  104. }
  105. else
  106. {
  107. downstream.OnError(ex);
  108. }
  109. Dispose();
  110. }
  111. }
  112. }
  113. internal void HandlerError(Exception error)
  114. {
  115. this.error = error;
  116. if (Interlocked.Increment(ref halfSerializer) == 1)
  117. {
  118. downstream.OnError(error);
  119. Dispose();
  120. }
  121. }
  122. internal void HandlerComplete()
  123. {
  124. if (Interlocked.Increment(ref halfSerializer) == 1)
  125. {
  126. downstream.OnCompleted();
  127. Dispose();
  128. }
  129. }
  130. internal void HandlerNext()
  131. {
  132. if (Interlocked.Increment(ref trampoline) == 1)
  133. {
  134. do
  135. {
  136. var sad = new SingleAssignmentDisposable();
  137. if (Disposable.TrySetSingle(ref upstream, sad) != TrySetSingleResult.Success)
  138. {
  139. return;
  140. }
  141. sad.Disposable = source.SubscribeSafe(this);
  142. }
  143. while (Interlocked.Decrement(ref trampoline) != 0);
  144. }
  145. }
  146. internal sealed class HandlerObserver : IObserver<U>, IDisposable
  147. {
  148. readonly MainObserver main;
  149. IDisposable upstream;
  150. internal HandlerObserver(MainObserver main)
  151. {
  152. this.main = main;
  153. }
  154. internal void OnSubscribe(IDisposable d)
  155. {
  156. Disposable.SetSingle(ref upstream, d);
  157. }
  158. public void Dispose()
  159. {
  160. Disposable.TryDispose(ref upstream);
  161. }
  162. public void OnCompleted()
  163. {
  164. main.HandlerComplete();
  165. Dispose();
  166. }
  167. public void OnError(Exception error)
  168. {
  169. main.HandlerError(error);
  170. Dispose();
  171. }
  172. public void OnNext(U value)
  173. {
  174. main.HandlerNext();
  175. }
  176. }
  177. }
  178. sealed class SerializedObserver : IObserver<Exception>
  179. {
  180. readonly IObserver<Exception> downstream;
  181. int wip;
  182. Exception terminalException;
  183. static readonly Exception DONE = new Exception();
  184. static readonly Exception SIGNALED = new Exception();
  185. readonly ConcurrentQueue<Exception> queue;
  186. internal SerializedObserver(IObserver<Exception> downstream)
  187. {
  188. this.downstream = downstream;
  189. this.queue = new ConcurrentQueue<Exception>();
  190. }
  191. public void OnCompleted()
  192. {
  193. if (Interlocked.CompareExchange(ref terminalException, DONE, null) == null)
  194. {
  195. Drain();
  196. }
  197. }
  198. public void OnError(Exception error)
  199. {
  200. if (Interlocked.CompareExchange(ref terminalException, error, null) == null)
  201. {
  202. Drain();
  203. }
  204. }
  205. public void OnNext(Exception value)
  206. {
  207. queue.Enqueue(value);
  208. Drain();
  209. }
  210. void Clear()
  211. {
  212. while (queue.TryDequeue(out var _)) ;
  213. }
  214. void Drain()
  215. {
  216. if (Interlocked.Increment(ref wip) != 1)
  217. {
  218. return;
  219. }
  220. int missed = 1;
  221. for (; ; )
  222. {
  223. var ex = Volatile.Read(ref terminalException);
  224. if (ex != null)
  225. {
  226. if (ex != SIGNALED)
  227. {
  228. Interlocked.Exchange(ref terminalException, SIGNALED);
  229. if (ex != DONE)
  230. {
  231. downstream.OnError(ex);
  232. }
  233. else
  234. {
  235. downstream.OnCompleted();
  236. }
  237. }
  238. Clear();
  239. }
  240. else
  241. {
  242. while (queue.TryDequeue(out var item))
  243. {
  244. downstream.OnNext(item);
  245. }
  246. }
  247. missed = Interlocked.Add(ref wip, -missed);
  248. if (missed == 0)
  249. {
  250. break;
  251. }
  252. }
  253. }
  254. }
  255. }
  256. }