All files / src/resilience retry.ts

0% Statements 0/224
0% Branches 0/1
0% Functions 0/1
0% Lines 0/224

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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
/**
 * Retry with Exponential Backoff
 *
 * Production-ready retry logic with jitter, max retries, and error filtering.
 *
 * @module v3/shared/resilience/retry
 */

/**
 * Retry options
 */
export interface RetryOptions {
  /** Maximum number of retry attempts (default: 3) */
  maxAttempts: number;

  /** Initial delay in milliseconds (default: 100) */
  initialDelay: number;

  /** Maximum delay in milliseconds (default: 10000) */
  maxDelay: number;

  /** Backoff multiplier (default: 2) */
  backoffMultiplier: number;

  /** Jitter factor 0-1 to randomize delays (default: 0.1) */
  jitter: number;

  /** Timeout for each attempt in milliseconds (default: 30000) */
  timeout: number;

  /** Errors that should trigger a retry (default: all errors) */
  retryableErrors?: (error: Error) => boolean;

  /** Callback for each retry attempt */
  onRetry?: (error: Error, attempt: number, delay: number) => void;
}

/**
 * Retry result
 */
export interface RetryResult<T> {
  success: boolean;
  result?: T;
  attempts: number;
  totalTime: number;
  errors: Error[];
}

/**
 * Retry error with attempt history
 */
export class RetryError extends Error {
  constructor(
    message: string,
    public readonly attempts: number,
    public readonly errors: Error[],
    public readonly totalTime: number
  ) {
    super(message);
    this.name = 'RetryError';
  }
}

/**
 * Default retry options
 */
const DEFAULT_OPTIONS: RetryOptions = {
  maxAttempts: 3,
  initialDelay: 100,
  maxDelay: 10000,
  backoffMultiplier: 2,
  jitter: 0.1,
  timeout: 30000,
};

/**
 * Retry a function with exponential backoff
 *
 * @param fn Function to retry
 * @param options Retry configuration
 * @returns Result with success/failure and metadata
 *
 * @example
 * const result = await retry(
 *   () => fetchData(),
 *   { maxAttempts: 5, initialDelay: 200 }
 * );
 *
 * if (result.success) {
 *   console.log('Data:', result.result);
 * } else {
 *   console.log('Failed after', result.attempts, 'attempts');
 * }
 */
export async function retry<T>(
  fn: () => Promise<T>,
  options: Partial<RetryOptions> = {}
): Promise<RetryResult<T>> {
  const opts = { ...DEFAULT_OPTIONS, ...options };
  const errors: Error[] = [];
  const startTime = Date.now();

  for (let attempt = 1; attempt <= opts.maxAttempts; attempt++) {
    try {
      // Execute with timeout
      const result = await withTimeout(fn(), opts.timeout, attempt);

      return {
        success: true,
        result,
        attempts: attempt,
        totalTime: Date.now() - startTime,
        errors,
      };
    } catch (error) {
      const err = error instanceof Error ? error : new Error(String(error));
      errors.push(err);

      // Check if error is retryable
      if (opts.retryableErrors && !opts.retryableErrors(err)) {
        return {
          success: false,
          attempts: attempt,
          totalTime: Date.now() - startTime,
          errors,
        };
      }

      // If this was the last attempt, don't delay
      if (attempt >= opts.maxAttempts) {
        break;
      }

      // Calculate delay with exponential backoff and jitter
      const baseDelay = opts.initialDelay * Math.pow(opts.backoffMultiplier, attempt - 1);
      const jitter = baseDelay * opts.jitter * (Math.random() * 2 - 1);
      const delay = Math.min(baseDelay + jitter, opts.maxDelay);

      // Callback before retry
      if (opts.onRetry) {
        opts.onRetry(err, attempt, delay);
      }

      // Wait before next attempt
      await sleep(delay);
    }
  }

  return {
    success: false,
    attempts: opts.maxAttempts,
    totalTime: Date.now() - startTime,
    errors,
  };
}

/**
 * Wrap a function with retry behavior
 *
 * @param fn Function to wrap
 * @param options Retry configuration
 * @returns Wrapped function that retries on failure
 */
export function withRetry<T extends (...args: unknown[]) => Promise<unknown>>(
  fn: T,
  options: Partial<RetryOptions> = {}
): (...args: Parameters<T>) => Promise<RetryResult<Awaited<ReturnType<T>>>> {
  return async (...args: Parameters<T>) => {
    return retry(() => fn(...args) as Promise<Awaited<ReturnType<T>>>, options);
  };
}

/**
 * Execute with timeout
 */
async function withTimeout<T>(promise: Promise<T>, timeout: number, attempt: number): Promise<T> {
  const timeoutPromise = new Promise<never>((_, reject) => {
    setTimeout(() => {
      reject(new Error(`Attempt ${attempt} timed out after ${timeout}ms`));
    }, timeout);
  });

  return Promise.race([promise, timeoutPromise]);
}

/**
 * Sleep for specified milliseconds
 */
function sleep(ms: number): Promise<void> {
  return new Promise((resolve) => setTimeout(resolve, ms));
}

/**
 * Common retryable error predicates
 */
export const RetryableErrors = {
  /** Network errors (ECONNRESET, ETIMEDOUT, etc.) */
  network: (error: Error): boolean => {
    const networkCodes = ['ECONNRESET', 'ETIMEDOUT', 'ECONNREFUSED', 'ENOTFOUND', 'EAI_AGAIN'];
    return networkCodes.some((code) => error.message.includes(code));
  },

  /** Rate limit errors (429) */
  rateLimit: (error: Error): boolean => {
    return error.message.includes('429') || error.message.toLowerCase().includes('rate limit');
  },

  /** Server errors (5xx) */
  serverError: (error: Error): boolean => {
    return /5\d\d/.test(error.message) || error.message.includes('Internal Server Error');
  },

  /** Transient errors (network + rate limit + 5xx) */
  transient: (error: Error): boolean => {
    return (
      RetryableErrors.network(error) ||
      RetryableErrors.rateLimit(error) ||
      RetryableErrors.serverError(error)
    );
  },

  /** All errors are retryable */
  all: (): boolean => true,
};