TimeoutSettings.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System;
  2. using Best.HTTP.Shared;
  3. namespace Best.HTTP.Request.Settings
  4. {
  5. /// <summary>
  6. /// Represents settings related to connection-timeouts and processing duration.
  7. /// </summary>
  8. public class TimeoutSettings
  9. {
  10. /// <summary>
  11. /// Gets the timestamp when the request was queued for processing.
  12. /// </summary>
  13. public DateTime QueuedAt { get; internal set; }
  14. /// <summary>
  15. /// Gets the timestamp when the processing of the request started by a connection.
  16. /// </summary>
  17. public DateTime ProcessingStarted { get; internal set; }
  18. /// <summary>
  19. /// Gets or sets the maximum time to wait for establishing the connection to the target server.
  20. /// If set to <c>TimeSpan.Zero</c> or lower, no connect timeout logic is executed. Default value is 20 seconds.
  21. /// </summary>
  22. public TimeSpan ConnectTimeout
  23. {
  24. get => this._connectTimeout ?? HTTPManager.PerHostSettings.Get(this._request.CurrentHostKey.Host).RequestSettings.ConnectTimeout;
  25. set => this._connectTimeout = value;
  26. }
  27. private TimeSpan? _connectTimeout;
  28. /// <summary>
  29. /// Gets or sets the maximum time to wait for the request to finish after the connection is established.
  30. /// </summary>
  31. public TimeSpan Timeout
  32. {
  33. get => this._timeout ?? HTTPManager.PerHostSettings.Get(this._request.CurrentHostKey.Host).RequestSettings.RequestTimeout;
  34. set => this._timeout = value;
  35. }
  36. private TimeSpan? _timeout;
  37. /// <summary>
  38. /// Returns <c>true</c> if the request has been stuck in the connection phase for too long.
  39. /// </summary>
  40. /// <param name="now">The current timestamp.</param>
  41. /// <returns><c>true</c> if the connection has timed out; otherwise, <c>false</c>.</returns>
  42. public bool IsConnectTimedOut(DateTime now) => this.QueuedAt != DateTime.MinValue && now - this.QueuedAt > this.ConnectTimeout;
  43. /// <summary>
  44. /// Returns <c>true</c> if the time has passed the specified Timeout setting since processing started or if the connection has timed out.
  45. /// </summary>
  46. /// <param name="now">The current timestamp.</param>
  47. /// <returns><c>true</c> if the request has timed out; otherwise, <c>false</c>.</returns>
  48. public bool IsTimedOut(DateTime now)
  49. {
  50. bool result = (this.ProcessingStarted != DateTime.MinValue && now - this.ProcessingStarted > this.Timeout) || this.IsConnectTimedOut(now); ;
  51. return result;
  52. }
  53. private HTTPRequest _request;
  54. /// <summary>
  55. /// Initializes a new instance of the TimeoutSettings class for a specific <see cref="HTTPRequest"/>.
  56. /// </summary>
  57. /// <param name="request">The <see cref="HTTPRequest"/> associated with these timeout settings.</param>
  58. public TimeoutSettings(HTTPRequest request)
  59. => this._request = request;
  60. public void SetProcessing(DateTime now)
  61. {
  62. this.QueuedAt = DateTime.MinValue;
  63. this.ProcessingStarted = now;
  64. }
  65. }
  66. }