Skip to content

Commit

Permalink
fix(csharp): add nextDelay parameter in Wait helpers
Browse files Browse the repository at this point in the history
algolia/api-clients-automation#2710

Co-authored-by: Morgan Leroi <morgan.leroi@gmail.com>
  • Loading branch information
algolia-bot and morganleroi committed Feb 16, 2024
1 parent 567c960 commit f6ab17a
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions algoliasearch/Utils/ClientExtensions.cs
Expand Up @@ -24,14 +24,16 @@ public static class ClientExtensions
/// <param name="indexName">The `indexName` where the operation was performed.</param>
/// <param name="taskId">The `taskID` returned in the method response.</param>
/// <param name="maxRetries">The maximum number of retry. 50 by default. (optional)</param>
/// <param name="timeout">The function to decide how long to wait between retries. Math.Min(retryCount * 200, 5000) by default. (optional)</param>
/// <param name="requestOptions">The requestOptions to send along with the query, they will be merged with the transporter requestOptions. (optional)</param>
/// <param name="ct">Cancellation token (optional)</param>
public static async Task<GetTaskResponse> WaitForTaskAsync(this SearchClient client, string indexName, long taskId,
int maxRetries = DefaultMaxRetries, RequestOptions requestOptions = null, CancellationToken ct = default)
int maxRetries = DefaultMaxRetries, Func<int, int> timeout = null, RequestOptions requestOptions = null,
CancellationToken ct = default)
{
return await RetryUntil(
async () => await client.GetTaskAsync(indexName, taskId, requestOptions, ct),
resp => resp.Status == Models.Search.TaskStatus.Published, maxRetries, ct).ConfigureAwait(false);
resp => resp.Status == Models.Search.TaskStatus.Published, maxRetries, timeout, ct).ConfigureAwait(false);
}

/// <summary>
Expand All @@ -41,11 +43,14 @@ public static class ClientExtensions
/// <param name="indexName">The `indexName` where the operation was performed.</param>
/// <param name="taskId">The `taskID` returned in the method response.</param>
/// <param name="maxRetries">The maximum number of retry. 50 by default. (optional)</param>
/// <param name="timeout">The function to decide how long to wait between retries. Math.Min(retryCount * 200, 5000) by default. (optional)</param>
/// <param name="requestOptions">The requestOptions to send along with the query, they will be merged with the transporter requestOptions. (optional)</param>
/// <param name="ct">Cancellation token (optional)</param>
public static GetTaskResponse WaitForTask(this SearchClient client, string indexName, long taskId,
int maxRetries = DefaultMaxRetries, RequestOptions requestOptions = null, CancellationToken ct = default) =>
AsyncHelper.RunSync(() => client.WaitForTaskAsync(indexName, taskId, maxRetries, requestOptions, ct));
int maxRetries = DefaultMaxRetries, Func<int, int> timeout = null, RequestOptions requestOptions = null,
CancellationToken ct = default) =>
AsyncHelper.RunSync(() =>
client.WaitForTaskAsync(indexName, taskId, maxRetries, timeout, requestOptions, ct));

/// <summary>
/// Helper method that waits for an API key task to be processed.
Expand All @@ -55,11 +60,13 @@ public static class ClientExtensions
/// <param name="key">The key that has been added, deleted or updated.</param>
/// <param name="apiKey">Necessary to know if an `update` operation has been processed, compare fields of the response with it. (optional - mandatory if operation is UPDATE)</param>
/// <param name="maxRetries">The maximum number of retry. 50 by default. (optional)</param>
/// <param name="timeout">The function to decide how long to wait between retries. Math.Min(retryCount * 200, 5000) by default. (optional)</param>
/// <param name="requestOptions">The requestOptions to send along with the query, they will be merged with the transporter requestOptions. (optional)</param>
/// <param name="ct">Cancellation token (optional)</param>
public static async Task<GetApiKeyResponse> WaitForApiKeyAsync(this SearchClient client,
ApiKeyOperation operation, string key,
ApiKey apiKey = default, int maxRetries = DefaultMaxRetries, RequestOptions requestOptions = null,
ApiKey apiKey = default, int maxRetries = DefaultMaxRetries, Func<int, int> timeout = null,
RequestOptions requestOptions = null,
CancellationToken ct = default)
{
if (operation == ApiKeyOperation.Update)
Expand All @@ -84,7 +91,7 @@ public static class ClientExtensions
MaxQueriesPerIPPerHour = resp.MaxQueriesPerIPPerHour
};
return apiKeyResponse.Equals(apiKey);
}, maxRetries: maxRetries, ct: ct).ConfigureAwait(false);
}, maxRetries, timeout, ct).ConfigureAwait(false);
}

