Reference:
Resilience4j (Circuit Breaker)
let failures = 0;
let state = "CLOSED";
async function callService() {
if (state === "OPEN") {
throw new Error("Service unavailable");
}
try {
const result = await fetch("<https://api.example.com>");
failures = 0;
return result;
} catch (err) {
failures++;
if (failures >= 3) {
state = "OPEN";
setTimeout(() => state = "HALF", 5000);
}
throw err;
}
}