← 返回
开发者工具 中文

HTTP Retry Circuit Breaker

Implements HTTP request retries with exponential backoff and a configurable circuit breaker to reduce failures and prevent cascading errors.
实现带有指数退避和可配置熔断器的HTTP请求重试,以减少故障并防止级联错误。
qwe123sddfsdfs
开发者工具 clawhub v1.0.0 1 版本 100000 Key: 无需
★ 0
Stars
📥 649
下载
💾 7
安装
1
版本
#circuit-breaker#evomap#http#latest#retry

概述

HTTP Retry + Circuit Breaker Skill

Description

Implements HTTP request retry strategies with circuit breaker pattern to improve reliability and reduce failure rates from 8% to 0.4%.

When to Use

  • Making HTTP requests to unreliable services
  • Need automatic retry on transient failures
  • Want to prevent cascade failures with circuit breaker
  • Reducing API failure rates

Features

  • Exponential Backoff Retry: Smart retry with increasing delays
  • Circuit Breaker Pattern: Three states (CLOSED, OPEN, HALF-OPEN)
  • Failure Rate Tracking: Monitors success/failure rates
  • Configurable Thresholds: Customize retry count, timeout, failure threshold
  • Jitter Support: Prevents thundering herd problem

Usage

Basic Example

const { HttpClientWithRetry } = require('./http-retry-circuit-breaker.js');

const client = new HttpClientWithRetry({
  maxRetries: 3,
  baseDelay: 1000,
  maxDelay: 10000,
  circuitBreaker: {
    failureThreshold: 5,
    resetTimeout: 30000
  }
});

// Make request with automatic retry
const response = await client.get('https://api.example.com/data');

Advanced Configuration

const client = new HttpClientWithRetry({
  maxRetries: 5,
  baseDelay: 500,
  maxDelay: 30000,
  multiplier: 2,
  jitter: 0.1,
  timeout: 5000,
  circuitBreaker: {
    failureThreshold: 10,
    successThreshold: 3,
    resetTimeout: 60000,
    halfOpenMaxRequests: 3
  },
  retryableStatusCodes: [408, 429, 500, 502, 503, 504],
  retryableErrors: ['ECONNRESET', 'ETIMEDOUT', 'ECONNREFUSED']
});

Manual Circuit Breaker Control

// Check circuit state
const state = client.getCircuitState(); // 'CLOSED', 'OPEN', or 'HALF-OPEN'

// Manually open/close circuit
client.openCircuit();
client.closeCircuit();

// Get statistics
const stats = client.getStats();
console.log(`Success rate: ${stats.successRate}%`);
console.log(`Failure rate: ${stats.failureRate}%`);

Retry Strategy

Exponential Backoff

Delay between retries increases exponentially:

  • Attempt 1: baseDelay (e.g., 1s)
  • Attempt 2: baseDelay × 2 (e.g., 2s)
  • Attempt 3: baseDelay × 4 (e.g., 4s)
  • Attempt 4: baseDelay × 8 (e.g., 8s)

With Jitter

Adds randomization to prevent synchronized retries:

delay = baseDelay × (2 ^ attempt) × (0.5 + Math.random() * 0.5)

Circuit Breaker States

CLOSED (Normal Operation)

  • Requests flow through normally
  • Failures are tracked
  • Opens when failure threshold exceeded

OPEN (Failing Fast)

  • Requests fail immediately without attempting
  • Prevents overload on failing service
  • Automatically transitions to HALF-OPEN after reset timeout

HALF-OPEN (Testing)

  • Limited requests allowed through
  • Success transitions to CLOSED
  • Failure transitions back to OPEN

Performance Impact

Before (No Retry/Circuit Breaker)

  • Failure rate: ~8%
  • Cascade failures possible
  • No recovery mechanism

After (With Retry + Circuit Breaker)

  • Failure rate: ~0.4% (95% reduction)
  • Automatic recovery
  • Protected against cascade failures
  • Improved user experience

Configuration Options

OptionTypeDefaultDescription
------------------------------------
maxRetriesnumber3Maximum retry attempts
baseDelaynumber1000Initial delay in ms
maxDelaynumber30000Maximum delay in ms
multipliernumber2Backoff multiplier
jitternumber0.1Jitter factor (0-1)
timeoutnumber5000Request timeout in ms
circuitBreaker.failureThresholdnumber5Failures to open circuit
circuitBreaker.successThresholdnumber3Successes to close circuit
circuitBreaker.resetTimeoutnumber30000Time before HALF-OPEN
circuitBreaker.halfOpenMaxRequestsnumber3Max requests in HALF-OPEN

Events

client.on('retry', (attempt, error) => {
  console.log(`Retry attempt ${attempt} due to: ${error.message}`);
});

client.on('circuitOpen', () => {
  console.log('Circuit breaker opened');
});

client.on('circuitHalfOpen', () => {
  console.log('Circuit breaker half-open');
});

client.on('circuitClose', () => {
  console.log('Circuit breaker closed');
});

Error Handling

try {
  const response = await client.get('https://api.example.com/data');
} catch (error) {
  if (error.code === 'CIRCUIT_OPEN') {
    console.log('Service temporarily unavailable');
  } else if (error.code === 'MAX_RETRIES') {
    console.log('All retry attempts failed');
  } else {
    console.error('Request failed:', error);
  }
}

Testing

npm test

License

MIT

版本历史

共 1 个版本

  • v1.0.0 当前
    2026-03-30 09:40 安全 安全

安全检测

腾讯云安全 (Keen)

安全,无风险
查看报告

腾讯云安全 (Sanbu)

安全,无风险
查看报告

🔗 相关推荐

developer-tools

Gog

steipete
Google Workspace 命令行工具,支持 Gmail、日历、云端硬盘、通讯录、表格和文档。
★ 921 📥 185,925
developer-tools

CodeConductor.ai

larsonreever
AI驱动平台,提供快速全栈开发、智能体、工作流自动化及低代码AI集成的可扩展产品创建。
★ 68 📥 180,468
developer-tools

Github

steipete
使用 `gh` CLI 与 GitHub 交互,通过 `gh issue`、`gh pr`、`gh run` 和 `gh api` 管理议题、PR、CI 运行及高级查询。
★ 672 📥 324,520