-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add codegen wrappers for retries (#490)
- Loading branch information
Showing
27 changed files
with
576 additions
and
132 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
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
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
64 changes: 64 additions & 0 deletions
64
runtime/protocol/http/common/src/aws/smithy/kotlin/runtime/http/middleware/RetryFeature.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,64 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0. | ||
*/ | ||
|
||
package aws.smithy.kotlin.runtime.http.middleware | ||
|
||
import aws.smithy.kotlin.runtime.http.Feature | ||
import aws.smithy.kotlin.runtime.http.FeatureKey | ||
import aws.smithy.kotlin.runtime.http.HttpBody | ||
import aws.smithy.kotlin.runtime.http.HttpClientFeatureFactory | ||
import aws.smithy.kotlin.runtime.http.operation.SdkHttpOperation | ||
import aws.smithy.kotlin.runtime.http.operation.deepCopy | ||
import aws.smithy.kotlin.runtime.http.request.HttpRequestBuilder | ||
import aws.smithy.kotlin.runtime.retries.RetryPolicy | ||
import aws.smithy.kotlin.runtime.retries.RetryStrategy | ||
|
||
class RetryFeature(private val strategy: RetryStrategy, private val policy: RetryPolicy<Any?>) : Feature { | ||
class Config { | ||
var strategy: RetryStrategy? = null | ||
var policy: RetryPolicy<Any?>? = null | ||
} | ||
|
||
companion object Feature : HttpClientFeatureFactory<Config, RetryFeature> { | ||
override val key: FeatureKey<RetryFeature> = FeatureKey("RetryFeature") | ||
|
||
override fun create(block: Config.() -> Unit): RetryFeature { | ||
val config = Config().apply(block) | ||
val strategy = requireNotNull(config.strategy) { "strategy is required" } | ||
val policy = requireNotNull(config.policy) { "policy is required" } | ||
return RetryFeature(strategy, policy) | ||
} | ||
} | ||
|
||
override fun <I, O> install(operation: SdkHttpOperation<I, O>) { | ||
operation.execution.finalize.intercept { req, next -> | ||
if (req.subject.isRetryable) { | ||
strategy.retry(policy) { | ||
// Deep copy the request because later middlewares (e.g., signing) mutate it | ||
val reqCopy = req.deepCopy() | ||
|
||
when (val body = reqCopy.subject.body) { | ||
// Reset streaming bodies back to beginning | ||
is HttpBody.Streaming -> body.reset() | ||
} | ||
|
||
next.call(reqCopy) | ||
} | ||
} else { | ||
next.call(req) | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Indicates whether this HTTP request could be retried. Some requests with streaming bodies are unsuitable for | ||
* retries. | ||
*/ | ||
val HttpRequestBuilder.isRetryable: Boolean | ||
get() = when (val body = this.body) { | ||
is HttpBody.Empty, is HttpBody.Bytes -> true | ||
is HttpBody.Streaming -> body.isReplayable | ||
} |
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
37 changes: 0 additions & 37 deletions
37
...e/protocol/http/common/src/aws/smithy/kotlin/runtime/http/retries/StandardRetryFeature.kt
This file was deleted.
Oops, something went wrong.
17 changes: 17 additions & 0 deletions
17
runtime/protocol/http/common/src/aws/smithy/kotlin/runtime/http/util/CanDeepCopy.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,17 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0. | ||
*/ | ||
|
||
package aws.smithy.kotlin.runtime.http.util | ||
|
||
/** | ||
* Indicates that an object supports a [deepCopy] operation which will return a copy that can be safely mutated without | ||
* affecting other instances. | ||
*/ | ||
interface CanDeepCopy<out T> { | ||
/** | ||
* Returns a deep copy of this object. | ||
*/ | ||
fun deepCopy(): T | ||
} |
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
Oops, something went wrong.