commonMain.aws.sdk.kotlin.runtime.config.imds.ImdsRetryPolicy.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of aws-config-jvm Show documentation
Show all versions of aws-config-jvm Show documentation
Support for AWS configuration
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package aws.sdk.kotlin.runtime.config.imds
import aws.smithy.kotlin.runtime.http.HttpStatusCode
import aws.smithy.kotlin.runtime.http.category
import aws.smithy.kotlin.runtime.retries.policy.RetryDirective
import aws.smithy.kotlin.runtime.retries.policy.RetryErrorType
import aws.smithy.kotlin.runtime.retries.policy.RetryPolicy
import aws.smithy.kotlin.runtime.telemetry.logging.logger
import kotlin.coroutines.CoroutineContext
internal class ImdsRetryPolicy(
private val callContext: CoroutineContext,
) : RetryPolicy {
override fun evaluate(result: Result): RetryDirective = when {
result.isSuccess -> RetryDirective.TerminateAndSucceed
else -> evaluate(result.exceptionOrNull()!!)
}
private fun evaluate(throwable: Throwable): RetryDirective = when (throwable) {
is EC2MetadataError -> {
val status = HttpStatusCode.fromValue(throwable.statusCode)
when {
status.category() == HttpStatusCode.Category.SERVER_ERROR -> RetryDirective.RetryError(RetryErrorType.ServerSide)
// 401 indicates the token has expired, this is retryable
status == HttpStatusCode.Unauthorized -> RetryDirective.RetryError(RetryErrorType.ServerSide)
else -> {
val logger = callContext.logger()
logger.debug { "Non retryable IMDS error: statusCode=${throwable.statusCode}; ${throwable.message}" }
RetryDirective.TerminateAndFail
}
}
}
else -> RetryDirective.TerminateAndFail
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy