Skip to content

Commit

Permalink
Add retry to runtime extension
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewFossAWS committed Sep 5, 2023
1 parent fd4795e commit 88bcec3
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 26 deletions.
6 changes: 6 additions & 0 deletions .changeset/chilled-lies-care.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@smithy/smithy-client": minor
"@smithy/types": minor
---

Add retry to runtime extension
40 changes: 21 additions & 19 deletions packages/smithy-client/src/extensions/checksum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,28 @@ export { AlgorithmId, ChecksumAlgorithm, ChecksumConfiguration };
/**
* @internal
*/
export const getChecksumConfiguration = (
runtimeConfig: Partial<{
sha256: ChecksumConstructor | HashConstructor;
md5: ChecksumConstructor | HashConstructor;
}>
) => {
const checksumAlgorithms: ChecksumAlgorithm[] = [];
export type PartialChecksumRuntimeConfigType = Partial<{
sha256: ChecksumConstructor | HashConstructor;
md5: ChecksumConstructor | HashConstructor;
crc32: ChecksumConstructor | HashConstructor;
crc32c: ChecksumConstructor | HashConstructor;
sha1: ChecksumConstructor | HashConstructor;
}>;

if (runtimeConfig.sha256 !== undefined) {
checksumAlgorithms.push({
algorithmId: () => AlgorithmId.SHA256,
checksumConstructor: () => runtimeConfig.sha256!,
});
}
/**
* @internal
*/
export const getChecksumConfiguration = (runtimeConfig: PartialChecksumRuntimeConfigType) => {
const checksumAlgorithms: ChecksumAlgorithm[] = [];

if (runtimeConfig.md5 != undefined) {
for (const id in AlgorithmId) {
const algorithmId = AlgorithmId[id as keyof typeof AlgorithmId];
if (runtimeConfig[algorithmId] === undefined) {
continue;
}
checksumAlgorithms.push({
algorithmId: () => AlgorithmId.MD5,
checksumConstructor: () => runtimeConfig.md5!,
algorithmId: () => algorithmId,
checksumConstructor: () => runtimeConfig[algorithmId]!,
});
}

Expand All @@ -42,9 +45,8 @@ export const getChecksumConfiguration = (
/**
* @internal
*/
export const resolveChecksumRuntimeConfig = (clientConfig: ChecksumConfiguration) => {
const runtimeConfig: Partial<Record<AlgorithmId, HashConstructor | ChecksumConstructor>> = {};

export const resolveChecksumRuntimeConfig = (clientConfig: ChecksumConfiguration): PartialChecksumRuntimeConfigType => {
const runtimeConfig: PartialChecksumRuntimeConfigType = {};
clientConfig.checksumAlgorithms().forEach((checksumAlgorithm) => {
runtimeConfig[checksumAlgorithm.algorithmId()] = checksumAlgorithm.checksumConstructor();
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import type { DefaultExtensionConfiguration } from "@smithy/types";

import { getChecksumConfiguration, resolveChecksumRuntimeConfig } from "./checksum";
import { getChecksumConfiguration, PartialChecksumRuntimeConfigType, resolveChecksumRuntimeConfig } from "./checksum";
import { getRetryConfiguration, PartialRetryRuntimeConfigType, resolveRetryRuntimeConfig } from "./retry";

/**
* @internal
*/
export type DefaultExtensionConfigType = Parameters<typeof getChecksumConfiguration>[0];
export type DefaultExtensionRuntimeConfigType = PartialRetryRuntimeConfigType & PartialChecksumRuntimeConfigType;

/**
* @internal
*
* Helper function to resolve default extension configuration from runtime config
*/
export const getDefaultExtensionConfiguration = (runtimeConfig: DefaultExtensionConfigType) => {
export const getDefaultExtensionConfiguration = (runtimeConfig: DefaultExtensionRuntimeConfigType) => {
return {
...getChecksumConfiguration(runtimeConfig),
...getRetryConfiguration(runtimeConfig),
};
};

Expand All @@ -31,8 +33,11 @@ export const getDefaultClientConfiguration = getDefaultExtensionConfiguration;
*
* Helper function to resolve runtime config from default extension configuration
*/
export const resolveDefaultRuntimeConfig = (config: DefaultExtensionConfiguration) => {
export const resolveDefaultRuntimeConfig = (
config: DefaultExtensionConfiguration
): DefaultExtensionRuntimeConfigType => {
return {
...resolveChecksumRuntimeConfig(config),
...resolveRetryRuntimeConfig(config),
};
};
29 changes: 29 additions & 0 deletions packages/smithy-client/src/extensions/retry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Provider, RetryStrategy, RetryStrategyConfiguration, RetryStrategyV2 } from "@smithy/types";

export type PartialRetryRuntimeConfigType = Partial<{ retryStrategy: Provider<RetryStrategyV2 | RetryStrategy> }>;

/**
* @internal
*/
export const getRetryConfiguration = (runtimeConfig: PartialRetryRuntimeConfigType) => {
let _retryStrategy = runtimeConfig.retryStrategy!;
return {
setRetryStrategy(retryStrategy: Provider<RetryStrategyV2 | RetryStrategy>): void {
_retryStrategy = retryStrategy;
},
retryStrategy(): Provider<RetryStrategyV2 | RetryStrategy> {
return _retryStrategy;
},
};
};

/**
* @internal
*/
export const resolveRetryRuntimeConfig = (
retryStrategyConfiguration: RetryStrategyConfiguration
): PartialRetryRuntimeConfigType => {
const runtimeConfig: PartialRetryRuntimeConfigType = {};
runtimeConfig.retryStrategy = retryStrategyConfiguration.retryStrategy();
return runtimeConfig;
};
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { ChecksumConfiguration } from "./checksum";
import { RetryStrategyConfiguration } from "./retry";

/**
* @internal
*
* Default extension configuration consisting various configurations for modifying a service client
*/
export interface DefaultExtensionConfiguration extends ChecksumConfiguration {}

type GetDefaultConfigurationType = (runtimeConfig: any) => DefaultExtensionConfiguration;
export interface DefaultExtensionConfiguration extends ChecksumConfiguration, RetryStrategyConfiguration {}
1 change: 1 addition & 0 deletions packages/types/src/extensions/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./defaultClientConfiguration";
export * from "./defaultExtensionConfiguration";
export { AlgorithmId, ChecksumAlgorithm, ChecksumConfiguration } from "./checksum";
export { RetryStrategyConfiguration } from "./retry";
20 changes: 20 additions & 0 deletions packages/types/src/extensions/retry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { RetryStrategyV2 } from "../retry";
import { Provider, RetryStrategy } from "../util";

/**
* A configuration interface with methods called by runtime extension
* @internal
*/
export interface RetryStrategyConfiguration {
/**
* Set retry strategy used for all http requests
* @param retryStrategy
*/
setRetryStrategy(retryStrategy: Provider<RetryStrategyV2 | RetryStrategy>): void;

/**
* Get retry strategy used for all http requests
* @param retryStrategy
*/
retryStrategy(): Provider<RetryStrategyV2 | RetryStrategy>;
}

0 comments on commit 88bcec3

Please sign in to comment.