CatchScheduler.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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.Disposables;
  5. using System.Runtime.CompilerServices;
  6. namespace System.Reactive.Concurrency
  7. {
  8. internal sealed class CatchScheduler<TException> : SchedulerWrapper
  9. where TException : Exception
  10. {
  11. private readonly Func<TException, bool> _handler;
  12. public CatchScheduler(IScheduler scheduler, Func<TException, bool> handler)
  13. : base(scheduler)
  14. {
  15. _handler = handler;
  16. }
  17. protected override Func<IScheduler, TState, IDisposable> Wrap<TState>(Func<IScheduler, TState, IDisposable> action)
  18. {
  19. return (self, state) =>
  20. {
  21. try
  22. {
  23. return action(GetRecursiveWrapper(self), state);
  24. }
  25. catch (TException exception) when (_handler(exception))
  26. {
  27. return Disposable.Empty;
  28. }
  29. };
  30. }
  31. public CatchScheduler(IScheduler scheduler, Func<TException, bool> handler, ConditionalWeakTable<IScheduler, IScheduler> cache)
  32. : base(scheduler, cache)
  33. {
  34. _handler = handler;
  35. }
  36. protected override SchedulerWrapper Clone(IScheduler scheduler, ConditionalWeakTable<IScheduler, IScheduler> cache)
  37. {
  38. return new CatchScheduler<TException>(scheduler, _handler, cache);
  39. }
  40. protected override bool TryGetService(IServiceProvider provider, Type serviceType, out object service)
  41. {
  42. service = provider.GetService(serviceType);
  43. if (service != null)
  44. {
  45. if (serviceType == typeof(ISchedulerLongRunning))
  46. {
  47. service = new CatchSchedulerLongRunning((ISchedulerLongRunning)service, _handler);
  48. }
  49. else if (serviceType == typeof(ISchedulerPeriodic))
  50. {
  51. service = new CatchSchedulerPeriodic((ISchedulerPeriodic)service, _handler);
  52. }
  53. }
  54. return true;
  55. }
  56. private class CatchSchedulerLongRunning : ISchedulerLongRunning
  57. {
  58. private readonly ISchedulerLongRunning _scheduler;
  59. private readonly Func<TException, bool> _handler;
  60. public CatchSchedulerLongRunning(ISchedulerLongRunning scheduler, Func<TException, bool> handler)
  61. {
  62. _scheduler = scheduler;
  63. _handler = handler;
  64. }
  65. public IDisposable ScheduleLongRunning<TState>(TState state, Action<TState, ICancelable> action)
  66. {
  67. return _scheduler.ScheduleLongRunning(
  68. (scheduler: this, action, state),
  69. (tuple, cancel) =>
  70. {
  71. try
  72. {
  73. tuple.action(tuple.state, cancel);
  74. }
  75. catch (TException exception) when (tuple.scheduler._handler(exception))
  76. {
  77. }
  78. });
  79. }
  80. }
  81. private sealed class CatchSchedulerPeriodic : ISchedulerPeriodic
  82. {
  83. private sealed class PeriodicallyScheduledWorkItem<TState> : IDisposable
  84. {
  85. private IDisposable _cancel;
  86. private bool _failed = false;
  87. private readonly Func<TState, TState> _action;
  88. private readonly CatchSchedulerPeriodic _catchScheduler;
  89. public PeriodicallyScheduledWorkItem(CatchSchedulerPeriodic scheduler, TState state, TimeSpan period, Func<TState, TState> action)
  90. {
  91. _catchScheduler = scheduler;
  92. _action = action;
  93. Disposable.SetSingle(ref _cancel, scheduler._scheduler.SchedulePeriodic((@this: this, state), period, tuple => tuple.@this?.Tick(tuple.state) ?? default));
  94. }
  95. public void Dispose()
  96. {
  97. Disposable.TryDispose(ref _cancel);
  98. }
  99. private (PeriodicallyScheduledWorkItem<TState> @this, TState state) Tick(TState state)
  100. {
  101. //
  102. // Cancellation may not be granted immediately; prevent from running user
  103. // code in that case. Periodic schedulers are assumed to introduce some
  104. // degree of concurrency, so we should return from the SchedulePeriodic
  105. // call eventually, allowing the d.Dispose() call in the catch block to
  106. // take effect.
  107. //
  108. if (_failed)
  109. {
  110. return default;
  111. }
  112. try
  113. {
  114. return (this, _action(state));
  115. }
  116. catch (TException exception)
  117. {
  118. _failed = true;
  119. if (!_catchScheduler._handler(exception))
  120. {
  121. throw;
  122. }
  123. Disposable.TryDispose(ref _cancel);
  124. return default;
  125. }
  126. }
  127. }
  128. private readonly ISchedulerPeriodic _scheduler;
  129. private readonly Func<TException, bool> _handler;
  130. public CatchSchedulerPeriodic(ISchedulerPeriodic scheduler, Func<TException, bool> handler)
  131. {
  132. _scheduler = scheduler;
  133. _handler = handler;
  134. }
  135. public IDisposable SchedulePeriodic<TState>(TState state, TimeSpan period, Func<TState, TState> action)
  136. {
  137. return new PeriodicallyScheduledWorkItem<TState>(this, state, period, action);
  138. }
  139. }
  140. }
  141. }