Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Timeout as TimeSpan, Support custom request timeout #2078

Merged
merged 1 commit into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/RestSharp/Options/RestClientOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,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 @@ -136,7 +136,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 @@ -18,6 +18,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 @@ -90,7 +92,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 @@ -216,7 +216,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.UserAgent != null &&
httpClient.DefaultRequestHeaders.UserAgent.All(x => $"{x.Product?.Name}/{x.Product?.Version}" != options.UserAgent)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public async Task Handles_HttpClient_Timeout_Error() {
public async Task Handles_Server_Timeout_Error() {
var client = new RestClient(_server.Url);

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

response.ErrorException.Should().BeOfType<TaskCanceledException>();
Expand All @@ -60,7 +60,7 @@ public async Task Handles_Server_Timeout_Error() {
[Fact]
public async Task Handles_Server_Timeout_Error_With_Deserializer() {
var client = new RestClient(_server.Url);
var request = new RestRequest("404") { Timeout = 500 };
var request = new RestRequest("404") { Timeout = TimeSpan.FromMilliseconds(500) };
var response = await client.ExecuteAsync<TestResponse>(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 @@ -40,7 +40,7 @@ public async Task Can_Timeout_PUT_Async() {
var request = new RestRequest(TimeoutResource, 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 @@ -57,7 +57,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