Skip to content

Commit

Permalink
fix(util-retry): packages/util-retry/src/StandardRetryStrategy.ts
Browse files Browse the repository at this point in the history
Co-authored-by: Trivikram Kamat <16024985+trivikr@users.noreply.github.com>
  • Loading branch information
kuhe and trivikr committed May 25, 2023
1 parent 4fcaff1 commit cdbe6a0
Showing 1 changed file with 14 additions and 17 deletions.
31 changes: 14 additions & 17 deletions packages/util-retry/src/StandardRetryStrategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,18 @@ export class StandardRetryStrategy implements RetryStrategyV2 {
): Promise<StandardRetryToken> {
const maxAttempts = await this.getMaxAttempts();

const getCapacityCost = (errorType: RetryErrorType) =>
errorType === "TRANSIENT" ? TIMEOUT_RETRY_COST : RETRY_COST;

if (this.shouldRetry(token, errorInfo, maxAttempts)) {
const errorType = errorInfo.errorType;
const capacityCost = getCapacityCost(errorType);
const delayBase = errorType === "THROTTLING" ? THROTTLING_RETRY_DELAY_BASE : DEFAULT_RETRY_DELAY_BASE;
this.retryBackoffStrategy.setDelayBase(delayBase);
this.retryBackoffStrategy.setDelayBase(
errorType === "THROTTLING" ? THROTTLING_RETRY_DELAY_BASE : DEFAULT_RETRY_DELAY_BASE
);

const delayFromErrorType = this.retryBackoffStrategy.computeNextBackoffDelay(token.getRetryCount());
let retryDelay: number;
if (errorInfo.retryAfterHint) {
const delayFromRetryAfterHint = errorInfo.retryAfterHint.getTime() - Date.now();
retryDelay = Math.max(delayFromRetryAfterHint || 0, delayFromErrorType);
} else {
retryDelay = delayFromErrorType;
}
const retryDelay = errorInfo.retryAfterHint
? Math.max(errorInfo.retryAfterHint.getTime() - Date.now() || 0, delayFromErrorType)
: delayFromErrorType;

const capacityCost = this.getCapacityCost(errorType);
this.capacity -= capacityCost;
return createDefaultRetryToken({
availableCapacity: this.capacity,
Expand Down Expand Up @@ -94,16 +90,17 @@ export class StandardRetryStrategy implements RetryStrategyV2 {
private shouldRetry(tokenToRenew: StandardRetryToken, errorInfo: RetryErrorInfo, maxAttempts: number): boolean {
const attempts = tokenToRenew.getRetryCount();

const getCapacityAmount = (errorType: RetryErrorType) =>
errorType === "TRANSIENT" ? TIMEOUT_RETRY_COST : RETRY_COST;

return (
attempts < maxAttempts &&
this.capacity >= getCapacityAmount(errorInfo.errorType) &&
this.capacity >= this.getCapacityCost(errorInfo.errorType) &&
this.isRetryableError(errorInfo.errorType)
);
}

private getCapacityCost(errorType: RetryErrorType) {
return errorType === "TRANSIENT" ? TIMEOUT_RETRY_COST : RETRY_COST;
}

private isRetryableError(errorType: RetryErrorType): boolean {
return errorType === "THROTTLING" || errorType === "TRANSIENT";
}
Expand Down

0 comments on commit cdbe6a0

Please sign in to comment.