-
Notifications
You must be signed in to change notification settings - Fork 241
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(amazonq): Adding backoff and retry for export result archive stre…
…aming API. (#5320) * Adding backoff and retry for export result archive.
- Loading branch information
1 parent
8318155
commit ac43387
Showing
4 changed files
with
252 additions
and
60 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
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
53 changes: 53 additions & 0 deletions
53
...ains-community/src/software/aws/toolkits/jetbrains/services/amazonq/RetryableOperation.kt
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,53 @@ | ||
// Copyright 2025 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package software.aws.toolkits.jetbrains.services.amazonq | ||
|
||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.runBlocking | ||
import software.amazon.awssdk.core.exception.RetryableException | ||
import kotlin.random.Random | ||
|
||
class RetryableOperation<T> { | ||
private var attempts = 0 | ||
private var currentDelay = INITIAL_DELAY | ||
|
||
private fun getJitteredDelay(): Long { | ||
currentDelay = (currentDelay * 2).coerceAtMost(MAX_BACKOFF) | ||
return (currentDelay * (0.5 + Random.nextDouble(0.5))).toLong() | ||
} | ||
|
||
fun execute( | ||
operation: () -> T, | ||
isRetryable: (Exception) -> Boolean = { it is RetryableException }, | ||
errorHandler: ((Exception, Int) -> Nothing), | ||
): T = runBlocking { | ||
executeSuspend(operation, isRetryable, errorHandler) | ||
} | ||
|
||
suspend fun executeSuspend( | ||
operation: suspend () -> T, | ||
isRetryable: (Exception) -> Boolean = { it is RetryableException }, | ||
errorHandler: (suspend (Exception, Int) -> Nothing), | ||
): T { | ||
while (attempts < MAX_RETRY_ATTEMPTS) { | ||
try { | ||
return operation() | ||
} catch (e: Exception) { | ||
attempts++ | ||
if (attempts >= MAX_RETRY_ATTEMPTS || !isRetryable(e)) { | ||
errorHandler.invoke(e, attempts) | ||
} | ||
delay(getJitteredDelay()) | ||
} | ||
} | ||
|
||
throw RuntimeException("Unexpected state after $attempts attempts") | ||
} | ||
|
||
companion object { | ||
private const val INITIAL_DELAY = 100L | ||
private const val MAX_BACKOFF = 10000L | ||
private const val MAX_RETRY_ATTEMPTS = 3 | ||
} | ||
} |
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