This commit is contained in:
RochesterX
2025-11-12 10:13:24 -05:00
parent d5b0f97adb
commit 6e820464d5
9761 changed files with 706938 additions and 0 deletions

View File

@@ -0,0 +1,70 @@
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
import { IncomingHttpHeaders } from "http";
import { HttpStatus, Logger } from "@azure/msal-common";
import { IHttpRetryPolicy } from "./IHttpRetryPolicy.js";
import { LinearRetryStrategy } from "./LinearRetryStrategy.js";
export const DEFAULT_MANAGED_IDENTITY_MAX_RETRIES: number = 3; // referenced in unit test
const DEFAULT_MANAGED_IDENTITY_RETRY_DELAY_MS: number = 1000;
const DEFAULT_MANAGED_IDENTITY_HTTP_STATUS_CODES_TO_RETRY_ON: Array<number> = [
HttpStatus.NOT_FOUND,
HttpStatus.REQUEST_TIMEOUT,
HttpStatus.TOO_MANY_REQUESTS,
HttpStatus.SERVER_ERROR,
HttpStatus.SERVICE_UNAVAILABLE,
HttpStatus.GATEWAY_TIMEOUT,
];
export class DefaultManagedIdentityRetryPolicy implements IHttpRetryPolicy {
/*
* this is defined here as a static variable despite being defined as a constant outside of the
* class because it needs to be overridden in the unit tests so that the unit tests run faster
*/
static get DEFAULT_MANAGED_IDENTITY_RETRY_DELAY_MS(): number {
return DEFAULT_MANAGED_IDENTITY_RETRY_DELAY_MS;
}
private linearRetryStrategy: LinearRetryStrategy =
new LinearRetryStrategy();
async pauseForRetry(
httpStatusCode: number,
currentRetry: number,
logger: Logger,
retryAfterHeader: IncomingHttpHeaders["retry-after"]
): Promise<boolean> {
if (
DEFAULT_MANAGED_IDENTITY_HTTP_STATUS_CODES_TO_RETRY_ON.includes(
httpStatusCode
) &&
currentRetry < DEFAULT_MANAGED_IDENTITY_MAX_RETRIES
) {
const retryAfterDelay: number =
this.linearRetryStrategy.calculateDelay(
retryAfterHeader,
DefaultManagedIdentityRetryPolicy.DEFAULT_MANAGED_IDENTITY_RETRY_DELAY_MS
);
logger.verbose(
`Retrying request in ${retryAfterDelay}ms (retry attempt: ${
currentRetry + 1
})`
);
// pause execution for the calculated delay
await new Promise((resolve) => {
// retryAfterHeader value of 0 evaluates to false, and DEFAULT_MANAGED_IDENTITY_RETRY_DELAY_MS will be used
return setTimeout(resolve, retryAfterDelay);
});
return true;
}
// if the status code is not retriable or max retries have been reached, do not retry
return false;
}
}

View File

@@ -0,0 +1,53 @@
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
export class ExponentialRetryStrategy {
// Minimum backoff time in milliseconds
private minExponentialBackoff: number;
// Maximum backoff time in milliseconds
private maxExponentialBackoff: number;
// Maximum backoff time in milliseconds
private exponentialDeltaBackoff: number;
constructor(
minExponentialBackoff: number,
maxExponentialBackoff: number,
exponentialDeltaBackoff: number
) {
this.minExponentialBackoff = minExponentialBackoff;
this.maxExponentialBackoff = maxExponentialBackoff;
this.exponentialDeltaBackoff = exponentialDeltaBackoff;
}
/**
* Calculates the exponential delay based on the current retry attempt.
*
* @param {number} currentRetry - The current retry attempt number.
* @returns {number} - The calculated exponential delay in milliseconds.
*
* The delay is calculated using the formula:
* - If `currentRetry` is 0, it returns the minimum backoff time.
* - Otherwise, it calculates the delay as the minimum of:
* - `(2^(currentRetry - 1)) * deltaBackoff`
* - `maxBackoff`
*
* This ensures that the delay increases exponentially with each retry attempt,
* but does not exceed the maximum backoff time.
*/
public calculateDelay(currentRetry: number): number {
// Attempt 1
if (currentRetry === 0) {
return this.minExponentialBackoff;
}
// Attempt 2+
const exponentialDelay = Math.min(
Math.pow(2, currentRetry - 1) * this.exponentialDeltaBackoff,
this.maxExponentialBackoff
);
return exponentialDelay;
}
}

View File

@@ -0,0 +1,27 @@
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
import { IncomingHttpHeaders } from "http";
import { Logger } from "@azure/msal-common";
export interface IHttpRetryPolicy {
_isNewRequest?: boolean;
// set isNewRequest(value: boolean);
/**
* Pauses execution for a specified amount of time before retrying an HTTP request.
*
* @param httpStatusCode - The HTTP status code of the response.
* @param currentRetry - The current retry attempt number.
* @param retryAfterHeader - The value of the `retry-after` HTTP header, if present.
* @returns A promise that resolves to a boolean indicating whether to retry the request.
*/
pauseForRetry(
httpStatusCode: number,
currentRetry: number,
logger: Logger,
retryAfterHeader?: IncomingHttpHeaders["retry-after"]
): Promise<boolean>;
}

View File

