-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
6 changed files
with
108 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
...oliasearch-core/src/main/java/com/algolia/exceptions/AlgoliaRetriesExceededException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.algolia.exceptions; | ||
|
||
/** | ||
* Exception thrown when an error occurs during the waitForTask strategy. For example: maximum | ||
* number of retry exceeded | ||
*/ | ||
public class AlgoliaRetriesExceededException extends AlgoliaRuntimeException { | ||
|
||
public static final long serialVersionUID = 1L; | ||
|
||
public AlgoliaRetriesExceededException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
|
||
public AlgoliaRetriesExceededException(String message) { | ||
super(message); | ||
} | ||
|
||
public AlgoliaRetriesExceededException(Throwable cause) { | ||
super(cause); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
...liasearch-client-java-2/algoliasearch-core/src/main/java/com/algolia/utils/TaskUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.algolia.utils; | ||
|
||
import com.algolia.exceptions.*; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.function.IntUnaryOperator; | ||
import java.util.function.Predicate; | ||
import java.util.function.Supplier; | ||
|
||
public class TaskUtils { | ||
|
||
public static final int DEFAULT_MAX_TRIAL = 50; | ||
public static final IntUnaryOperator DEFAULT_TIMEOUT = (int retries) -> { | ||
return Math.min(retries * 200, 5000); | ||
}; | ||
|
||
public static <TResponse> void retryUntil( | ||
Supplier<CompletableFuture<TResponse>> func, | ||
Predicate<TResponse> validate, | ||
int maxTrial, | ||
IntUnaryOperator timeout | ||
) throws AlgoliaRuntimeException { | ||
int retryCount = 0; | ||
while (retryCount < maxTrial) { | ||
try { | ||
TResponse resp = func.get().get(); | ||
if (validate.test(resp)) { | ||
return; | ||
} | ||
} catch (InterruptedException | ExecutionException e) { | ||
// if the task is interrupted, just return | ||
return; | ||
} | ||
try { | ||
Thread.sleep(timeout.applyAsInt(retryCount)); | ||
} catch (InterruptedException ignored) { | ||
// Restore interrupted state... | ||
Thread.currentThread().interrupt(); | ||
} | ||
|
||
retryCount++; | ||
} | ||
throw new AlgoliaRetriesExceededException( | ||
"The maximum number of trials exceeded. (" + | ||
(retryCount + 1) + | ||
"/" + | ||
maxTrial + | ||
")" | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters