-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Move retry middleware from SDK (#502)
- Loading branch information
Showing
12 changed files
with
194 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// | ||
// Copyright Amazon.com Inc. or its affiliates. | ||
// All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
public struct RetryerMiddleware<Output: HttpResponseBinding, | ||
OutputError: HttpResponseBinding>: Middleware { | ||
|
||
public var id: String = "Retryer" | ||
|
||
let retryer: SDKRetryer | ||
|
||
public init(retryer: SDKRetryer) { | ||
self.retryer = retryer | ||
} | ||
|
||
public func handle<H>( | ||
context: Context, | ||
input: SdkHttpRequestBuilder, | ||
next: H | ||
) async throws -> OperationOutput<Output> where | ||
H: Handler, | ||
Self.MInput == H.Input, | ||
Self.MOutput == H.Output, | ||
Self.Context == H.Context { | ||
|
||
// Select a partition ID to be used for throttling retry requests. Requests with the | ||
// same partition ID will be "pooled" together for throttling purposes. | ||
let partitionID: String | ||
if let customPartitionID = context.getPartitionID(), !customPartitionID.isEmpty { | ||
// use custom partition ID provided by context | ||
partitionID = customPartitionID | ||
} else if !input.host.isEmpty { | ||
// fall back to the hostname for partition ID, which is a "commonsense" default | ||
partitionID = input.host | ||
} else { | ||
throw SdkError<OutputError>.client(ClientError.unknownError("Partition ID could not be determined")) | ||
} | ||
|
||
do { | ||
let token = try await retryer.acquireToken(partitionId: partitionID) | ||
return try await tryRequest( | ||
token: token, | ||
partitionID: partitionID, | ||
context: context, | ||
input: input, | ||
next: next | ||
) | ||
} catch { | ||
throw SdkError<OutputError>.client(ClientError.retryError(error)) | ||
} | ||
} | ||
|
||
func tryRequest<H>( | ||
token: RetryToken, | ||
errorType: RetryError? = nil, | ||
partitionID: String, | ||
context: Context, | ||
input: SdkHttpRequestBuilder, | ||
next: H | ||
) async throws -> OperationOutput<Output> where | ||
H: Handler, | ||
Self.MInput == H.Input, | ||
Self.MOutput == H.Output, | ||
Self.Context == H.Context { | ||
|
||
do { | ||
let serviceResponse = try await next.handle(context: context, input: input) | ||
retryer.recordSuccess(token: token) | ||
return serviceResponse | ||
} catch let error as SdkError<OutputError> where retryer.isErrorRetryable(error: error) { | ||
let errorType = retryer.getErrorType(error: error) | ||
let newToken = try await retryer.scheduleRetry(token: token, error: errorType) | ||
// TODO: rewind the stream once streaming is properly implemented | ||
return try await tryRequest( | ||
token: newToken, | ||
partitionID: partitionID, | ||
context: context, | ||
input: input, | ||
next: next | ||
) | ||
} | ||
} | ||
|
||
public typealias MInput = SdkHttpRequestBuilder | ||
public typealias MOutput = OperationOutput<Output> | ||
public typealias Context = HttpContext | ||
} |
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
34 changes: 34 additions & 0 deletions
34
...in/kotlin/software/amazon/smithy/swift/codegen/integration/middlewares/RetryMiddleware.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,34 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0. | ||
*/ | ||
|
||
package software.amazon.smithy.swift.codegen.integration.middlewares | ||
|
||
import software.amazon.smithy.codegen.core.SymbolProvider | ||
import software.amazon.smithy.model.Model | ||
import software.amazon.smithy.model.shapes.OperationShape | ||
import software.amazon.smithy.swift.codegen.ClientRuntimeTypes | ||
import software.amazon.smithy.swift.codegen.SwiftWriter | ||
import software.amazon.smithy.swift.codegen.integration.middlewares.handlers.MiddlewareShapeUtils | ||
import software.amazon.smithy.swift.codegen.middleware.MiddlewarePosition | ||
import software.amazon.smithy.swift.codegen.middleware.MiddlewareRenderable | ||
import software.amazon.smithy.swift.codegen.middleware.MiddlewareStep | ||
|
||
class RetryMiddleware( | ||
val model: Model, | ||
val symbolProvider: SymbolProvider | ||
) : MiddlewareRenderable { | ||
|
||
override val name = "RetryMiddleware" | ||
|
||
override val middlewareStep = MiddlewareStep.FINALIZESTEP | ||
|
||
override val position = MiddlewarePosition.AFTER | ||
|
||
override fun render(writer: SwiftWriter, op: OperationShape, operationStackName: String) { | ||
val output = MiddlewareShapeUtils.outputSymbol(symbolProvider, model, op) | ||
val outputError = MiddlewareShapeUtils.outputErrorSymbol(op) | ||
writer.write("$operationStackName.${middlewareStep.stringValue()}.intercept(position: ${position.stringValue()}, middleware: \$N<\$N, \$N>(retryer: config.retryer))", ClientRuntimeTypes.Middleware.RetryerMiddleware, output, outputError) | ||
} | ||
} |
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
24 changes: 24 additions & 0 deletions
24
smithy-swift-codegen/src/test/kotlin/RetryMiddlewareTests.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,24 @@ | ||
import io.kotest.matchers.string.shouldContainOnlyOnce | ||
import org.junit.jupiter.api.Test | ||
|
||
class RetryMiddlewareTests { | ||
|
||
@Test | ||
fun `generates operation with retry middleware`() { | ||
val context = setupTests("Isolated/contentmd5checksum.smithy", "aws.protocoltests.restxml#RestXml") | ||
val contents = getFileContents(context.manifest, "/RestXml/RestXmlProtocolClient.swift") | ||
val expectedContents = """ | ||
operation.finalizeStep.intercept(position: .after, middleware: ClientRuntime.RetryerMiddleware<IdempotencyTokenWithStructureOutputResponse, IdempotencyTokenWithStructureOutputError>(retryer: config.retryer)) | ||
""".trimIndent() | ||
contents.shouldContainOnlyOnce(expectedContents) | ||
} | ||
private fun setupTests(smithyFile: String, serviceShapeId: String): TestContext { | ||
val context = TestContext.initContextFrom(smithyFile, serviceShapeId, MockHttpRestXMLProtocolGenerator()) { model -> | ||
model.defaultSettings(serviceShapeId, "RestXml", "2019-12-16", "Rest Xml Protocol") | ||
} | ||
context.generator.initializeMiddleware(context.generationCtx) | ||
context.generator.generateProtocolClient(context.generationCtx) | ||
context.generationCtx.delegator.flushWriters() | ||
return context | ||
} | ||
} |