Skip to content
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