xiaohongshu
xiaohongshu
circuit-breaker
Implement the circuit breaker pattern to prevent cascade failures in distributed systems. Use when adding resilience to API clients, external service calls, or any operation that can fail and should fail fast.
Full skill instructions
Prevent cascade failures by failing fast when a service is unhealthy.
A circuit breaker has three states:
// circuit-breaker.ts
type CircuitState = 'CLOSED' | 'OPEN' | 'HALF_OPEN';
interface CircuitBreakerConfig {
failureThreshold: number; // Failures before opening
successThreshold: number; // Successes to close from half-open
timeout: number; // Ms before trying half-open
fallback?: () => Promise<any>; // Optional fallback
}
class CircuitBreaker {
private state: CircuitState = 'CLOSED';
private failures = 0;
private successes = 0;
private lastFailureTime = 0;
private readonly config: CircuitBreakerConfig;
constructor(config: Partial<CircuitBreakerConfig> = {}) {
this.config = {
failureThreshold: 5,
successThreshold: 2,
timeout: 30000,
...config,
};
}
async execute<T>(fn: () => Promise<T>): Promise<T> {
if (this.state === 'OPEN') {
if (Date.now() - this.lastFailureTime >= this.config.timeout) {
this.state = 'HALF_OPEN';
} else {
if (this.config.fallback) {
return this.config.fallback();
}
throw new CircuitOpenError('Circuit is OPEN');
}
}
try {
const result = await fn();
this.onSuccess();
return result;
} catch (error) {
this.onFailure();
throw error;
}
}
private onSuccess(): void {
this.failures = 0;
if (this.state === 'HALF_OPEN') {
this.successes++;
if (this.successes >= this.config.successThreshold) {
this.state = 'CLOSED';
this.successes = 0;
}
}
}
private onFailure(): void {
this.failures++;
this.lastFailureTime = Date.now();
if (this.failures >= this.config.failureThreshold) {
this.state = 'OPEN';
}
}
getState(): CircuitState {
return this.state;
}
}
class CircuitOpenError extends Error {
constructor(message: string) {
super(message);
this.name = 'CircuitOpenError';
}
}
export { CircuitBreaker, CircuitBreakerConfig, CircuitOpenError };
# circuit_breaker.py
import time
from enum import Enum
from typing import Callable, TypeVar, Optional
from functools import wraps
T = TypeVar('T')
class CircuitState(Enum):
CLOSED = "closed"
OPEN = "open"
HALF_OPEN = "half_open"
class CircuitOpenError(Exception):
pass
class CircuitBreaker:
def __init__(
self,
failure_threshold: int = 5,
success_threshold: int = 2,
timeout: float = 30.0,
fallback: Optional[Callable] = None,
):
self.failure_threshold = failure_threshold
self.success_threshold = success_threshold
self.timeout = timeout
self.fallback = fallback
self._state = CircuitState.CLOSED
self._failures = 0
self._successes = 0
self._last_failure_time = 0.0
@property
def state(self) -> CircuitState:
return self._state
def __call__(self, fn: Callable[..., T]) -> Callable[..., T]:
@wraps(fn)
def wrapper(*args, **kwargs) -> T:
return self.execute(lambda: fn(*args, **kwargs))
return wrapper
def execute(self, fn: Callable[[], T]) -> T:
if self._state == CircuitState.OPEN:
if time.time() - self._last_failure_time >= self.timeout:
self._state = CircuitState.HALF_OPEN
else:
if self.fallback:
return self.fallback()
raise CircuitOpenError("Circuit is OPEN")
try:
result = fn()
self._on_success()
return result
except Exception as e:
self._on_failure()
raise
def _on_success(self) -> None:
self._failures = 0
if self._state == CircuitState.HALF_OPEN:
self._successes += 1
if self._successes >= self.success_threshold:
self._state = CircuitState.CLOSED
self._successes = 0
def _on_failure(self) -> None:
self._failures += 1
self._last_failure_time = time.time()
if self._failures >= self.failure_threshold:
self._state = CircuitState.OPEN
const paymentCircuit = new CircuitBreaker({
failureThreshold: 3,
timeout: 60000,
fallback: async () => ({ status: 'pending', message: 'Payment service unavailable' }),
});
async function processPayment(amount: number) {
return paymentCircuit.execute(async () => {
const response = await fetch('https://api.stripe.com/v1/charges', {
method: 'POST',
body: JSON.stringify({ amount }),
});
if (!response.ok) throw new Error('Payment failed');
return response.json();
});
}
circuit = CircuitBreaker(failure_threshold=3, timeout=60)
@circuit
def call_external_api(endpoint: str) -> dict:
response = requests.get(endpoint, timeout=5)
response.raise_for_status()
return response.json()
Add metrics to track circuit state:
// With Prometheus
import { Gauge, Counter } from 'prom-client';
const circuitState = new Gauge({
name: 'circuit_breaker_state',
help: 'Current circuit breaker state (0=closed, 1=open, 2=half-open)',
labelNames: ['service'],
});
const circuitTrips = new Counter({
name: 'circuit_breaker_trips_total',
help: 'Total circuit breaker trips',
labelNames: ['service'],
});
xiaohongshu
technical spec
product ux expert
database patterns
Conduct multi-agent task orchestration and workflow coordination.
Initialize project with Conductor artifacts (product definition,
Expert in web animations, transitions, and motion design using Framer Motion and CSS
Creates Mermaid and ASCII diagrams for flowcharts, architecture, ERDs, state machines, mindmaps, and more. Use when user mentions diagram, flowchart, mermaid, ASCII diagram, text diagram, terminal diagram, visualize, C4, mindmap, architecture diagram, sequence diagram, ERD, or needs visual docume...
PostgreSQL bindings for H3 hexagonal grid system. Use when working with H3 cells in Postgres, including spatial indexing, geometry/geography integration, and raster analysis.
Context-Driven Development skill for projects using Conductor. Use this skill when you detect a `conductor/` directory in the project, when working on tasks defined in a `plan.md` file, or when the user asks about tracks, specs, or plans. Automatically applies TDD workflow, tracks task completion...
Display project status, active tracks, and next actions
Official Stakpak application containerization standard operating procedure, a step-by-step guidline to properly dockerize applications. This is a rule book curated by the Stakpak Team.
Generate, edit, and beat-sync AI video with leading models in one workspace.
The world's fastest calendar for remote work
Transform Your Design with AI Designer by ImgCreator.ai
Revolutionizing Video Production with AI-Powered Creativity
Extend an image past the frame and let AI fill the new aspect ratio.
Discover your celebrity doppelgänger with StarByFace!
ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.
Opus.ai: Revolutionize Your Web Experience