1
0

Throttle.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. #if !NO_PERF
  5. using System;
  6. using System.Reactive.Concurrency;
  7. using System.Reactive.Disposables;
  8. namespace System.Reactive.Linq.ObservableImpl
  9. {
  10. class Throttle<TSource> : Producer<TSource>
  11. {
  12. private readonly IObservable<TSource> _source;
  13. private readonly TimeSpan _dueTime;
  14. private readonly IScheduler _scheduler;
  15. public Throttle(IObservable<TSource> source, TimeSpan dueTime, IScheduler scheduler)
  16. {
  17. _source = source;
  18. _dueTime = dueTime;
  19. _scheduler = scheduler;
  20. }
  21. protected override IDisposable Run(IObserver<TSource> observer, IDisposable cancel, Action<IDisposable> setSink)
  22. {
  23. var sink = new _(this, observer, cancel);
  24. setSink(sink);
  25. return sink.Run();
  26. }
  27. class _ : Sink<TSource>, IObserver<TSource>
  28. {
  29. private readonly Throttle<TSource> _parent;
  30. public _(Throttle<TSource> parent, IObserver<TSource> observer, IDisposable cancel)
  31. : base(observer, cancel)
  32. {
  33. _parent = parent;
  34. }
  35. private object _gate;
  36. private TSource _value;
  37. private bool _hasValue;
  38. private SerialDisposable _cancelable;
  39. private ulong _id;
  40. public IDisposable Run()
  41. {
  42. _gate = new object();
  43. _value = default(TSource);
  44. _hasValue = false;
  45. _cancelable = new SerialDisposable();
  46. _id = 0UL;
  47. var subscription = _parent._source.SubscribeSafe(this);
  48. return StableCompositeDisposable.Create(subscription, _cancelable);
  49. }
  50. public void OnNext(TSource value)
  51. {
  52. var currentid = default(ulong);
  53. lock (_gate)
  54. {
  55. _hasValue = true;
  56. _value = value;
  57. _id = unchecked(_id + 1);
  58. currentid = _id;
  59. }
  60. var d = new SingleAssignmentDisposable();
  61. _cancelable.Disposable = d;
  62. d.Disposable = _parent._scheduler.Schedule(currentid, _parent._dueTime, Propagate);
  63. }
  64. private IDisposable Propagate(IScheduler self, ulong currentid)
  65. {
  66. lock (_gate)
  67. {
  68. if (_hasValue && _id == currentid)
  69. base._observer.OnNext(_value);
  70. _hasValue = false;
  71. }
  72. return Disposable.Empty;
  73. }
  74. public void OnError(Exception error)
  75. {
  76. _cancelable.Dispose();
  77. lock (_gate)
  78. {
  79. base._observer.OnError(error);
  80. base.Dispose();
  81. _hasValue = false;
  82. _id = unchecked(_id + 1);
  83. }
  84. }
  85. public void OnCompleted()
  86. {
  87. _cancelable.Dispose();
  88. lock (_gate)
  89. {
  90. if (_hasValue)
  91. base._observer.OnNext(_value);
  92. base._observer.OnCompleted();
  93. base.Dispose();
  94. _hasValue = false;
  95. _id = unchecked(_id + 1);
  96. }
  97. }
  98. }
  99. }
  100. class Throttle<TSource, TThrottle> : Producer<TSource>
  101. {
  102. private readonly IObservable<TSource> _source;
  103. private readonly Func<TSource, IObservable<TThrottle>> _throttleSelector;
  104. public Throttle(IObservable<TSource> source, Func<TSource, IObservable<TThrottle>> throttleSelector)
  105. {
  106. _source = source;
  107. _throttleSelector = throttleSelector;
  108. }
  109. protected override IDisposable Run(IObserver<TSource> observer, IDisposable cancel, Action<IDisposable> setSink)
  110. {
  111. var sink = new _(this, observer, cancel);
  112. setSink(sink);
  113. return sink.Run();
  114. }
  115. class _ : Sink<TSource>, IObserver<TSource>
  116. {
  117. private readonly Throttle<TSource, TThrottle> _parent;
  118. public _(Throttle<TSource, TThrottle> parent, IObserver<TSource> observer, IDisposable cancel)
  119. : base(observer, cancel)
  120. {
  121. _parent = parent;
  122. }
  123. private object _gate;
  124. private TSource _value;
  125. private bool _hasValue;
  126. private SerialDisposable _cancelable;
  127. private ulong _id;
  128. public IDisposable Run()
  129. {
  130. _gate = new object();
  131. _value = default(TSource);
  132. _hasValue = false;
  133. _cancelable = new SerialDisposable();
  134. _id = 0UL;
  135. var subscription = _parent._source.SubscribeSafe(this);
  136. return StableCompositeDisposable.Create(subscription, _cancelable);
  137. }
  138. public void OnNext(TSource value)
  139. {
  140. var throttle = default(IObservable<TThrottle>);
  141. try
  142. {
  143. throttle = _parent._throttleSelector(value);
  144. }
  145. catch (Exception error)
  146. {
  147. lock (_gate)
  148. {
  149. base._observer.OnError(error);
  150. base.Dispose();
  151. }
  152. return;
  153. }
  154. ulong currentid;
  155. lock (_gate)
  156. {
  157. _hasValue = true;
  158. _value = value;
  159. _id = unchecked(_id + 1);
  160. currentid = _id;
  161. }
  162. var d = new SingleAssignmentDisposable();
  163. _cancelable.Disposable = d;
  164. d.Disposable = throttle.SubscribeSafe(new Delta(this, value, currentid, d));
  165. }
  166. public void OnError(Exception error)
  167. {
  168. _cancelable.Dispose();
  169. lock (_gate)
  170. {
  171. base._observer.OnError(error);
  172. base.Dispose();
  173. _hasValue = false;
  174. _id = unchecked(_id + 1);
  175. }
  176. }
  177. public void OnCompleted()
  178. {
  179. _cancelable.Dispose();
  180. lock (_gate)
  181. {
  182. if (_hasValue)
  183. base._observer.OnNext(_value);
  184. base._observer.OnCompleted();
  185. base.Dispose();
  186. _hasValue = false;
  187. _id = unchecked(_id + 1);
  188. }
  189. }
  190. class Delta : IObserver<TThrottle>
  191. {
  192. private readonly _ _parent;
  193. private readonly TSource _value;
  194. private readonly ulong _currentid;
  195. private readonly IDisposable _self;
  196. public Delta(_ parent, TSource value, ulong currentid, IDisposable self)
  197. {
  198. _parent = parent;
  199. _value = value;
  200. _currentid = currentid;
  201. _self = self;
  202. }
  203. public void OnNext(TThrottle value)
  204. {
  205. lock (_parent._gate)
  206. {
  207. if (_parent._hasValue && _parent._id == _currentid)
  208. _parent._observer.OnNext(_value);
  209. _parent._hasValue = false;
  210. _self.Dispose();
  211. }
  212. }
  213. public void OnError(Exception error)
  214. {
  215. lock (_parent._gate)
  216. {
  217. _parent._observer.OnError(error);
  218. _parent.Dispose();
  219. }
  220. }
  221. public void OnCompleted()
  222. {
  223. lock (_parent._gate)
  224. {
  225. if (_parent._hasValue && _parent._id == _currentid)
  226. _parent._observer.OnNext(_value);
  227. _parent._hasValue = false;
  228. _self.Dispose();
  229. }
  230. }
  231. }
  232. }
  233. }
  234. }
  235. #endif