Skip to content

Commit 4822dcf

Browse files
committed
review
1 parent 6843328 commit 4822dcf

File tree

3 files changed

+23
-21
lines changed

3 files changed

+23
-21
lines changed

clients/algoliasearch-client-java-2/algoliasearch-core/src/main/java/com/algolia/utils/TaskUtils.java

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99

1010
public class TaskUtils {
1111

12+
public static final int DEFAULT_MAX_TRIAL = 50;
13+
public static final IntUnaryOperator DEFAULT_TIMEOUT = (int retries) -> {
14+
return Math.min(retries * 200, 5000);
15+
};
16+
1217
public static <TResponse> void retryUntil(
1318
Supplier<CompletableFuture<TResponse>> func,
1419
Predicate<TResponse> validate,
@@ -19,7 +24,6 @@ public static <TResponse> void retryUntil(
1924
while (retryCount < maxTrial) {
2025
try {
2126
TResponse resp = func.get().get();
22-
System.out.println(resp);
2327
if (validate.test(resp)) {
2428
return;
2529
}
@@ -28,7 +32,6 @@ public static <TResponse> void retryUntil(
2832
return;
2933
}
3034
try {
31-
System.out.println("sleeping for " + timeout.applyAsInt(retryCount));
3235
Thread.sleep(timeout.applyAsInt(retryCount));
3336
} catch (InterruptedException ignored) {
3437
// Restore interrupted state...
@@ -45,18 +48,4 @@ public static <TResponse> void retryUntil(
4548
")"
4649
);
4750
}
48-
49-
public static <TResponse> void retryUntil(
50-
Supplier<CompletableFuture<TResponse>> func,
51-
Predicate<TResponse> validate
52-
) {
53-
retryUntil(
54-
func,
55-
validate,
56-
50,
57-
(int retries) -> {
58-
return Math.min(retries * 200, 5000);
59-
}
60-
);
61-
}
6251
}

clients/algoliasearch-client-javascript/packages/client-common/src/createRetryablePromise.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import type { CreateRetryablePromiseOptions } from './types/CreateRetryablePromise';
22

3+
export const DEFAULT_MAX_TRIAL = 50;
4+
export const DEFAULT_TIMEOUT = (retryCount: number): number =>
5+
Math.min(retryCount * 200, 5000);
6+
37
/**
48
* Return a promise that retry a task until it meets the condition.
59
*
@@ -12,8 +16,8 @@ import type { CreateRetryablePromiseOptions } from './types/CreateRetryablePromi
1216
export function createRetryablePromise<TResponse>({
1317
func,
1418
validate,
15-
maxTrial = 50,
16-
timeout = (retryCount: number): number => Math.min(retryCount * 200, 5000),
19+
maxTrial = DEFAULT_MAX_TRIAL,
20+
timeout = DEFAULT_TIMEOUT,
1721
}: CreateRetryablePromiseOptions<TResponse>): Promise<TResponse> {
1822
let retryCount = 0;
1923
const retry = (): Promise<TResponse> => {

templates/java/libraries/okhttp-gson/api.mustache

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import com.algolia.exceptions.*;
1313
import com.algolia.utils.retry.CallType;
1414
import com.algolia.utils.retry.StatefulHost;
1515
import com.algolia.utils.RequestOptions;
16+
import java.util.function.IntUnaryOperator;
1617

1718
import java.util.EnumSet;
1819
import java.util.Random;
@@ -218,16 +219,24 @@ public class {{classname}} extends ApiClient {
218219
{{/operation}}
219220

220221
{{#isSearchClient}}
221-
public void waitForTask(String indexName, Long taskID, RequestOptions requestOptions) {
222+
public void waitForTask(String indexName, Long taskID, RequestOptions requestOptions, int maxTrial, IntUnaryOperator timeout) {
222223
TaskUtils.retryUntil(() -> {
223224
return this.getTaskAsync(indexName, taskID, requestOptions);
224225
}, (GetTaskResponse task) -> {
225226
return task.getStatus() == TaskStatus.PUBLISHED;
226-
});
227+
}, maxTrial, timeout);
228+
}
229+
230+
public void waitForTask(String indexName, Long taskID, RequestOptions requestOptions) {
231+
this.waitForTask(indexName, taskID, requestOptions, TaskUtils.DEFAULT_MAX_TRIAL, TaskUtils.DEFAULT_TIMEOUT);
232+
}
233+
234+
public void waitForTask(String indexName, Long taskID, int maxTrial, IntUnaryOperator timeout) {
235+
this.waitForTask(indexName, taskID, null, maxTrial, timeout);
227236
}
228237

229238
public void waitForTask(String indexName, Long taskID) {
230-
this.waitForTask(indexName, taskID, null);
239+
this.waitForTask(indexName, taskID, null, TaskUtils.DEFAULT_MAX_TRIAL, TaskUtils.DEFAULT_TIMEOUT);
231240
}
232241
{{/isSearchClient}}
233242
}

0 commit comments

Comments
 (0)