Skip to content

Commit

Permalink
Merge timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeyzimarev committed May 22, 2024
2 parents 6891792 + c9482f3 commit 9117b7a
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 10 deletions.
5 changes: 2 additions & 3 deletions src/RestSharp/Options/RestClientOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,9 @@ public RestClientOptions(string baseUrl) : this(new Uri(Ensure.NotEmptyString(ba
public CookieContainer? CookieContainer { get; set; }

/// <summary>
/// Maximum request duration in milliseconds. When the request timeout is specified using <seealso cref="RestRequest.Timeout"/>,
/// the lowest value between the client timeout and request timeout will be used.
/// Request duration. Used when the request timeout is not specified using <seealso cref="RestRequest.Timeout"/>,
/// </summary>
public int MaxTimeout { get; set; }
public TimeSpan? Timeout { get; set; }

/// <summary>
/// Default encoding to use when no encoding is specified in the content type header.
Expand Down
2 changes: 1 addition & 1 deletion src/RestSharp/Request/RestRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public RestRequest(Uri resource, Method method = Method.Get)
/// <summary>
/// Custom request timeout
/// </summary>
public int Timeout { get; set; }
public TimeSpan? Timeout { get; set; }

/// <summary>
/// The Resource URL to make the request against.
Expand Down
4 changes: 3 additions & 1 deletion src/RestSharp/RestClient.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
namespace RestSharp;

public partial class RestClient {
// Default HttpClient timeout
public TimeSpan DefaultTimeout = TimeSpan.FromSeconds(100);
/// <inheritdoc />
public async Task<RestResponse> ExecuteAsync(RestRequest request, CancellationToken cancellationToken = default) {
using var internalResponse = await ExecuteRequestAsync(request, cancellationToken).ConfigureAwait(false);
Expand Down Expand Up @@ -113,7 +115,7 @@ async Task<HttpResponse> ExecuteRequestAsync(RestRequest request, CancellationTo
message.Headers.Host = Options.BaseHost;
message.Headers.CacheControl = request.CachePolicy ?? Options.CachePolicy;

using var timeoutCts = new CancellationTokenSource(request.Timeout > 0 ? request.Timeout : int.MaxValue);
using var timeoutCts = new CancellationTokenSource(request.Timeout ?? Options.Timeout ?? DefaultTimeout);
using var cts = CancellationTokenSource.CreateLinkedTokenSource(timeoutCts.Token, cancellationToken);

var ct = cts.Token;
Expand Down
4 changes: 3 additions & 1 deletion src/RestSharp/RestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,9 @@ public RestClient(
: this(new HttpClient(handler, disposeHandler), true, configureRestClient, configureSerialization) { }

static void ConfigureHttpClient(HttpClient httpClient, RestClientOptions options) {
if (options.MaxTimeout > 0) httpClient.Timeout = TimeSpan.FromMilliseconds(options.MaxTimeout);

// We will use Options.Timeout in ExecuteAsInternalAsync method
httpClient.Timeout = Timeout.InfiniteTimeSpan;

if (options.Expect100Continue != null) httpClient.DefaultRequestHeaders.ExpectContinue = options.Expect100Continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public async Task Handles_HttpClient_Timeout_Error() {
public async Task Handles_Server_Timeout_Error() {
using var client = new RestClient(_server.Url!);

var request = new RestRequest("timeout") { Timeout = 500 };
var request = new RestRequest("404") { Timeout = TimeSpan.FromMilliseconds(500) };
var response = await client.ExecuteAsync(request);

response.ErrorException.Should().BeOfType<TaskCanceledException>();
Expand All @@ -56,7 +56,7 @@ public async Task Handles_Server_Timeout_Error() {
public async Task Handles_Server_Timeout_Error_With_Deserializer() {
using var client = new RestClient(_server.Url!);

var request = new RestRequest("timeout") { Timeout = 500 };
var request = new RestRequest("timeout") { Timeout = TimeSpan.FromMilliseconds(500) };
var response = await client.ExecuteAsync<SuccessResponse>(request);

response.Data.Should().BeNull();
Expand Down
2 changes: 1 addition & 1 deletion test/RestSharp.Tests.Integrated/PutTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public async Task Can_Timeout_PUT_Async() {
var request = new RestRequest("/timeout", Method.Put).AddBody("Body_Content");

// Half the value of ResponseHandler.Timeout
request.Timeout = 200;
request.Timeout = TimeSpan.FromMilliseconds(200);

var response = await _client.ExecuteAsync(request);

Expand Down
2 changes: 1 addition & 1 deletion test/RestSharp.Tests.Integrated/RequestTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public async Task Can_Timeout_GET_Async() {
var request = new RestRequest("timeout").AddBody("Body_Content");

// Half the value of ResponseHandler.Timeout
request.Timeout = 200;
request.Timeout = TimeSpan.FromMilliseconds(200);

var response = await _client.ExecuteAsync(request);

Expand Down

0 comments on commit 9117b7a

Please sign in to comment.