Skip to content

Commit a9cd550

Browse files
committed
feat(middleware-retry): call retry strategy based on value in retryMode
1 parent 0196ab5 commit a9cd550

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

packages/middleware-retry/src/configurations.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { LoadedConfigSelectors } from "@aws-sdk/node-config-provider";
22
import { Provider, RetryStrategy } from "@aws-sdk/types";
33

4-
import { DEFAULT_MAX_ATTEMPTS, DEFAULT_RETRY_MODE } from "./config";
4+
import { AdaptiveRetryStrategy } from "./AdaptiveRetryStrategy";
5+
import { DEFAULT_MAX_ATTEMPTS, DEFAULT_RETRY_MODE, RETRY_MODES } from "./config";
56
import { StandardRetryStrategy } from "./StandardRetryStrategy";
67

78
export const ENV_MAX_ATTEMPTS = "AWS_MAX_ATTEMPTS";
@@ -38,6 +39,15 @@ export interface RetryInputConfig {
3839
* The strategy to retry the request. Using built-in exponential backoff strategy by default.
3940
*/
4041
retryStrategy?: RetryStrategy;
42+
/**
43+
* Specifies which retry algorithm to use.
44+
*/
45+
retryMode?: string;
46+
/**
47+
* Specifies provider for retry algorithm to use.
48+
* @internal
49+
*/
50+
retryModeProvider: Provider<string>;
4151
}
4252

4353
interface PreviouslyResolved {}
@@ -49,15 +59,24 @@ export interface RetryResolvedConfig {
4959
/**
5060
* Resolved value for input config {@link RetryInputConfig.retryStrategy}
5161
*/
52-
retryStrategy: RetryStrategy;
62+
retryStrategy: Provider<RetryStrategy>;
5363
}
5464

5565
export const resolveRetryConfig = <T>(input: T & PreviouslyResolved & RetryInputConfig): T & RetryResolvedConfig => {
5666
const maxAttempts = normalizeMaxAttempts(input.maxAttempts);
5767
return {
5868
...input,
5969
maxAttempts,
60-
retryStrategy: input.retryStrategy || new StandardRetryStrategy(maxAttempts),
70+
retryStrategy: async () => {
71+
if (input.retryStrategy) {
72+
return input.retryStrategy;
73+
}
74+
const retryMode = input.retryMode || (await input.retryModeProvider());
75+
if (retryMode === RETRY_MODES.adaptive) {
76+
return new AdaptiveRetryStrategy(maxAttempts);
77+
}
78+
return new StandardRetryStrategy(maxAttempts);
79+
},
6180
};
6281
};
6382

packages/middleware-retry/src/retryMiddleware.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ export const retryMiddleware = (options: RetryResolvedConfig) => <Output extends
1717
): FinalizeHandler<any, Output> => async (
1818
args: FinalizeHandlerArguments<any>
1919
): Promise<FinalizeHandlerOutput<Output>> => {
20-
if (options?.retryStrategy?.mode)
21-
context.userAgent = [...(context.userAgent || []), ["cfg/retry-mode", options.retryStrategy.mode]];
22-
return options.retryStrategy.retry(next, args);
20+
const retryStrategy = await options.retryStrategy();
21+
if (retryStrategy?.mode) context.userAgent = [...(context.userAgent || []), ["cfg/retry-mode", retryStrategy.mode]];
22+
return retryStrategy.retry(next, args);
2323
};
2424

2525
export const retryMiddlewareOptions: FinalizeRequestHandlerOptions & AbsoluteLocation = {

0 commit comments

Comments
 (0)