circuit-breaker-pattern
Implement circuit breaker patterns for fault tolerance, automatic failure detection, and fallback mechanisms. Use when calling external services, handling cascading failures, or implementing resilience patterns.
适合你,如果正在构建需要高可用的分布式系统或微服务
npx oh-my-skill add aj-geddes/useful-ai-prompts/circuit-breaker-patterncurl -fsSL https://oh-my-skill.com/install.sh | bash -s -- aj-geddes/useful-ai-prompts/circuit-breaker-patternnpx oh-my-skill verify aj-geddes/useful-ai-prompts/circuit-breaker-pattern怎么用
商店整理自技能原文 · 版本 3f5182c · 表述以原文为准装上后,Claude 会实现断路器模式,用于监控外部服务调用。当检测到连续失败时,断路器会打开,停止后续请求并触发回退机制;一段时间后尝试恢复,成功则关闭断路器。
当需要调用外部服务、处理微服务通信、数据库连接等场景,或需要防止级联故障、实现回退机制时触发。
技能原文 SKILL.md
Circuit Breaker Pattern
Table of Contents
- [Overview](#overview)
- [When to Use](#when-to-use)
- [Quick Start](#quick-start)
- [Reference Guides](#reference-guides)
- [Best Practices](#best-practices)
Overview
Implement circuit breaker patterns to prevent cascading failures and provide graceful degradation when dependencies fail.
When to Use
- External API calls
- Microservices communication
- Database connections
- Third-party service integrations
- Preventing cascading failures
- Implementing fallback mechanisms
- Rate limiting protection
- Timeout handling
Quick Start
Minimal working example:
enum CircuitState {
CLOSED = "CLOSED",
OPEN = "OPEN",
HALF_OPEN = "HALF_OPEN",
}
interface CircuitBreakerConfig {
failureThreshold: number;
successThreshold: number;
timeout: number;
resetTimeout: number;
}
interface CircuitBreakerStats {
failures: number;
successes: number;
consecutiveFailures: number;
consecutiveSuccesses: number;
lastFailureTime?: number;
}
class CircuitBreaker {
private state: CircuitState = CircuitState.CLOSED;
private stats: CircuitBreakerStats = {
failures: 0,
// ... (see reference guides for full implementation)
Reference Guides
Detailed implementations in the references/ directory:
| Guide | Contents | |---|---| | [TypeScript Circuit Breaker](references/typescript-circuit-breaker.md) | TypeScript Circuit Breaker | | [Circuit Breaker with Monitoring](references/circuit-breaker-with-monitoring.md) | Circuit Breaker with Monitoring | | [Opossum-Style Circuit Breaker (Node.js)](references/opossum-style-circuit-breaker-nodejs.md) | Opossum-Style Circuit Breaker (Node.js) | | [Python Circuit Breaker](references/python-circuit-breaker.md) | Python Circuit Breaker | | [Resilience4j-Style (Java)](references/resilience4j-style-java.md) | Resilience4j-Style (Java) |
Best Practices
✅ DO
- Use appropriate thresholds for your use case
- Implement fallback mechanisms
- Monitor circuit breaker states
- Set reasonable timeouts
- Use exponential backoff
- Log state transitions
- Alert on frequent trips
- Test circuit breaker behavior
- Use per-dependency breakers
- Implement health checks
❌ DON'T
- Use same breaker for all dependencies
- Set unrealistic thresholds
- Skip fallback implementation
- Ignore open circuit breakers
- Use overly aggressive reset timeouts
- Forget to monitor