Skip to content

Commit

Permalink
feat(clients): endpoint level timeout part 2 [skip-bc] (generated)
Browse files Browse the repository at this point in the history
algolia/api-clients-automation#4318

Co-authored-by: algolia-bot <[email protected]>
Co-authored-by: Thomas Raffray <[email protected]>
Co-authored-by: Pierre Millot <[email protected]>
  • Loading branch information
3 people committed Jan 13, 2025
1 parent e20d6bb commit dd0be15
Show file tree
Hide file tree
Showing 14 changed files with 97 additions and 40 deletions.
6 changes: 3 additions & 3 deletions algoliasearch/Clients/AbtestingConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ public AbtestingConfig(string appId, string apiKey, string region = null) : base
{
DefaultHosts = GetDefaultHosts(region);
Compression = CompressionType.None;
ReadTimeout = TimeSpan.FromSeconds(5);
WriteTimeout = TimeSpan.FromSeconds(30);
ConnectTimeout = TimeSpan.FromSeconds(2);
ReadTimeout = TimeSpan.FromMilliseconds(5000);
WriteTimeout = TimeSpan.FromMilliseconds(30000);
ConnectTimeout = TimeSpan.FromMilliseconds(2000);
}
private static List<StatefulHost> GetDefaultHosts(string region)
{
Expand Down
6 changes: 3 additions & 3 deletions algoliasearch/Clients/AnalyticsConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ public AnalyticsConfig(string appId, string apiKey, string region = null) : base
{
DefaultHosts = GetDefaultHosts(region);
Compression = CompressionType.None;
ReadTimeout = TimeSpan.FromSeconds(5);
WriteTimeout = TimeSpan.FromSeconds(30);
ConnectTimeout = TimeSpan.FromSeconds(2);
ReadTimeout = TimeSpan.FromMilliseconds(5000);
WriteTimeout = TimeSpan.FromMilliseconds(30000);
ConnectTimeout = TimeSpan.FromMilliseconds(2000);
}
private static List<StatefulHost> GetDefaultHosts(string region)
{
Expand Down
12 changes: 12 additions & 0 deletions algoliasearch/Clients/IngestionClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2823,6 +2823,9 @@ public async Task<WatchResponse> PushTaskAsync(string taskID, PushTaskPayload pu

requestOptions.AddQueryParameter("watch", watch);
requestOptions.Data = pushTaskPayload;
requestOptions.ReadTimeout ??= TimeSpan.FromMilliseconds(180000);
requestOptions.WriteTimeout ??= TimeSpan.FromMilliseconds(180000);
requestOptions.ConnectTimeout ??= TimeSpan.FromMilliseconds(180000);
return await _transport.ExecuteRequestAsync<WatchResponse>(new HttpMethod("POST"), "/2/tasks/{taskID}/push", requestOptions, cancellationToken).ConfigureAwait(false);
}

Expand Down Expand Up @@ -3024,6 +3027,9 @@ public async Task<WatchResponse> TriggerDockerSourceDiscoverAsync(string sourceI

requestOptions.PathParameters.Add("sourceID", QueryStringHelper.ParameterToString(sourceID));

requestOptions.ReadTimeout ??= TimeSpan.FromMilliseconds(180000);
requestOptions.WriteTimeout ??= TimeSpan.FromMilliseconds(180000);
requestOptions.ConnectTimeout ??= TimeSpan.FromMilliseconds(180000);
return await _transport.ExecuteRequestAsync<WatchResponse>(new HttpMethod("POST"), "/1/sources/{sourceID}/discover", requestOptions, cancellationToken).ConfigureAwait(false);
}

Expand Down Expand Up @@ -3235,6 +3241,9 @@ public async Task<WatchResponse> ValidateSourceAsync(SourceCreate sourceCreate =


requestOptions.Data = sourceCreate;
requestOptions.ReadTimeout ??= TimeSpan.FromMilliseconds(180000);
requestOptions.WriteTimeout ??= TimeSpan.FromMilliseconds(180000);
requestOptions.ConnectTimeout ??= TimeSpan.FromMilliseconds(180000);
return await _transport.ExecuteRequestAsync<WatchResponse>(new HttpMethod("POST"), "/1/sources/validate", requestOptions, cancellationToken).ConfigureAwait(false);
}

Expand All @@ -3260,6 +3269,9 @@ public async Task<WatchResponse> ValidateSourceBeforeUpdateAsync(string sourceID
requestOptions.PathParameters.Add("sourceID", QueryStringHelper.ParameterToString(sourceID));

requestOptions.Data = sourceUpdate;
requestOptions.ReadTimeout ??= TimeSpan.FromMilliseconds(180000);
requestOptions.WriteTimeout ??= TimeSpan.FromMilliseconds(180000);
requestOptions.ConnectTimeout ??= TimeSpan.FromMilliseconds(180000);
return await _transport.ExecuteRequestAsync<WatchResponse>(new HttpMethod("POST"), "/1/sources/{sourceID}/validate", requestOptions, cancellationToken).ConfigureAwait(false);
}

Expand Down
6 changes: 3 additions & 3 deletions algoliasearch/Clients/IngestionConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ public IngestionConfig(string appId, string apiKey, string region) : base(appId,
{
DefaultHosts = GetDefaultHosts(region);
Compression = CompressionType.None;
ReadTimeout = TimeSpan.FromSeconds(25);
WriteTimeout = TimeSpan.FromSeconds(25);
ConnectTimeout = TimeSpan.FromSeconds(25);
ReadTimeout = TimeSpan.FromMilliseconds(25000);
WriteTimeout = TimeSpan.FromMilliseconds(25000);
ConnectTimeout = TimeSpan.FromMilliseconds(25000);
}
private static List<StatefulHost> GetDefaultHosts(string region)
{
Expand Down
6 changes: 3 additions & 3 deletions algoliasearch/Clients/InsightsConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ public InsightsConfig(string appId, string apiKey, string region = null) : base(
{
DefaultHosts = GetDefaultHosts(region);
Compression = CompressionType.None;
ReadTimeout = TimeSpan.FromSeconds(5);
WriteTimeout = TimeSpan.FromSeconds(30);
ConnectTimeout = TimeSpan.FromSeconds(2);
ReadTimeout = TimeSpan.FromMilliseconds(5000);
WriteTimeout = TimeSpan.FromMilliseconds(30000);
ConnectTimeout = TimeSpan.FromMilliseconds(2000);
}
private static List<StatefulHost> GetDefaultHosts(string region)
{
Expand Down
6 changes: 3 additions & 3 deletions algoliasearch/Clients/MonitoringConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ public MonitoringConfig(string appId, string apiKey) : base(appId, apiKey, "Moni
{
DefaultHosts = GetDefaultHosts();
Compression = CompressionType.None;
ReadTimeout = TimeSpan.FromSeconds(5);
WriteTimeout = TimeSpan.FromSeconds(30);
ConnectTimeout = TimeSpan.FromSeconds(2);
ReadTimeout = TimeSpan.FromMilliseconds(5000);
WriteTimeout = TimeSpan.FromMilliseconds(30000);
ConnectTimeout = TimeSpan.FromMilliseconds(2000);
}
private static List<StatefulHost> GetDefaultHosts()
{
Expand Down
6 changes: 3 additions & 3 deletions algoliasearch/Clients/PersonalizationConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ public PersonalizationConfig(string appId, string apiKey, string region) : base(
{
DefaultHosts = GetDefaultHosts(region);
Compression = CompressionType.None;
ReadTimeout = TimeSpan.FromSeconds(5);
WriteTimeout = TimeSpan.FromSeconds(30);
ConnectTimeout = TimeSpan.FromSeconds(2);
ReadTimeout = TimeSpan.FromMilliseconds(5000);
WriteTimeout = TimeSpan.FromMilliseconds(30000);
ConnectTimeout = TimeSpan.FromMilliseconds(2000);
}
private static List<StatefulHost> GetDefaultHosts(string region)
{
Expand Down
6 changes: 3 additions & 3 deletions algoliasearch/Clients/QuerySuggestionsConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ public QuerySuggestionsConfig(string appId, string apiKey, string region) : base
{
DefaultHosts = GetDefaultHosts(region);
Compression = CompressionType.None;
ReadTimeout = TimeSpan.FromSeconds(5);
WriteTimeout = TimeSpan.FromSeconds(30);
ConnectTimeout = TimeSpan.FromSeconds(2);
ReadTimeout = TimeSpan.FromMilliseconds(5000);
WriteTimeout = TimeSpan.FromMilliseconds(30000);
ConnectTimeout = TimeSpan.FromMilliseconds(2000);
}
private static List<StatefulHost> GetDefaultHosts(string region)
{
Expand Down
6 changes: 3 additions & 3 deletions algoliasearch/Clients/RecommendConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ public RecommendConfig(string appId, string apiKey) : base(appId, apiKey, "Recom
{
DefaultHosts = GetDefaultHosts(appId);
Compression = CompressionType.None;
ReadTimeout = TimeSpan.FromSeconds(5);
WriteTimeout = TimeSpan.FromSeconds(30);
ConnectTimeout = TimeSpan.FromSeconds(2);
ReadTimeout = TimeSpan.FromMilliseconds(5000);
WriteTimeout = TimeSpan.FromMilliseconds(30000);
ConnectTimeout = TimeSpan.FromMilliseconds(2000);
}
private static List<StatefulHost> GetDefaultHosts(string appId)
{
Expand Down
6 changes: 3 additions & 3 deletions algoliasearch/Clients/SearchConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ public SearchConfig(string appId, string apiKey) : base(appId, apiKey, "Search",
{
DefaultHosts = GetDefaultHosts(appId);
Compression = CompressionType.None;
ReadTimeout = TimeSpan.FromSeconds(5);
WriteTimeout = TimeSpan.FromSeconds(30);
ConnectTimeout = TimeSpan.FromSeconds(2);
ReadTimeout = TimeSpan.FromMilliseconds(5000);
WriteTimeout = TimeSpan.FromMilliseconds(30000);
ConnectTimeout = TimeSpan.FromMilliseconds(2000);
}
private static List<StatefulHost> GetDefaultHosts(string appId)
{
Expand Down
18 changes: 15 additions & 3 deletions algoliasearch/Http/InternalRequestOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ public InternalRequestOptions(RequestOptions options = null)

CustomPathParameters = new Dictionary<string, string>();
PathParameters = new Dictionary<string, string>();
Timeout = options?.Timeout;
ConnectTimeout = options?.ConnectTimeout;
ReadTimeout = options?.ReadTimeout;
WriteTimeout = options?.WriteTimeout;
}

public void AddQueryParameter(string key, object value)
Expand Down Expand Up @@ -76,9 +78,19 @@ public void AddCustomQueryParameters(IDictionary<string, object> values)
public object Data { get; set; }

/// <summary>
/// Request timeout
/// Request read timeout
/// </summary>
public TimeSpan? Timeout { get; set; }
public TimeSpan? ReadTimeout { get; set; }

/// <summary>
/// Request write timeout
/// </summary>
public TimeSpan? WriteTimeout { get; set; }

/// <summary>
/// Request connect timeout
/// </summary>
public TimeSpan? ConnectTimeout { get; set; }

/// <summary>
/// Enforce the Read Transporter
Expand Down
28 changes: 25 additions & 3 deletions algoliasearch/Http/RequestOptionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,35 @@ public RequestOptionBuilder AddExtraQueryParameters(string key, object value)
}

/// <summary>
/// Set the request timeout
/// Set the request read timeout
/// </summary>
/// <param name="timeout"></param>
/// <returns></returns>
public RequestOptionBuilder SetTimeout(TimeSpan timeout)
public RequestOptionBuilder SetReadTimeout(TimeSpan timeout)
{
_options.Timeout = timeout;
_options.ReadTimeout = timeout;
return this;
}

/// <summary>
/// Set the request write timeout
/// </summary>
/// <param name="timeout"></param>
/// <returns></returns>
public RequestOptionBuilder SetWriteTimeout(TimeSpan timeout)
{
_options.WriteTimeout = timeout;
return this;
}

/// <summary>
/// Set the request connect timeout
/// </summary>
/// <param name="timeout"></param>
/// <returns></returns>
public RequestOptionBuilder SetConnectTimeout(TimeSpan timeout)
{
_options.ConnectTimeout = timeout;
return this;
}

Expand Down
14 changes: 12 additions & 2 deletions algoliasearch/Http/RequestOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,19 @@ public class RequestOptions
public IDictionary<string, object> QueryParameters { get; set; }

/// <summary>
/// Request timeout in seconds
/// Request timeout
/// </summary>
public TimeSpan? Timeout { get; set; }
public TimeSpan? ReadTimeout { get; set; }

/// <summary>
/// Request timeout
/// </summary>
public TimeSpan? WriteTimeout { get; set; }

/// <summary>
/// Request timeout
/// </summary>
public TimeSpan? ConnectTimeout { get; set; }

/// <summary>
/// Constructs a new instance of <see cref="RequestOptions"/>
Expand Down
11 changes: 6 additions & 5 deletions algoliasearch/Transport/HttpTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ private async Task<TResult> ExecuteRequestAsync<TResult, TData>(HttpMethod metho
request.Uri = BuildUri(host, uri, requestOptions?.CustomPathParameters, requestOptions?.PathParameters,
requestOptions?.QueryParameters);
var requestTimeout =
TimeSpan.FromTicks((requestOptions?.Timeout ?? GetTimeOut(callType)).Ticks * (host.RetryCount + 1));
TimeSpan.FromTicks((GetTimeOut(callType, requestOptions)).Ticks * (host.RetryCount + 1));

if (request.Body == null && (method == HttpMethod.Post || method == HttpMethod.Put))
{
Expand All @@ -137,7 +137,7 @@ private async Task<TResult> ExecuteRequestAsync<TResult, TData>(HttpMethod metho
}

var response = await _httpClient
.SendRequestAsync(request, requestTimeout, _algoliaConfig.ConnectTimeout ?? Defaults.ConnectTimeout, ct)
.SendRequestAsync(request, requestTimeout, requestOptions?.ConnectTimeout ?? _algoliaConfig.ConnectTimeout ?? Defaults.ConnectTimeout, ct)
.ConfigureAwait(false);

_errorMessage = response.Error;
Expand Down Expand Up @@ -280,13 +280,14 @@ private static Uri BuildUri(StatefulHost host, string baseUri,
/// Compute the request timeout with the given call type and configuration
/// </summary>
/// <param name="callType"></param>
/// <param name="requestOptions"></param>
/// <returns></returns>
private TimeSpan GetTimeOut(CallType callType)
private TimeSpan GetTimeOut(CallType callType, InternalRequestOptions requestOptions = null)
{
return callType switch
{
CallType.Read => _algoliaConfig.ReadTimeout ?? Defaults.ReadTimeout,
CallType.Write => _algoliaConfig.WriteTimeout ?? Defaults.WriteTimeout,
CallType.Read => requestOptions?.ReadTimeout ?? _algoliaConfig.ReadTimeout ?? Defaults.ReadTimeout,
CallType.Write => requestOptions?.WriteTimeout ?? _algoliaConfig.WriteTimeout ?? Defaults.WriteTimeout,
_ => Defaults.WriteTimeout
};
}
Expand Down

0 comments on commit dd0be15

Please sign in to comment.