using System;
using Best.HTTP.Shared;
namespace Best.HTTP.Request.Settings
{
///
/// Represents settings related to connection-timeouts and processing duration.
///
public class TimeoutSettings
{
///
/// Gets the timestamp when the request was queued for processing.
///
public DateTime QueuedAt { get; internal set; }
///
/// Gets the timestamp when the processing of the request started by a connection.
///
public DateTime ProcessingStarted { get; internal set; }
///
/// Gets or sets the maximum time to wait for establishing the connection to the target server.
/// If set to TimeSpan.Zero or lower, no connect timeout logic is executed. Default value is 20 seconds.
///
public TimeSpan ConnectTimeout
{
get => this._connectTimeout ?? HTTPManager.PerHostSettings.Get(this._request.CurrentHostKey.Host).RequestSettings.ConnectTimeout;
set => this._connectTimeout = value;
}
private TimeSpan? _connectTimeout;
///
/// Gets or sets the maximum time to wait for the request to finish after the connection is established.
///
public TimeSpan Timeout
{
get => this._timeout ?? HTTPManager.PerHostSettings.Get(this._request.CurrentHostKey.Host).RequestSettings.RequestTimeout;
set => this._timeout = value;
}
private TimeSpan? _timeout;
///
/// Returns true if the request has been stuck in the connection phase for too long.
///
/// The current timestamp.
/// true if the connection has timed out; otherwise, false.
public bool IsConnectTimedOut(DateTime now) => this.QueuedAt != DateTime.MinValue && now - this.QueuedAt > this.ConnectTimeout;
///
/// Returns true if the time has passed the specified Timeout setting since processing started or if the connection has timed out.
///
/// The current timestamp.
/// true if the request has timed out; otherwise, false.
public bool IsTimedOut(DateTime now)
{
bool result = (this.ProcessingStarted != DateTime.MinValue && now - this.ProcessingStarted > this.Timeout) || this.IsConnectTimedOut(now); ;
return result;
}
private HTTPRequest _request;
///
/// Initializes a new instance of the TimeoutSettings class for a specific .
///
/// The associated with these timeout settings.
public TimeoutSettings(HTTPRequest request)
=> this._request = request;
public void SetProcessing(DateTime now)
{
this.QueuedAt = DateTime.MinValue;
this.ProcessingStarted = now;
}
}
}