@bernierllc/retry-policy

中文文档
0.7.0发布于 2026/05/20MITTypeScriptCommonJS近 30 天下载 2481

Atomic retry policy utilities with exponential backoff and jitter

原始使用文档
文档目录27 节
  1. Overview
  2. Features
  3. Installation
  4. Quick Start
  5. API Reference
  6. RetryPolicy Class
  7. Utility Functions
  8. Usage Examples
  9. Basic Usage
  10. Custom Backoff Strategy
  11. Different Backoff Types
  12. Jitter Types
  13. Custom Retry Conditions
  14. Configuration
  15. RetryPolicyOptions
  16. BackoffConfig
  17. JitterConfig
  18. Default Values
  19. Integration Status
  20. Logger Integration
  21. NeverHub Integration
  22. Docs-Suite Integration
  23. Security
  24. Testing
  25. Contributing
  26. License
  27. Related Packages

@bernierllc/retry-policy

Atomic retry policy utilities with exponential backoff and jitter support.

Overview

@bernierllc/retry-policy provides atomic utilities for implementing retry logic with configurable backoff strategies and jitter. This package is designed to be a core building block that can be used by higher-level retry managers and services.

Features

  • Multiple backoff strategies: Exponential, linear, and constant backoff
  • Configurable jitter: Full, equal, decorrelated, and no jitter options
  • Custom retry conditions: Flexible error evaluation logic
  • Type-safe API: Full TypeScript support with strict typing
  • Zero dependencies: Pure functions with no external dependencies
  • Comprehensive testing: 95%+ test coverage

Installation

npm install @bernierllc/retry-policy

Quick Start

import { RetryPolicy, createRetryPolicy } from '@bernierllc/retry-policy';

// Create a retry policy with default settings
const policy = new RetryPolicy();

// Evaluate if an operation should be retried
const result = policy.evaluateRetry(1, new Error('Network error'));
console.log(result);
// {
//   shouldRetry: true,
//   delay: 2000,
//   attempt: 1,
//   isFinalAttempt: false
// }

API Reference

RetryPolicy Class

The main class for managing retry policies.

Constructor

new RetryPolicy(options?: Partial<RetryPolicyOptions>, backoffConfig?: Partial<BackoffConfig>)

Methods

  • evaluateRetry(attempt: number, error: any): RetryPolicyResult
  • calculateDelay(attempt: number): number
  • getOptions(): Required<RetryPolicyOptions>
  • getBackoffConfig(): BackoffConfig
  • updateOptions(options: Partial<RetryPolicyOptions>): void
  • updateBackoffConfig(config: Partial<BackoffConfig>): void

Utility Functions

  • createRetryPolicy(options?, backoffConfig?): RetryPolicy
  • calculateRetryDelay(attempt, options?, backoffConfig?): number
  • shouldRetry(attempt, error, options?): boolean

Usage Examples

Basic Usage

import { RetryPolicy } from '@bernierllc/retry-policy';

const policy = new RetryPolicy({
  maxRetries: 3,
  initialDelayMs: 1000,
  shouldRetry: (error) => error.code === 'NETWORK_ERROR'
});

// In your retry loop
for (let attempt = 0; attempt <= 3; attempt++) {
  try {
    const result = await someOperation();
    break; // Success, exit retry loop
  } catch (error) {
    const retryResult = policy.evaluateRetry(attempt, error);
    
    if (!retryResult.shouldRetry) {
      throw error; // Don't retry, re-throw error
    }
    
    // Wait before next attempt
    await new Promise(resolve => setTimeout(resolve, retryResult.delay));
  }
}

Custom Backoff Strategy

import { RetryPolicy } from '@bernierllc/retry-policy';

const policy = new RetryPolicy(
  { maxRetries: 5 },
  {
    type: 'exponential',
    baseDelay: 500,
    factor: 3,
    maxDelay: 10000,
    jitter: {
      type: 'full',
      factor: 0.1
    }
  }
);

// This will use exponential backoff with full jitter
const delay = policy.calculateDelay(2); // ~4500ms with jitter

Different Backoff Types

import { RetryPolicy } from '@bernierllc/retry-policy';

// Exponential backoff (default)
const exponential = new RetryPolicy(undefined, {
  type: 'exponential',
  baseDelay: 1000,
  factor: 2
});
// Delays: 1000ms, 2000ms, 4000ms, 8000ms...

// Linear backoff
const linear = new RetryPolicy(undefined, {
  type: 'linear',
  baseDelay: 1000
});
// Delays: 1000ms, 2000ms, 3000ms, 4000ms...

// Constant backoff
const constant = new RetryPolicy(undefined, {
  type: 'constant',
  baseDelay: 1000
});
// Delays: 1000ms, 1000ms, 1000ms, 1000ms...

Jitter Types

import { RetryPolicy } from '@bernierllc/retry-policy';

// Full jitter: random value between 0 and delay
const fullJitter = new RetryPolicy(undefined, {
  jitter: { type: 'full', factor: 0.1 }
});

// Equal jitter: random value between delay/2 and delay
const equalJitter = new RetryPolicy(undefined, {
  jitter: { type: 'equal', factor: 0.1 }
});

// Decorrelated jitter: random value between delay and delay * 3
const decorrelatedJitter = new RetryPolicy(undefined, {
  jitter: { type: 'decorrelated', factor: 0.1 }
});

// No jitter: exact delay values
const noJitter = new RetryPolicy(undefined, {
  jitter: { type: 'none' }
});

Custom Retry Conditions

import { RetryPolicy } from '@bernierllc/retry-policy';

const policy = new RetryPolicy({
  maxRetries: 5,
  shouldRetry: (error) => {
    // Only retry on network errors or 5xx server errors
    if (error.code === 'NETWORK_ERROR') return true;
    if (error.status >= 500 && error.status < 600) return true;
    return false;
  },
  onRetry: (attempt, delay, error) => {
    console.log(`Retry attempt ${attempt} after ${delay}ms due to:`, error.message);
  },
  onFailure: (error) => {
    console.log('All retry attempts exhausted:', error.message);
  }
});

Configuration

RetryPolicyOptions

interface RetryPolicyOptions {
  maxRetries: number;           // Maximum number of retry attempts
  initialDelayMs: number;       // Initial delay in milliseconds
  maxDelayMs?: number;          // Maximum delay in milliseconds
  backoffFactor?: number;       // Exponential backoff factor
  jitter?: boolean;             // Whether to apply jitter
  shouldRetry?: (error: any) => boolean;  // Custom retry condition
  onRetry?: (attempt: number, delay: number, error: any) => void;  // Retry callback
  onFailure?: (error: any) => void;  // Failure callback
}

BackoffConfig

interface BackoffConfig {
  type: 'exponential' | 'linear' | 'constant';  // Backoff strategy
  baseDelay: number;           // Base delay in milliseconds
  maxDelay: number;            // Maximum delay in milliseconds
  factor?: number;             // Backoff factor for exponential
  jitter?: JitterConfig;       // Jitter configuration
}

JitterConfig

interface JitterConfig {
  type: 'none' | 'full' | 'equal' | 'decorrelated';  // Jitter type
  factor?: number;             // Jitter factor (0-1)
}

Default Values

const DEFAULT_RETRY_OPTIONS = {
  maxRetries: 5,
  initialDelayMs: 1000,
  maxDelayMs: 30000,
  backoffFactor: 2,
  jitter: true,
  shouldRetry: () => true,
  onRetry: () => {},
  onFailure: () => {}
};

const DEFAULT_BACKOFF_CONFIG = {
  type: 'exponential',
  baseDelay: 1000,
  maxDelay: 30000,
  factor: 2,
  jitter: { type: 'full', factor: 0.1 }
};

Integration Status

Logger Integration

Status: Not applicable

Justification: This is a pure utility package with no runtime state, side effects, or error conditions that require logging. The RetryPolicy class is stateless and deterministic - it simply calculates retry delays and backoff strategies based on attempt numbers and error types. All errors are returned as structured results that calling code can handle, and there are no background operations, network calls, or state changes that would benefit from structured logging.

Pattern: Pure functional utility - no logger integration needed. Logging is handled by consuming packages that use this retry policy.

NeverHub Integration

Status: Not applicable

Justification: This is a core utility package that provides retry policy calculations. It does not participate in service discovery, event publishing, or service mesh operations. Retry policy calculations are stateless utility operations that don't require service registration or discovery. Service-level packages that use this utility can integrate with NeverHub if needed.

Pattern: Core utility - no service mesh integration needed. Service-level packages that use this retry policy can integrate with NeverHub if needed.

Docs-Suite Integration

Status: Ready

Format: TypeDoc-compatible JSDoc comments are included throughout the source code. All public APIs are documented with examples and type information.

Security

This package has zero runtime dependencies and uses only pure TypeScript functions. Security considerations:

  • No External Dependencies: All retry logic is implemented internally with no external packages at runtime
  • Type Safety: Strict TypeScript typing prevents common runtime errors
  • No Network Calls: Pure computational functions with no I/O operations
  • No Secret Storage: Does not handle or store any sensitive data
  • Audit Clean: npm audit shows no vulnerabilities (zero dependencies)

For security concerns, please open an issue in the repository.

Testing

# Run tests
npm test

# Run tests with coverage
npm run test:coverage

# Run tests in watch mode
npm run test:watch

Contributing

This package follows the Bernier LLC development standards:

  • TypeScript strict mode - All code must pass strict type checking
  • Comprehensive testing - 95%+ test coverage required
  • Documentation - All public APIs must be documented
  • Code style - Follow project ESLint configuration

License

ISC License - see LICENSE file for details.

  • @bernierllc/retry-state - Retry state management and persistence
  • @bernierllc/retry-metrics - Retry performance metrics collection
  • @bernierllc/retry-manager - High-level retry orchestration service

版本与发布记录

@bernierllc/retry-policy 共发布 10 个版本,首次发布于 2025/07/28;过去 12 个月发布了 8 个版本。

版本发布日期距今
0.7.0latest2026/05/205 个月前
0.6.02026/05/205 个月前
0.5.12026/05/195 个月前
0.4.02026/05/195 个月前
0.3.22026/03/117 个月前
0.3.12026/03/048 个月前

关于 @bernierllc/retry-policy 的常见问题

@bernierllc/retry-policy 是什么?

Atomic retry policy utilities with exponential backoff and jitter

如何安装 @bernierllc/retry-policy?

在项目目录运行 npm install @bernierllc/retry-policy,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:p-retry(^5.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@bernierllc/retry-policy 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@bernierllc/retry-policy 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@bernierllc/retry-policy 可以商用吗?

@bernierllc/retry-policy 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@bernierllc/retry-policy 的下载量有多少?

根据 npm 官方下载统计,@bernierllc/retry-policy 在 2026/10/08 – 2026/11/06 这 30 天内被下载 2,481 次,日均约 83 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@bernierllc/retry-policy 还在维护吗?

最新版本 0.7.0 发布于 2026/05/20(5 个月前);至今共发布 10 个版本,其中过去 12 个月发布了 8 个;首次发布于 2025/07/28。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。