-
Notifications
You must be signed in to change notification settings - Fork 3
feat: auto retry on deployment-related 499 errors #240
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
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
abda35b
chore: implement retryOnException
duyhungtnn 8838f63
fix: lint fail
duyhungtnn f06b1ae
feat: retry on 499 when register events
duyhungtnn cb84755
fix: lint fail
duyhungtnn 97890dd
Refactor retryOnException and update API client usage
duyhungtnn c95b223
feat: use retry on register event
duyhungtnn 67978f2
test: add test-case for retry logic
duyhungtnn 64b85df
fix: can not retry
duyhungtnn 8899e1b
test: get evaluation
duyhungtnn f83e3f1
Add registerEvents retry and error handling tests
duyhungtnn 419a20c
Rename and update retryOnException function and tests
duyhungtnn 678cea2
Skip CLIENT_CLOSED_REQUEST test in ApiClientImplTest
duyhungtnn 8ac2f3c
Fix comment formatting in ApiClientImplTest
duyhungtnn b2072d1
Fix retry loop to use correct attempt range
duyhungtnn 4c1c5ab
chore: remove unused test
duyhungtnn 14f2673
Add documentation and comments to retryOnException
duyhungtnn dececc9
Fix KDoc formatting in RetryOnException.kt
duyhungtnn 7526ef0
Add tests for fetchEvaluations retry logic on 499 errors
duyhungtnn 44c5a85
Clone requests before reuse in ApiClientImpl
duyhungtnn e59782e
Clarify backoff strategy in RetryOnException docs
duyhungtnn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
40 changes: 40 additions & 0 deletions
40
bucketeer/src/main/kotlin/io/bucketeer/sdk/android/internal/remote/RetryOnException.kt
This file contains hidden or 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,40 @@ | ||
| package io.bucketeer.sdk.android.internal.remote | ||
|
|
||
| /** | ||
| * Retries the given [block] of code up to [maxRetries] times if it throws an exception | ||
| * that satisfies [exceptionCheck]. Linear backoff delay is applied between retries. | ||
| * | ||
| * Note: This function blocks the current thread during delays. Use only on background threads. | ||
| * | ||
| * @param maxRetries Maximum retry attempts (default: 3). | ||
| * @param delayMillis Base delay in milliseconds between retries (default: 1000ms). | ||
| * @param exceptionCheck Predicate to determine if an exception is retriable. | ||
| * @param block Code block to execute. | ||
| * @return Result of [block] if successful. | ||
| * @throws Throwable Last exception if retries fail or exception is not retriable. | ||
| */ | ||
| internal fun <T> retryOnException( | ||
| maxRetries: Int = 3, | ||
| delayMillis: Long = 1000, | ||
| exceptionCheck: (Throwable) -> Boolean, | ||
| block: () -> T, | ||
| ): T { | ||
| var lastException: Throwable? = null | ||
| val maxAttempts = if (maxRetries < 0) 1 else maxRetries + 1 | ||
| for (attempt in 0 until maxAttempts) { | ||
| try { | ||
| return block() | ||
| } catch (e: Throwable) { | ||
| lastException = e | ||
| if (!exceptionCheck(e) || attempt >= maxAttempts - 1) { | ||
| throw e | ||
| } | ||
| // Delay with linear backoff using Thread.sleep. | ||
| // Ensure this runs on a background thread as it blocks the current thread. | ||
| // Coroutine support is not used since the SDK does not rely on coroutines. | ||
| Thread.sleep(delayMillis * (attempt + 1)) | ||
duyhungtnn marked this conversation as resolved.
Show resolved
Hide resolved
duyhungtnn marked this conversation as resolved.
Show resolved
Hide resolved
duyhungtnn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| // This line should never be reached, but Kotlin compiler requires a return statement here. | ||
| throw lastException!! | ||
duyhungtnn marked this conversation as resolved.
Show resolved
Hide resolved
duyhungtnn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.