AnsiRequestScheduler.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #nullable enable
  2. using System.Collections.Concurrent;
  3. namespace Terminal.Gui;
  4. /// <summary>
  5. /// Manages <see cref="AnsiEscapeSequenceRequest"/> made to an <see cref="IAnsiResponseParser"/>.
  6. /// Ensures there are not 2+ outstanding requests with the same terminator, throttles request sends
  7. /// to prevent console becoming unresponsive and handles evicting ignored requests (no reply from
  8. /// terminal).
  9. /// </summary>
  10. internal class AnsiRequestScheduler
  11. {
  12. private readonly IAnsiResponseParser _parser;
  13. /// <summary>
  14. /// Function for returning the current time. Use in unit tests to
  15. /// ensure repeatable tests.
  16. /// </summary>
  17. internal Func<DateTime> Now { get; set; }
  18. private readonly HashSet<Tuple<AnsiEscapeSequenceRequest, DateTime>> _queuedRequests = new ();
  19. internal IReadOnlyCollection<AnsiEscapeSequenceRequest> QueuedRequests => _queuedRequests.Select (r => r.Item1).ToList ();
  20. /// <summary>
  21. /// <para>
  22. /// Dictionary where key is ansi request terminator and value is when we last sent a request for
  23. /// this terminator. Combined with <see cref="_throttle"/> this prevents hammering the console
  24. /// with too many requests in sequence which can cause console to freeze as there is no space for
  25. /// regular screen drawing / mouse events etc to come in.
  26. /// </para>
  27. /// <para>
  28. /// When user exceeds the throttle, new requests accumulate in <see cref="_queuedRequests"/> (i.e. remain
  29. /// queued).
  30. /// </para>
  31. /// </summary>
  32. private readonly ConcurrentDictionary<string, DateTime> _lastSend = new ();
  33. /// <summary>
  34. /// Number of milliseconds after sending a request that we allow
  35. /// another request to go out.
  36. /// </summary>
  37. private readonly TimeSpan _throttle = TimeSpan.FromMilliseconds (100);
  38. private readonly TimeSpan _runScheduleThrottle = TimeSpan.FromMilliseconds (100);
  39. /// <summary>
  40. /// If console has not responded to a request after this period of time, we assume that it is never going
  41. /// to respond. Only affects when we try to send a new request with the same terminator - at which point
  42. /// we tell the parser to stop expecting the old request and start expecting the new request.
  43. /// </summary>
  44. private readonly TimeSpan _staleTimeout = TimeSpan.FromSeconds (1);
  45. private readonly DateTime _lastRun;
  46. public AnsiRequestScheduler (IAnsiResponseParser parser, Func<DateTime>? now = null)
  47. {
  48. _parser = parser;
  49. Now = now ?? (() => DateTime.Now);
  50. _lastRun = Now ();
  51. }
  52. /// <summary>
  53. /// Sends the <paramref name="request"/> immediately or queues it if there is already
  54. /// an outstanding request for the given <see cref="AnsiEscapeSequenceRequest.Terminator"/>.
  55. /// </summary>
  56. /// <param name="request"></param>
  57. /// <returns><see langword="true"/> if request was sent immediately. <see langword="false"/> if it was queued.</returns>
  58. public bool SendOrSchedule (AnsiEscapeSequenceRequest request)
  59. {
  60. return SendOrSchedule (request, true);
  61. }
  62. private bool SendOrSchedule (AnsiEscapeSequenceRequest request,bool addToQueue)
  63. {
  64. if (CanSend (request, out ReasonCannotSend reason))
  65. {
  66. Send (request);
  67. return true;
  68. }
  69. if (reason == ReasonCannotSend.OutstandingRequest)
  70. {
  71. // If we can evict an old request (no response from terminal after ages)
  72. if (EvictStaleRequests (request.Terminator))
  73. {
  74. // Try again after evicting
  75. if (CanSend (request, out _))
  76. {
  77. Send (request);
  78. return true;
  79. }
  80. }
  81. }
  82. if (addToQueue)
  83. {
  84. _queuedRequests.Add (Tuple.Create (request, Now ()));
  85. }
  86. return false;
  87. }
  88. /// <summary>
  89. /// Looks to see if the last time we sent <paramref name="withTerminator"/>
  90. /// is a long time ago. If so we assume that we will never get a response and
  91. /// can proceed with a new request for this terminator (returning <see langword="true"/>).
  92. /// </summary>
  93. /// <param name="withTerminator"></param>
  94. /// <returns></returns>
  95. private bool EvictStaleRequests (string withTerminator)
  96. {
  97. if (_lastSend.TryGetValue (withTerminator, out DateTime dt))
  98. {
  99. if (Now () - dt > _staleTimeout)
  100. {
  101. _parser.StopExpecting (withTerminator, false);
  102. return true;
  103. }
  104. }
  105. return false;
  106. }
  107. /// <summary>
  108. /// Identifies and runs any <see cref="_queuedRequests"/> that can be sent based on the
  109. /// current outstanding requests of the parser.
  110. /// </summary>
  111. /// <param name="force">
  112. /// Repeated requests to run the schedule over short period of time will be ignored.
  113. /// Pass <see langword="true"/> to override this behaviour and force evaluation of outstanding requests.
  114. /// </param>
  115. /// <returns>
  116. /// <see langword="true"/> if a request was found and run. <see langword="false"/>
  117. /// if no outstanding requests or all have existing outstanding requests underway in parser.
  118. /// </returns>
  119. public bool RunSchedule (bool force = false)
  120. {
  121. if (!force && Now () - _lastRun < _runScheduleThrottle)
  122. {
  123. return false;
  124. }
  125. // Get oldest request
  126. Tuple<AnsiEscapeSequenceRequest, DateTime>? opportunity = _queuedRequests.MinBy (r=>r.Item2);
  127. if (opportunity != null)
  128. {
  129. // Give it another go
  130. if (SendOrSchedule (opportunity.Item1, false))
  131. {
  132. _queuedRequests.Remove (opportunity);
  133. return true;
  134. }
  135. }
  136. return false;
  137. }
  138. private void Send (AnsiEscapeSequenceRequest r)
  139. {
  140. _lastSend.AddOrUpdate (r.Terminator, _ => Now (), (_, _) => Now ());
  141. _parser.ExpectResponse (r.Terminator, r.ResponseReceived, false);
  142. r.Send ();
  143. }
  144. private bool CanSend (AnsiEscapeSequenceRequest r, out ReasonCannotSend reason)
  145. {
  146. if (ShouldThrottle (r))
  147. {
  148. reason = ReasonCannotSend.TooManyRequests;
  149. return false;
  150. }
  151. if (_parser.IsExpecting (r.Terminator))
  152. {
  153. reason = ReasonCannotSend.OutstandingRequest;
  154. return false;
  155. }
  156. reason = default (ReasonCannotSend);
  157. return true;
  158. }
  159. private bool ShouldThrottle (AnsiEscapeSequenceRequest r)
  160. {
  161. if (_lastSend.TryGetValue (r.Terminator, out DateTime value))
  162. {
  163. return Now () - value < _throttle;
  164. }
  165. return false;
  166. }
  167. }
  168. internal enum ReasonCannotSend
  169. {
  170. /// <summary>
  171. /// No reason given.
  172. /// </summary>
  173. None = 0,
  174. /// <summary>
  175. /// The parser is already waiting for a request to complete with the given terminator.
  176. /// </summary>
  177. OutstandingRequest,
  178. /// <summary>
  179. /// There have been too many requests sent recently, new requests will be put into
  180. /// queue to prevent console becoming unresponsive.
  181. /// </summary>
  182. TooManyRequests
  183. }