var addedKey = new GetApiKeyResponse();
Expand Down Expand Up @@ -115,8 +122,7 @@ public static class ClientExtensions
_ => false
};
},
maxRetries, ct
);
maxRetries, timeout, ct);
return addedKey;
}

Expand All @@ -128,13 +134,14 @@ public static class ClientExtensions
/// <param name="key">The key that has been added, deleted or updated.</param>
/// <param name="apiKey">Necessary to know if an `update` operation has been processed, compare fields of the response with it. (optional - mandatory if operation is UPDATE)</param>
/// <param name="maxRetries">The maximum number of retry. 50 by default. (optional)</param>
/// <param name="timeout">The function to decide how long to wait between retries. Math.Min(retryCount * 200, 5000) by default. (optional)</param>
/// <param name="requestOptions">The requestOptions to send along with the query, they will be merged with the transporter requestOptions. (optional)</param>
/// <param name="ct">Cancellation token (optional)</param>
public static GetApiKeyResponse WaitForApiKey(this SearchClient client,
ApiKeyOperation operation, string key,
ApiKey apiKey = default, int maxRetries = DefaultMaxRetries, RequestOptions requestOptions = null,
ApiKey apiKey = default, int maxRetries = DefaultMaxRetries, Func<int, int> timeout = null, RequestOptions requestOptions = null,
CancellationToken ct = default) =>
AsyncHelper.RunSync(() => client.WaitForApiKeyAsync(operation, key, apiKey, maxRetries, requestOptions, ct));
AsyncHelper.RunSync(() => client.WaitForApiKeyAsync(operation, key, apiKey, maxRetries, timeout, requestOptions, ct));


/// <summary>
Expand Down Expand Up @@ -260,7 +267,7 @@ public static class ClientExtensions
/// <exception cref="Algolia.Search.Exceptions.AlgoliaUnreachableHostException">Thrown when the client failed to call the endpoint</exception>
/// <returns>Task of List{SearchResponse{T}}</returns>
public static async Task<List<SearchResponse<T>>> SearchForHitsAsync<T>(this SearchClient client,
IEnumerable<SearchForHits> requests, SearchStrategy? searchStrategy, RequestOptions options = null,
IEnumerable<SearchForHits> requests, SearchStrategy? searchStrategy = null, RequestOptions options = null,
CancellationToken cancellationToken = default)
{
var queries = requests.Select(t => new SearchQuery(t)).ToList();
Expand All @@ -282,7 +289,7 @@ public static class ClientExtensions
/// <exception cref="Algolia.Search.Exceptions.AlgoliaUnreachableHostException">Thrown when the client failed to call the endpoint</exception>
/// <returns>Task of List{SearchResponse{T}}</returns>
public static List<SearchResponse<T>> SearchForHits<T>(this SearchClient client,
IEnumerable<SearchForHits> requests, SearchStrategy? searchStrategy, RequestOptions options = null,
IEnumerable<SearchForHits> requests, SearchStrategy? searchStrategy = null, RequestOptions options = null,
CancellationToken cancellationToken = default) =>
AsyncHelper.RunSync(() =>
client.SearchForHitsAsync<T>(requests, searchStrategy, options, cancellationToken));
Expand All @@ -307,7 +314,8 @@ public static class ClientExtensions
var queries = requests.Select(t => new SearchQuery(t)).ToList();
var searchMethod = new SearchMethodParams(queries) { Strategy = searchStrategy };
var searchResponses = await client.SearchAsync<object>(searchMethod, options, cancellationToken);
return searchResponses.Results.Where(x => x.IsSearchForFacetValuesResponse()).Select(x => x.AsSearchForFacetValuesResponse()).ToList();
return searchResponses.Results.Where(x => x.IsSearchForFacetValuesResponse())
.Select(x => x.AsSearchForFacetValuesResponse()).ToList();
}

/// <summary>
Expand All @@ -328,8 +336,10 @@ public static class ClientExtensions
AsyncHelper.RunSync(() => client.SearchForFacetsAsync(requests, searchStrategy, options, cancellationToken));

private static async Task<T> RetryUntil<T>(Func<Task<T>> func, Func<T, bool> validate,
int maxRetries = DefaultMaxRetries, CancellationToken ct = default)
int maxRetries = DefaultMaxRetries, Func<int, int> timeout = null, CancellationToken ct = default)
{
timeout ??= NextDelay;

var retryCount = 0;
while (retryCount < maxRetries)
{
Expand All @@ -339,7 +349,7 @@ public static class ClientExtensions
return resp;
}

await Task.Delay(NextDelay(retryCount), ct).ConfigureAwait(false);
await Task.Delay(timeout(retryCount), ct).ConfigureAwait(false);
retryCount++;
}

Expand Down

0 comments on commit f6ab17a

Please sign in to comment.