API Gateway 패턴
클라이언트의 단일 진입점으로, 라우팅, 인증, 속도 제한을 담당합니다.
Circuit Breaker
class CircuitBreaker {
constructor(private threshold = 5, private timeout = 60000) {}
private failures = 0;
private state: "closed" | "open" | "half-open" = "closed";
async call(fn: () => Promise<any>) {
if (this.state === "open") throw new Error("Circuit is open");
try {
const result = await fn();
this.reset();
return result;
} catch (err) {
this.failures++;
if (this.failures >= this.threshold) this.state = "open";
throw err;
}
}
}
댓글 0