All files / src RetryStrategyConfiguration.ts

97.22% Statements 35/36
94.12% Branches 32/34
100% Functions 6/6
97.06% Lines 33/34

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94                                  3x           3x           3x               3x       15x 15x 15x 15x   15x   15x 364x   15x 201x   15x 166x 166x   3x   163x 163x 163x     15x 106x 106x     15x             3x   3x 2x   2x 2x 2x     2x     2x 2x    
/* 
  Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  
  Licensed under the Apache License, Version 2.0 (the "License").
  You may not use this file except in compliance with the License.
  You may obtain a copy of the License at
  
      http://www.apache.org/licenses/LICENSE-2.0
  
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/
import { SdkError } from "@aws-sdk/smithy-client";
import { RetryQuota } from "@aws-sdk/middleware-retry";
export const RETRY_COST = 5;
 
/**
 * The total amount of retry tokens to be decremented from retry token balance
 * when a throttling error is encountered.
 */
export const TIMEOUT_RETRY_COST = 10;
 
/**
 * The total amount of retry token to be incremented from retry token balance
 * if an SDK operation invocation succeeds without requiring a retry request.
 */
export const NO_RETRY_INCREMENT = 1;
 
export interface DefaultRetryQuotaOptions {
  noRetryIncrement?: number;
  retryCost?: number;
  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" ? timeoutRetryCost : retryCost;
 
  const hasRetryTokens = (error: SdkError) =>
    getCapacityAmount(error) <= availableCapacity;
 
  const retrieveRetryTokens = (error: SdkError) => {
    console.log("availableCapacity", availableCapacity);
    if (!hasRetryTokens(error)) {
      // retryStrategy should stop retrying, and return last error
      throw new Error("No retry token available");
    }
    const capacityAmount = getCapacityAmount(error);
    availableCapacity -= capacityAmount;
    return capacityAmount;
  };
 
  const releaseRetryTokens = (capacityReleaseAmount?: number) => {
    availableCapacity += capacityReleaseAmount ?? noRetryIncrement;
    availableCapacity = Math.min(availableCapacity, MAX_CAPACITY);
  };
 
  return Object.freeze({
    hasRetryTokens,
    retrieveRetryTokens,
    releaseRetryTokens,
  });
};
 
const DEFAULT_WAIT_TIME = 1000;
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export const exponentialBackOffDelayDecider = (_: number, attempts: number) => {
  console.log("wait attempts", attempts);
 
  let power = attempts;
  const jitter = Math.random() * 2 ** attempts * 100;
  Iif (attempts > 5) {
    power = 5;
  }
  const waitTime = Math.floor(
    Math.min(45 * 1000, Math.random() * 2 ** power * DEFAULT_WAIT_TIME) + jitter
  );
  console.log("wait time", waitTime);
  return waitTime;
};