Skip to content

Commit

Permalink
chore(middleware-retry): use normalizeProvider from util-middleware (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr committed Mar 22, 2022
1 parent 9873569 commit fb160d0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
1 change: 1 addition & 0 deletions packages/middleware-retry/package.json
Expand Up @@ -21,6 +21,7 @@
"dependencies": {
"@aws-sdk/protocol-http": "*",
"@aws-sdk/service-error-classification": "*",
"@aws-sdk/util-middleware": "*",
"@aws-sdk/types": "*",
"tslib": "^2.3.1",
"uuid": "^8.3.2"
Expand Down
23 changes: 18 additions & 5 deletions packages/middleware-retry/src/configurations.spec.ts
@@ -1,3 +1,5 @@
import { normalizeProvider } from "@aws-sdk/util-middleware";

import { AdaptiveRetryStrategy } from "./AdaptiveRetryStrategy";
import { DEFAULT_MAX_ATTEMPTS } from "./config";
import {
Expand All @@ -10,19 +12,30 @@ import { StandardRetryStrategy } from "./StandardRetryStrategy";

jest.mock("./AdaptiveRetryStrategy");
jest.mock("./StandardRetryStrategy");
jest.mock("@aws-sdk/util-middleware");

describe(resolveRetryConfig.name, () => {
const retryMode = jest.fn() as any;

beforeEach(() => {
(normalizeProvider as jest.Mock).mockImplementation((input) =>
typeof input === "function" ? input : () => Promise.resolve(input)
);
});

afterEach(() => {
jest.clearAllMocks();
});

describe("maxAttempts", () => {
it("assigns maxAttempts value if present", async () => {
for (const maxAttempts of [1, 2, 3]) {
const output = await resolveRetryConfig({ maxAttempts, retryMode }).maxAttempts();
expect(output).toStrictEqual(maxAttempts);
}
it.each([1, 2, 3])("assigns provided value %s", async (maxAttempts) => {
const output = await resolveRetryConfig({ maxAttempts, retryMode }).maxAttempts();
expect(output).toStrictEqual(maxAttempts);
});

it(`assigns default ${DEFAULT_MAX_ATTEMPTS} is value not provided`, async () => {
const output = await resolveRetryConfig({ retryMode }).maxAttempts();
expect(output).toStrictEqual(DEFAULT_MAX_ATTEMPTS);
});
});

Expand Down
20 changes: 3 additions & 17 deletions packages/middleware-retry/src/configurations.ts
@@ -1,5 +1,6 @@
import { LoadedConfigSelectors } from "@aws-sdk/node-config-provider";
import { Provider, RetryStrategy } from "@aws-sdk/types";
import { normalizeProvider } from "@aws-sdk/util-middleware";

import { AdaptiveRetryStrategy } from "./AdaptiveRetryStrategy";
import { DEFAULT_MAX_ATTEMPTS, DEFAULT_RETRY_MODE, RETRY_MODES } from "./config";
Expand Down Expand Up @@ -61,15 +62,15 @@ export interface RetryResolvedConfig {
}

export const resolveRetryConfig = <T>(input: T & PreviouslyResolved & RetryInputConfig): T & RetryResolvedConfig => {
const maxAttempts = normalizeMaxAttempts(input.maxAttempts);
const maxAttempts = normalizeProvider(input.maxAttempts ?? DEFAULT_MAX_ATTEMPTS);
return {
...input,
maxAttempts,
retryStrategy: async () => {
if (input.retryStrategy) {
return input.retryStrategy;
}
const retryMode = await getRetryMode(input.retryMode);
const retryMode = await normalizeProvider(input.retryMode)();
if (retryMode === RETRY_MODES.ADAPTIVE) {
return new AdaptiveRetryStrategy(maxAttempts);
}
Expand All @@ -78,21 +79,6 @@ export const resolveRetryConfig = <T>(input: T & PreviouslyResolved & RetryInput
};
};

const getRetryMode = async (retryMode: string | Provider<string>): Promise<string> => {
if (typeof retryMode === "string") {
return retryMode;
}
return await retryMode();
};

const normalizeMaxAttempts = (maxAttempts: number | Provider<number> = DEFAULT_MAX_ATTEMPTS): Provider<number> => {
if (typeof maxAttempts === "number") {
const promisified = Promise.resolve(maxAttempts);
return () => promisified;
}
return maxAttempts;
};

export const ENV_RETRY_MODE = "AWS_RETRY_MODE";
export const CONFIG_RETRY_MODE = "retry_mode";

Expand Down

0 comments on commit fb160d0

Please sign in to comment.