Skip to content

chore(middleware-retry): add options to StandardRetryStrategy #2458

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 3, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions packages/middleware-retry/src/defaultRetryQuota.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,34 @@ import { SdkError } from "@aws-sdk/smithy-client";
import { NO_RETRY_INCREMENT, RETRY_COST, TIMEOUT_RETRY_COST } from "./constants";
import { RetryQuota } from "./types";

export const getDefaultRetryQuota = (initialRetryTokens: number): RetryQuota => {
export interface DefaultRetryQuotaOptions {
/**
* The total amount of retry token to be incremented from retry token balance
* if an SDK operation invocation succeeds without requiring a retry request.
*/
noRetryIncrement?: number;

/**
* The total amount of retry tokens to be decremented from retry token balance.
*/
retryCost?: number;

/**
* The total amount of retry tokens to be decremented from retry token balance
* when a throttling error is encountered.
*/
timeoutRetryCost?: number;
}

export const getDefaultRetryQuota = (initialRetryTokens: number, options?: DefaultRetryQuotaOptions): RetryQuota => {
const MAX_CAPACITY = initialRetryTokens;
const noRetryIncrement = options?.noRetryIncrement ?? NO_RETRY_INCREMENT;
const retryCost = options?.retryCost ?? RETRY_COST;
const timeoutRetryCost = options?.timeoutRetryCost ?? TIMEOUT_RETRY_COST;

let availableCapacity = initialRetryTokens;

const getCapacityAmount = (error: SdkError) => (error.name === "TimeoutError" ? TIMEOUT_RETRY_COST : RETRY_COST);
const getCapacityAmount = (error: SdkError) => (error.name === "TimeoutError" ? timeoutRetryCost : retryCost);

const hasRetryTokens = (error: SdkError) => getCapacityAmount(error) <= availableCapacity;

Expand All @@ -22,7 +45,7 @@ export const getDefaultRetryQuota = (initialRetryTokens: number): RetryQuota =>
};

const releaseRetryTokens = (capacityReleaseAmount?: number) => {
availableCapacity += capacityReleaseAmount ?? NO_RETRY_INCREMENT;
availableCapacity += capacityReleaseAmount ?? noRetryIncrement;
availableCapacity = Math.min(availableCapacity, MAX_CAPACITY);
};

Expand Down