@@ -0,0 +1,121 @@
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
import { HttpStatus, Logger } from "@azure/msal-common";
import { ExponentialRetryStrategy } from "./ExponentialRetryStrategy.js";
import { IHttpRetryPolicy } from "./IHttpRetryPolicy.js";
const HTTP_STATUS_400_CODES_FOR_EXPONENTIAL_STRATEGY: Array<number> = [
HttpStatus.NOT_FOUND,
HttpStatus.REQUEST_TIMEOUT,
HttpStatus.GONE,
HttpStatus.TOO_MANY_REQUESTS,
];
const EXPONENTIAL_STRATEGY_NUM_RETRIES = 3;
const LINEAR_STRATEGY_NUM_RETRIES = 7;
const MIN_EXPONENTIAL_BACKOFF_MS: number = 1000;
const MAX_EXPONENTIAL_BACKOFF_MS: number = 4000;
const EXPONENTIAL_DELTA_BACKOFF_MS: number = 2000;
const HTTP_STATUS_GONE_RETRY_AFTER_MS: number = 10 * 1000; // 10 seconds
export class ImdsRetryPolicy implements IHttpRetryPolicy {
/*
* these are defined here as static variables despite being defined as constants outside of the
* class because they need to be overridden in the unit tests so that the unit tests run faster
*/
static get MIN_EXPONENTIAL_BACKOFF_MS(): number {
return MIN_EXPONENTIAL_BACKOFF_MS;
}
static get MAX_EXPONENTIAL_BACKOFF_MS(): number {
return MAX_EXPONENTIAL_BACKOFF_MS;
}
static get EXPONENTIAL_DELTA_BACKOFF_MS(): number {
return EXPONENTIAL_DELTA_BACKOFF_MS;
}
static get HTTP_STATUS_GONE_RETRY_AFTER_MS(): number {
return HTTP_STATUS_GONE_RETRY_AFTER_MS;
}
public _isNewRequest: boolean;
set isNewRequest(value: boolean) {
this._isNewRequest = value;
}
private maxRetries: number;
private exponentialRetryStrategy: ExponentialRetryStrategy =
new ExponentialRetryStrategy(
ImdsRetryPolicy.MIN_EXPONENTIAL_BACKOFF_MS,
ImdsRetryPolicy.MAX_EXPONENTIAL_BACKOFF_MS,
ImdsRetryPolicy.EXPONENTIAL_DELTA_BACKOFF_MS
);
/**
* Pauses execution for a calculated delay before retrying a request.
*
* @param httpStatusCode - The HTTP status code of the response.
* @param currentRetry - The current retry attempt number.
* @param retryAfterHeader - The value of the "retry-after" header from the response.
* @returns A promise that resolves to a boolean indicating whether a retry should be attempted.
*/
async pauseForRetry(
httpStatusCode: number,
currentRetry: number,
logger: Logger
): Promise<boolean> {
if (this._isNewRequest) {
this._isNewRequest = false;
// calculate the maxRetries based on the status code, once per request
this.maxRetries =
httpStatusCode === HttpStatus.GONE
? LINEAR_STRATEGY_NUM_RETRIES
: EXPONENTIAL_STRATEGY_NUM_RETRIES;
}
/**
* (status code is one of the retriable 400 status code
* or
* status code is >= 500 and <= 599)
* and
* current count of retries is less than the max number of retries
*/
if (
(HTTP_STATUS_400_CODES_FOR_EXPONENTIAL_STRATEGY.includes(
httpStatusCode
) ||
(httpStatusCode >= HttpStatus.SERVER_ERROR_RANGE_START &&
httpStatusCode <= HttpStatus.SERVER_ERROR_RANGE_END &&
currentRetry < this.maxRetries)) &&
currentRetry < this.maxRetries
) {
const retryAfterDelay: number =
httpStatusCode === HttpStatus.GONE
? ImdsRetryPolicy.HTTP_STATUS_GONE_RETRY_AFTER_MS
: this.exponentialRetryStrategy.calculateDelay(
currentRetry
);
logger.verbose(
`Retrying request in ${retryAfterDelay}ms (retry attempt: ${
currentRetry + 1
})`
);
// pause execution for the calculated delay
await new Promise((resolve) => {
return setTimeout(resolve, retryAfterDelay);
});
return true;
}
// if the status code is not retriable or max retries have been reached, do not retry
return false;
}
}

View File

@@ -0,0 +1,40 @@
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
import { IncomingHttpHeaders } from "http";
export class LinearRetryStrategy {
/**
* Calculates the number of milliseconds to sleep based on the `retry-after` HTTP header.
*
* @param retryHeader - The value of the `retry-after` HTTP header. This can be either a number of seconds
* or an HTTP date string.
* @returns The number of milliseconds to sleep before retrying the request. If the `retry-after` header is not
* present or cannot be parsed, returns 0.
*/
public calculateDelay(
retryHeader: IncomingHttpHeaders["retry-after"],
minimumDelay: number
): number {
if (!retryHeader) {
return minimumDelay;
}
// retry-after header is in seconds
let millisToSleep = Math.round(parseFloat(retryHeader) * 1000);
/*
* retry-after header is in HTTP Date format
* <day-name>, <day> <month> <year> <hour>:<minute>:<second> GMT
*/
if (isNaN(millisToSleep)) {
// .valueOf() is needed to subtract dates in TypeScript
millisToSleep =
new Date(retryHeader).valueOf() - new Date().valueOf();
}
return Math.max(minimumDelay, millisToSleep);
}
}