Skip to content

Commit c710c72

Browse files
authored
refactor: make AWS retry policy inheritable (#1122)
1 parent 094809f commit c710c72

File tree

7 files changed

+86
-65
lines changed

7 files changed

+86
-65
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"id": "4cbc20ed-789f-4040-b81c-328ecd3d0700",
3+
"type": "feature",
4+
"description": "Make the AWS retry policy inheritable",
5+
"issues": [
6+
"awslabs/aws-sdk-kotlin#1055"
7+
]
8+
}

aws-runtime/aws-http/api/aws-http.api

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,13 @@ public final class aws/sdk/kotlin/runtime/http/operation/CustomUserAgentMetadata
4949
public static final fun getCustomUserAgentMetadata (Laws/smithy/kotlin/runtime/operation/ExecutionContext;)Laws/sdk/kotlin/runtime/http/operation/CustomUserAgentMetadata;
5050
}
5151

52-
public final class aws/sdk/kotlin/runtime/http/retries/AwsDefaultRetryPolicy : aws/smithy/kotlin/runtime/retries/policy/StandardRetryPolicy {
53-
public static final field INSTANCE Laws/sdk/kotlin/runtime/http/retries/AwsDefaultRetryPolicy;
52+
public class aws/sdk/kotlin/runtime/http/retries/AwsRetryPolicy : aws/smithy/kotlin/runtime/retries/policy/StandardRetryPolicy {
53+
public static final field Companion Laws/sdk/kotlin/runtime/http/retries/AwsRetryPolicy$Companion;
54+
public fun <init> ()V
55+
protected fun evaluateSpecificExceptions (Ljava/lang/Throwable;)Laws/smithy/kotlin/runtime/retries/policy/RetryDirective;
56+
}
57+
58+
public final class aws/sdk/kotlin/runtime/http/retries/AwsRetryPolicy$Companion {
59+
public final fun getDefault ()Laws/sdk/kotlin/runtime/http/retries/AwsRetryPolicy;
5460
}
5561

aws-runtime/aws-http/common/src/aws/sdk/kotlin/runtime/http/retries/AwsDefaultRetryPolicy.kt

Lines changed: 0 additions & 55 deletions
This file was deleted.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package aws.sdk.kotlin.runtime.http.retries
7+
8+
import aws.smithy.kotlin.runtime.ServiceErrorMetadata
9+
import aws.smithy.kotlin.runtime.ServiceException
10+
import aws.smithy.kotlin.runtime.http.response.HttpResponse
11+
import aws.smithy.kotlin.runtime.retries.policy.RetryDirective
12+
import aws.smithy.kotlin.runtime.retries.policy.RetryErrorType.*
13+
import aws.smithy.kotlin.runtime.retries.policy.StandardRetryPolicy
14+
15+
public open class AwsRetryPolicy : StandardRetryPolicy() {
16+
public companion object {
17+
/**
18+
* The default [aws.smithy.kotlin.runtime.retries.policy.RetryPolicy] used by AWS service clients
19+
*/
20+
public val Default: AwsRetryPolicy = AwsRetryPolicy()
21+
22+
internal val knownErrorTypes = mapOf(
23+
"BandwidthLimitExceeded" to Throttling,
24+
"EC2ThrottledException" to Throttling,
25+
"IDPCommunicationError" to Transient,
26+
"LimitExceededException" to Throttling,
27+
"PriorRequestNotComplete" to Throttling,
28+
"ProvisionedThroughputExceededException" to Throttling,
29+
"RequestLimitExceeded" to Throttling,
30+
"RequestThrottled" to Throttling,
31+
"RequestThrottledException" to Throttling,
32+
"RequestTimeout" to Transient,
33+
"RequestTimeoutException" to Transient,
34+
"SlowDown" to Throttling,
35+
"ThrottledException" to Throttling,
36+
"Throttling" to Throttling,
37+
"ThrottlingException" to Throttling,
38+
"TooManyRequestsException" to Throttling,
39+
"TransactionInProgressException" to Throttling,
40+
)
41+
42+
internal val knownStatusCodes = mapOf(
43+
500 to Transient,
44+
502 to Transient,
45+
503 to Transient,
46+
504 to Transient,
47+
)
48+
}
49+
50+
override fun evaluateSpecificExceptions(ex: Throwable): RetryDirective? = when (ex) {
51+
is ServiceException -> evaluateServiceException(ex)
52+
else -> null
53+
}
54+
55+
private fun evaluateServiceException(ex: ServiceException): RetryDirective? = with(ex.sdkErrorMetadata) {
56+
(knownErrorTypes[errorCode] ?: knownStatusCodes[statusCode])
57+
?.let { RetryDirective.RetryError(it) }
58+
}
59+
60+
private val ServiceErrorMetadata.statusCode: Int?
61+
get() = (protocolResponse as? HttpResponse)?.status?.value
62+
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,25 @@ import aws.smithy.kotlin.runtime.retries.policy.RetryDirective
1414
import kotlin.test.Test
1515
import kotlin.test.assertEquals
1616

17-
class AwsDefaultRetryPolicyTest {
17+
class AwsRetryPolicyTest {
1818
@Test
1919
fun testErrorsByErrorCode() {
20-
AwsDefaultRetryPolicy.knownErrorTypes.forEach { (errorCode, errorType) ->
20+
AwsRetryPolicy.knownErrorTypes.forEach { (errorCode, errorType) ->
2121
val ex = ServiceException()
2222
ex.sdkErrorMetadata.attributes[ServiceErrorMetadata.ErrorCode] = errorCode
23-
val result = AwsDefaultRetryPolicy.evaluate(Result.failure(ex))
23+
val result = AwsRetryPolicy.Default.evaluate(Result.failure(ex))
2424
assertEquals(RetryDirective.RetryError(errorType), result)
2525
}
2626
}
2727

2828
@Test
2929
fun testErrorsByStatusCode() {
30-
AwsDefaultRetryPolicy.knownStatusCodes.forEach { (statusCode, errorType) ->
30+
AwsRetryPolicy.knownStatusCodes.forEach { (statusCode, errorType) ->
3131
val modeledStatusCode = HttpStatusCode.fromValue(statusCode)
3232
val response = HttpResponse(modeledStatusCode, Headers.Empty, HttpBody.Empty)
3333
val ex = ServiceException()
3434
ex.sdkErrorMetadata.attributes[ServiceErrorMetadata.ProtocolResponse] = response
35-
val result = AwsDefaultRetryPolicy.evaluate(Result.failure(ex))
35+
val result = AwsRetryPolicy.Default.evaluate(Result.failure(ex))
3636
assertEquals(RetryDirective.RetryError(errorType), result)
3737
}
3838
}

codegen/smithy-aws-kotlin-codegen/src/main/kotlin/aws/sdk/kotlin/codegen/AwsRuntimeTypes.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ object AwsRuntimeTypes {
6363
}
6464

6565
object Retries {
66-
val AwsDefaultRetryPolicy = symbol("AwsDefaultRetryPolicy", "retries")
66+
val AwsRetryPolicy = symbol("AwsRetryPolicy", "retries")
6767
}
6868

6969
object Middleware {

codegen/smithy-aws-kotlin-codegen/src/main/kotlin/aws/sdk/kotlin/codegen/AwsServiceConfigIntegration.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ class AwsServiceConfigIntegration : KotlinIntegration {
121121
.RetryPolicy
122122
.toBuilder()
123123
.apply {
124-
propertyType = ConfigPropertyType.RequiredWithDefault("AwsDefaultRetryPolicy")
125-
additionalImports = listOf(AwsRuntimeTypes.Http.Retries.AwsDefaultRetryPolicy)
124+
propertyType = ConfigPropertyType.RequiredWithDefault("AwsRetryPolicy.Default")
125+
additionalImports = listOf(AwsRuntimeTypes.Http.Retries.AwsRetryPolicy)
126126
}
127127
.build()
128128

0 commit comments

Comments
 (0)