commonMain.aws.sdk.kotlin.runtime.http.middleware.AwsRetryHeaderMiddleware.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of aws-http-jvm Show documentation
Show all versions of aws-http-jvm Show documentation
HTTP core for AWS service clients
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package aws.sdk.kotlin.runtime.http.middleware
import aws.sdk.kotlin.runtime.InternalSdkApi
import aws.smithy.kotlin.runtime.http.operation.*
import aws.smithy.kotlin.runtime.http.request.header
import aws.smithy.kotlin.runtime.io.Handler
/**
* The per/operation unique client side ID header name. This will match
* the [HttpOperationContext.SdkInvocationId]
*/
internal const val AMZ_SDK_INVOCATION_ID_HEADER = "amz-sdk-invocation-id"
/**
* Details about the current request such as the attempt number, maximum possible attempts, ttl, etc
*/
internal const val AMZ_SDK_REQUEST_HEADER = "amz-sdk-request"
/**
* This middleware adds AWS specific retry headers
*/
@InternalSdkApi
public class AwsRetryHeaderMiddleware : MutateMiddleware {
private var attempt = 0
private var maxAttempts: Int? = null
override fun install(op: SdkHttpOperation<*, O>) {
maxAttempts = op.execution.retryStrategy.config.maxAttempts
op.execution.onEachAttempt.register(this)
}
override suspend fun > handle(request: SdkHttpRequest, next: H): O {
attempt++
request.subject.header(AMZ_SDK_INVOCATION_ID_HEADER, request.context.sdkInvocationId)
onAttempt(request, attempt)
return next.call(request)
}
private fun onAttempt(request: SdkHttpRequest, attempt: Int) {
// setting ttl would never be accurate, just set what we know which is attempt and maybe max attempt
val formattedMaxAttempts = maxAttempts?.let { "; max=$it" } ?: ""
request.subject.header(AMZ_SDK_REQUEST_HEADER, "attempt=${attempt}$formattedMaxAttempts")
}
}