Protect downstream services with a three-state circuit breaker pattern.
Import
import { CircuitBreaker } from "bytekit/retry-policy";
What it does
CircuitBreaker implements the circuit breaker pattern to prevent cascading failures. When failures exceed a threshold, the breaker opens and immediately rejects calls for a cooldown period instead of overloading an already-failing service.
ApiClient creates an internal CircuitBreaker from the circuitBreaker config option. Use this class directly when you need circuit breaking outside of HTTP requests.
Constructor
const breaker = new CircuitBreaker(config?: CircuitBreakerConfig);
CircuitBreakerConfig
Property
Type
Default
Description
failureThreshold
number
5
Consecutive failures before the circuit opens.
successThreshold
number
2
Consecutive successes in half-open state required to close the circuit.
timeoutMs
number
60000
How long the circuit stays open before transitioning to half-open (ms).
errorMessageFormatter
(retryAfterMs: number) => string
—
Custom formatter for the “circuit open” error message.
Methods
execute<T>(fn)
const result = await breaker.execute(() => callExternalService());
Runs fn if the circuit is closed or half-open. Throws immediately if the circuit is open and the timeout has not expired.
Parameter
Type
Description
fn
() => Promise<T>
The async function to protect.
getState()
const state = breaker.getState();// "closed" | "open" | "half-open"
Returns the current state of the circuit.
reset()
breaker.reset();
Manually resets the circuit to the closed state, clearing all counters.
Requests pass through. Failures increment the counter.
Open
All calls are rejected immediately with an error. The breaker waits for timeoutMs before moving to half-open.
Half-Open
A limited number of calls pass through. If successThreshold consecutive calls succeed, the circuit closes. Any failure re-opens it.
Examples
Basic usage
import { CircuitBreaker } from "bytekit/retry-policy";const breaker = new CircuitBreaker({ failureThreshold: 5, successThreshold: 2, timeoutMs: 30_000,});try { const data = await breaker.execute(() => fetchFromPaymentGateway());} catch (err) { if (breaker.getState() === "open") { console.warn("Circuit is open — using fallback"); return cachedData; } throw err;}
Custom error message
const breaker = new CircuitBreaker({ failureThreshold: 3, timeoutMs: 60_000, errorMessageFormatter: (retryAfterMs) => `Service unavailable. Try again in ${Math.ceil(retryAfterMs / 1000)}s.`,});
Combined with RetryPolicy
import { RetryPolicy, CircuitBreaker } from "bytekit/retry-policy";const breaker = new CircuitBreaker({ failureThreshold: 5 });const retryPolicy = new RetryPolicy({ maxAttempts: 3 });const data = await breaker.execute(() => retryPolicy.execute(() => fetchCriticalData()),);
When using ApiClient, both retry and circuit breaker are configured together in the constructor — you don’t need to compose them manually.
The circuit breaker does not persist state across process restarts. In serverless or multi-instance environments, each instance maintains its own circuit state.
{"docsConfig":{"theme":"mint","$schema":"https://mintlify.com/docs.json","name":"ByteKit","colors":{"primary":"#a31515","light":"#c93d06","dark":"#620101"},"logo":{"light":"https://mintcdn.com/bytekit/GqAGby6knjs69u3M/logo/light.svg?fit=max\u0026auto=format\u0026n=GqAGby6knjs69u3M\u0026q=85\u0026s=0aba21795fc8b8e59ee6f5fc4e06a794","dark":"https://mintcdn.com/bytekit/GqAGby6knjs69u3M/logo/dark.svg?fit=max\u0026auto=format\u0026n=GqAGby6knjs69u3M\u0026q=85\u0026s=b1151342f9cc9b27af680001e4b3ccdf"},"favicon":"/favicon.svg","navbar":{"links":[{"href":"https://github.com/sebamar88/bytekit/issues","label":"Issues"}],"primary":{"type":"button","label":"GitHub","href":"https://github.com/sebamar88/bytekit"}},"footer":{"socials":{"github":"https://github.com/sebamar88/bytekit"}},"contextual":{"options":["copy","view","chatgpt","claude","perplexity","mcp","cursor","vscode"],"display":"header"},"styling":{"latex":false},"navigation":{"global":{"anchors":[{"anchor":"GitHub","icon":"github","href":"https://github.com/sebamar88/bytekit"},{"anchor":"npm","icon":"box","href":"https://www.npmjs.com/package/bytekit"}]},"pages":[]}},"deployment":{"deploymentMetadataValue":{"subdomain":"bytekit","hasAudioNarration":false,"actualSubdomain":"bytekit","customDomains":[],"gitSource":{"type":"github","owner":"sebamar88","repo":"docs","deployBranch":"main","contentDirectory":"","isPrivate":false,"mountPath":"","label":"","_id":"6b10dba89ffae5051c40f579"},"entitlements":{},"buildId":"6ae94a72d1e90e32157f2f60:success","clientVersion":"0.0.4453","searchFilterCounts":[{"language":"default","tag":"API reference","count":30},{"language":"default","tag":"Helpers","count":13},{"language":"default","tag":"Guides","count":8},{"language":"default","tag":"Resilience","count":7},{"language":"default","tag":"Core guides","count":5},{"language":"default","tag":"HTTP","count":4},{"language":"default","tag":"Observability","count":3},{"language":"default","tag":"Getting started","count":3},{"language":"default","tag":"Async","count":2},{"language":"default","tag":"Reference","count":2}]},"assistantConfig":{"enableAskAiButton":false,"enableStarterQuestions":false,"enableContextualStarterQuestions":false,"enableAskAiSearchBar":true},"basePath":"","hasChatPermissions":false,"hasWebMcpPermissions":false,"toggles":[{"name":"kill-isr","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"agent-mintlify-slack","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"agent-integrations-settings","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"rbac-expansion","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"automations-pr-grouping","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"custom-prefix","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"analytics-v7","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"user-flows-analytics","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"no-publish-flow","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"snapshot-serving-live","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"stripTemperatureMiddleware","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"support-platform-pylon-zendesk","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"snapshot-serving","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"instant-rollback","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"granular-notifications-server","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"granular-notifications","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"automations-fable","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"fixed-ai-action-pricing","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"automation-resolve-merge-conflict","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"scoped-publishing","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"mintql","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"page-level-analytics","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"editor-version-history-v2","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"billing-page-v2","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"full-page-search","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"workspace-projects","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"fork-pr-previews","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"revalidation-jwt","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"hobby-abuse-scan","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"dashboard-inbox","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"turbopuffer-dual-write","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"approve-review-flow","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"editor-metadata","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"turbopuffer-serve","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"live-agents","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"starter-pricing","enabled":true,"variant":{"name":"treatment","enabled":true,"payload":{"type":"string","value":"treatment"},"feature_enabled":true},"impressionData":false},{"name":"llms-txt-v2","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"auth-vite-origin","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false}],"starterQuestions":[],"googleFontsInlined":false},"customCss":[]}
{"page":{"pagePath":"api-reference/circuit-breaker","slug":"/api-reference/circuit-breaker","pageMetadata":{"title":"CircuitBreaker","description":"Protect downstream services with a three-state circuit breaker pattern.","href":"/api-reference/circuit-breaker"},"description":{"compiledSource":"\"use strict\";\nconst {jsx: _jsx} = arguments[0];\nconst {useMDXComponents: _provideComponents} = arguments[0];\nfunction _createMdxContent(props) {\n const _components = {\n p: \"p\",\n ..._provideComponents(),\n ...props.components\n };\n return _jsx(_components.p, {\n children: \"Protect downstream services with a three-state circuit breaker pattern.\"\n });\n}\nfunction MDXContent(props = {}) {\n const {wrapper: MDXLayout} = {\n ..._provideComponents(),\n ...props.components\n };\n return MDXLayout ? _jsx(MDXLayout, {\n ...props,\n children: _jsx(_createMdxContent, {\n ...props\n })\n }) : _createMdxContent(props);\n}\nreturn {\n default: MDXContent\n};\n","frontmatter":{},"scope":{}},"pageType":"default","mdx":{"mdxSource":{"compiledSource":"\"use strict\";const{Fragment:_Fragment,jsx:_jsx,jsxs:_jsxs}=arguments[0];const{useMDXComponents:_provideComponents}=arguments[0];function _createMdxContent(props){const _components={code:`code`,em:`em`,p:`p`,pre:`pre`,span:`span`,strong:`strong`,tbody:`tbody`,td:`td`,th:`th`,thead:`thead`,tr:`tr`,..._provideComponents(),...props.components},{CodeBlock,Heading,Info,Table,Tip,Warning}=_components;if(!CodeBlock)_missingMdxReference(`CodeBlock`,true);if(!Heading)_missingMdxReference(`Heading`,true);if(!Info)_missingMdxReference(`Info`,true);if(!Table)_missingMdxReference(`Table`,true);if(!Tip)_missingMdxReference(`Tip`,true);if(!Warning)_missingMdxReference(`Warning`,true);return _jsxs(_Fragment,{children:[_jsx(Heading,{level:`2`,id:`import`,children:`Import`}),`\n`,_jsx(CodeBlock,{filename:``,numberOfLines:`1`,language:`typescript`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light-default dark-plus`,style:{backgroundColor:`#ffffff`,\"--shiki-dark-bg\":`#0B0C0E`,color:`#1f2328`,\"--shiki-dark\":`#D4D4D4`},language:`typescript`,children:_jsxs(_components.code,{language:`typescript`,numberOfLines:`1`,children:[_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:`import`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` { `}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:`CircuitBreaker`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` } `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:`from`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:` \"bytekit/retry-policy\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`;`})]}),`\n`]})})}),`\n`,_jsx(Heading,{level:`2`,id:`what-it-does`,children:`What it does`}),`\n`,_jsxs(_components.p,{children:[_jsx(_components.code,{children:`CircuitBreaker`}),` implements the circuit breaker pattern to prevent cascading failures. When failures exceed a threshold, the breaker `,_jsx(_components.em,{children:`opens`}),` and immediately rejects calls for a cooldown period instead of overloading an already-failing service.`]}),`\n`,_jsx(Info,{children:_jsxs(_components.p,{children:[_jsx(_components.code,{children:`ApiClient`}),` creates an internal `,_jsx(_components.code,{children:`CircuitBreaker`}),` from the `,_jsx(_components.code,{children:`circuitBreaker`}),` config option. Use this class directly when you need circuit breaking outside of HTTP requests.`]})}),`\n`,_jsx(Heading,{level:`2`,id:`constructor`,children:`Constructor`}),`\n`,_jsx(CodeBlock,{filename:``,numberOfLines:`1`,language:`typescript`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light-default dark-plus`,style:{backgroundColor:`#ffffff`,\"--shiki-dark-bg\":`#0B0C0E`,color:`#1f2328`,\"--shiki-dark\":`#D4D4D4`},language:`typescript`,children:_jsxs(_components.code,{language:`typescript`,numberOfLines:`1`,children:[_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`const`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#4FC1FF`},children:` breaker`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:` =`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` new`}),_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:` CircuitBreaker`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:`config`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`?:`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:` CircuitBreakerConfig`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`);`})]}),`\n`]})})}),`\n`,_jsx(Heading,{level:`3`,id:`circuitbreakerconfig`,children:_jsx(_components.code,{children:`CircuitBreakerConfig`})}),`\n`,_jsxs(Table,{children:[_jsx(_components.thead,{children:_jsxs(_components.tr,{children:[_jsx(_components.th,{children:`Property`}),_jsx(_components.th,{children:`Type`}),_jsx(_components.th,{children:`Default`}),_jsx(_components.th,{children:`Description`})]})}),_jsxs(_components.tbody,{children:[_jsxs(_components.tr,{children:[_jsx(_components.td,{children:_jsx(_components.code,{children:`failureThreshold`})}),_jsx(_components.td,{children:_jsx(_components.code,{children:`number`})}),_jsx(_components.td,{\"data-numeric\":`true`,children:_jsx(_components.code,{children:`5`})}),_jsx(_components.td,{children:`Consecutive failures before the circuit opens.`})]}),_jsxs(_components.tr,{children:[_jsx(_components.td,{children:_jsx(_components.code,{children:`successThreshold`})}),_jsx(_components.td,{children:_jsx(_components.code,{children:`number`})}),_jsx(_components.td,{\"data-numeric\":`true`,children:_jsx(_components.code,{children:`2`})}),_jsx(_components.td,{children:`Consecutive successes in half-open state required to close the circuit.`})]}),_jsxs(_components.tr,{children:[_jsx(_components.td,{children:_jsx(_components.code,{children:`timeoutMs`})}),_jsx(_components.td,{children:_jsx(_components.code,{children:`number`})}),_jsx(_components.td,{\"data-numeric\":`true`,children:_jsx(_components.code,{children:`60000`})}),_jsx(_components.td,{children:`How long the circuit stays open before transitioning to half-open (ms).`})]}),_jsxs(_components.tr,{children:[_jsx(_components.td,{children:_jsx(_components.code,{children:`errorMessageFormatter`})}),_jsx(_components.td,{children:_jsx(_components.code,{children:`(retryAfterMs: number) => string`})}),_jsx(_components.td,{children:`—`}),_jsx(_components.td,{children:`Custom formatter for the “circuit open” error message.`})]})]})]}),`\n`,_jsx(Heading,{level:`2`,id:`methods`,children:`Methods`}),`\n`,_jsx(Heading,{level:`3`,id:`execute\u003ct>-fn`,children:_jsx(_components.code,{children:`execute\u003cT>(fn)`})}),`\n`,_jsx(CodeBlock,{filename:``,numberOfLines:`1`,language:`typescript`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light-default dark-plus`,style:{backgroundColor:`#ffffff`,\"--shiki-dark-bg\":`#0B0C0E`,color:`#1f2328`,\"--shiki-dark\":`#D4D4D4`},language:`typescript`,children:_jsxs(_components.code,{language:`typescript`,numberOfLines:`1`,children:[_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`const`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#4FC1FF`},children:` result`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:` =`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:` await`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:` breaker`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`.`}),_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:`execute`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(() `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`=>`}),_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:` callExternalService`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`());`})]}),`\n`]})})}),`\n`,_jsxs(_components.p,{children:[`Runs `,_jsx(_components.code,{children:`fn`}),` if the circuit is closed or half-open. Throws immediately if the circuit is open and the timeout has not expired.`]}),`\n`,_jsxs(Table,{children:[_jsx(_components.thead,{children:_jsxs(_components.tr,{children:[_jsx(_components.th,{children:`Parameter`}),_jsx(_components.th,{children:`Type`}),_jsx(_components.th,{children:`Description`})]})}),_jsx(_components.tbody,{children:_jsxs(_components.tr,{children:[_jsx(_components.td,{children:_jsx(_components.code,{children:`fn`})}),_jsx(_components.td,{children:_jsx(_components.code,{children:`() => Promise\u003cT>`})}),_jsx(_components.td,{children:`The async function to protect.`})]})})]}),`\n`,_jsx(Heading,{level:`3`,id:`getstate`,children:_jsx(_components.code,{children:`getState()`})}),`\n`,_jsx(CodeBlock,{filename:``,numberOfLines:`2`,language:`typescript`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light-default dark-plus`,style:{backgroundColor:`#ffffff`,\"--shiki-dark-bg\":`#0B0C0E`,color:`#1f2328`,\"--shiki-dark\":`#D4D4D4`},language:`typescript`,children:_jsxs(_components.code,{language:`typescript`,numberOfLines:`2`,children:[_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`const`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#4FC1FF`},children:` state`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:` =`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:` breaker`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`.`}),_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:`getState`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`();`})]}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:`// \"closed\" | \"open\" | \"half-open\"`})}),`\n`]})})}),`\n`,_jsx(_components.p,{children:`Returns the current state of the circuit.`}),`\n`,_jsx(Heading,{level:`3`,id:`reset`,children:_jsx(_components.code,{children:`reset()`})}),`\n`,_jsx(CodeBlock,{filename:``,numberOfLines:`1`,language:`typescript`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light-default dark-plus`,style:{backgroundColor:`#ffffff`,\"--shiki-dark-bg\":`#0B0C0E`,color:`#1f2328`,\"--shiki-dark\":`#D4D4D4`},language:`typescript`,children:_jsxs(_components.code,{language:`typescript`,numberOfLines:`1`,children:[_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:`breaker`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`.`}),_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:`reset`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`();`})]}),`\n`]})})}),`\n`,_jsxs(_components.p,{children:[`Manually resets the circuit to the `,_jsx(_components.code,{children:`closed`}),` state, clearing all counters.`]}),`\n`,_jsx(Heading,{level:`2`,id:`state-machine`,children:`State machine`}),`\n`,_jsx(_components.p,{children:`The circuit breaker cycles through three states:`}),`\n`,_jsx(CodeBlock,{numberOfLines:`10`,language:`text`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light-default dark-plus`,style:{backgroundColor:`#ffffff`,\"--shiki-dark-bg\":`#0B0C0E`,color:`#1f2328`,\"--shiki-dark\":`#D4D4D4`},language:`text`,children:_jsxs(_components.code,{language:`text`,numberOfLines:`10`,children:[_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{children:`┌────────┐ failures >= threshold ┌────────┐`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{children:`│ Closed │ ──────────────────────▶ │ Open │`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{children:`└────────┘ └────────┘`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{children:` ▲ │`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{children:` │ │ timeout expires`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{children:` │ ▼`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{children:` │ successes >= threshold ┌───────────┐`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{children:` └──────────────────────────── │ Half-Open │`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{children:` └───────────┘`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{})})]})})}),`\n`,_jsxs(Table,{children:[_jsx(_components.thead,{children:_jsxs(_components.tr,{children:[_jsx(_components.th,{children:`State`}),_jsx(_components.th,{children:`Behavior`})]})}),_jsxs(_components.tbody,{children:[_jsxs(_components.tr,{children:[_jsx(_components.td,{children:_jsx(_components.strong,{children:`Closed`})}),_jsx(_components.td,{children:`Requests pass through. Failures increment the counter.`})]}),_jsxs(_components.tr,{children:[_jsx(_components.td,{children:_jsx(_components.strong,{children:`Open`})}),_jsxs(_components.td,{children:[`All calls are rejected immediately with an error. The breaker waits for `,_jsx(_components.code,{children:`timeoutMs`}),` before moving to half-open.`]})]}),_jsxs(_components.tr,{children:[_jsx(_components.td,{children:_jsx(_components.strong,{children:`Half-Open`})}),_jsxs(_components.td,{children:[`A limited number of calls pass through. If `,_jsx(_components.code,{children:`successThreshold`}),` consecutive calls succeed, the circuit closes. Any failure re-opens it.`]})]})]})]}),`\n`,_jsx(Heading,{level:`2`,id:`examples`,children:`Examples`}),`\n`,_jsx(Heading,{level:`3`,id:`basic-usage`,children:`Basic usage`}),`\n`,_jsx(CodeBlock,{filename:``,numberOfLines:`17`,language:`typescript`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light-default dark-plus`,style:{backgroundColor:`#ffffff`,\"--shiki-dark-bg\":`#0B0C0E`,color:`#1f2328`,\"--shiki-dark\":`#D4D4D4`},language:`typescript`,children:_jsxs(_components.code,{language:`typescript`,numberOfLines:`17`,children:[_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:`import`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` { `}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:`CircuitBreaker`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` } `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:`from`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:` \"bytekit/retry-policy\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`;`})]}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`const`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#4FC1FF`},children:` breaker`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:` =`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` new`}),_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:` CircuitBreaker`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`({`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:` failureThreshold:`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:` 5`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`,`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:` successThreshold:`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:` 2`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`,`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:` timeoutMs:`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:` 30_000`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`,`})]}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`});`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:`try`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` {`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` const`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#4FC1FF`},children:` data`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:` =`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:` await`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:` breaker`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`.`}),_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:`execute`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(() `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`=>`}),_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:` fetchFromPaymentGateway`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`());`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`} `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:`catch`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` (`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:`err`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`) {`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:` if`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` (`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:`breaker`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`.`}),_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:`getState`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`() `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`===`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:` \"open\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`) {`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:` console`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`.`}),_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:`warn`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`\"Circuit is open — using fallback\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`);`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:` return`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:` cachedData`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`;`})]}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` }`})}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:` throw`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:` err`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`;`})]}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`}`})}),`\n`]})})}),`\n`,_jsx(Heading,{level:`3`,id:`custom-error-message`,children:`Custom error message`}),`\n`,_jsx(CodeBlock,{filename:``,numberOfLines:`6`,language:`typescript`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light-default dark-plus`,style:{backgroundColor:`#ffffff`,\"--shiki-dark-bg\":`#0B0C0E`,color:`#1f2328`,\"--shiki-dark\":`#D4D4D4`},language:`typescript`,children:_jsxs(_components.code,{language:`typescript`,numberOfLines:`6`,children:[_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`const`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#4FC1FF`},children:` breaker`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:` =`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` new`}),_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:` CircuitBreaker`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`({`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:` failureThreshold:`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:` 3`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`,`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:` timeoutMs:`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:` 60_000`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`,`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:` errorMessageFormatter`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:`:`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` (`}),_jsx(_components.span,{style:{color:`#953800`,\"--shiki-dark\":`#9CDCFE`},children:`retryAfterMs`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`) `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`=>`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:\" `Service unavailable. Try again in \"}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#569CD6`},children:\"${\"}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:`Math`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#D4D4D4`},children:`.`}),_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:`ceil`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#D4D4D4`},children:`(`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:`retryAfterMs`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:` /`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:` 1000`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#D4D4D4`},children:`)`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#569CD6`},children:`}`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:\"s.`\"}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`,`})]}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`});`})}),`\n`]})})}),`\n`,_jsx(Heading,{level:`3`,id:`combined-with-retrypolicy`,children:`Combined with RetryPolicy`}),`\n`,_jsx(CodeBlock,{filename:``,numberOfLines:`8`,language:`typescript`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light-default dark-plus`,style:{backgroundColor:`#ffffff`,\"--shiki-dark-bg\":`#0B0C0E`,color:`#1f2328`,\"--shiki-dark\":`#D4D4D4`},language:`typescript`,children:_jsxs(_components.code,{language:`typescript`,numberOfLines:`8`,children:[_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:`import`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` { `}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:`RetryPolicy`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`, `}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:`CircuitBreaker`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` } `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:`from`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:` \"bytekit/retry-policy\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`;`})]}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`const`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#4FC1FF`},children:` breaker`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:` =`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` new`}),_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:` CircuitBreaker`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`({ `}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:`failureThreshold:`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:` 5`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` });`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`const`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#4FC1FF`},children:` retryPolicy`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:` =`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` new`}),_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:` RetryPolicy`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`({ `}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:`maxAttempts:`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:` 3`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` });`})]}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`const`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#4FC1FF`},children:` data`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:` =`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:` await`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:` breaker`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`.`}),_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:`execute`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(() `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`=>`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:` retryPolicy`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`.`}),_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:`execute`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(() `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`=>`}),_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:` fetchCriticalData`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`()),`})]}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`);`})}),`\n`]})})}),`\n`,_jsx(Tip,{children:_jsxs(_components.p,{children:[`When using `,_jsx(_components.code,{children:`ApiClient`}),`, both retry and circuit breaker are configured together in the constructor — you don’t need to compose them manually.`]})}),`\n`,_jsx(Warning,{children:_jsx(_components.p,{children:`The circuit breaker does not persist state across process restarts. In serverless or multi-instance environments, each instance maintains its own circuit state.`})})]})}function MDXContent(props={}){const{wrapper:MDXLayout}={..._provideComponents(),...props.components};return MDXLayout?_jsx(MDXLayout,{...props,children:_jsx(_createMdxContent,{...props})}):_createMdxContent(props)}return{default:MDXContent};function _missingMdxReference(id,component){throw new Error(`Expected `+(component?`component`:`object`)+\" `\"+id+\"` to be defined: you likely forgot to import, pass, or provide it.\")}","frontmatter":{},"scope":{"config":{},"pageMetadata":{"title":"CircuitBreaker","description":"Protect downstream services with a three-state circuit breaker pattern.","href":"/api-reference/circuit-breaker"}}},"mdxSourceWithNoJs":null},"mdxExtracts":{"tableOfContents":[{"title":"Import","slug":"import","depth":2,"children":[]},{"title":"What it does","slug":"what-it-does","depth":2,"children":[]},{"title":"Constructor","slug":"constructor","depth":2,"children":[{"title":"CircuitBreakerConfig","slug":"circuitbreakerconfig","depth":3,"children":[]}]},{"title":"Methods","slug":"methods","depth":2,"children":[{"title":"execute\u003cT>(fn)","slug":"execute\u003ct>-fn","depth":3,"children":[]},{"title":"getState()","slug":"getstate","depth":3,"children":[]},{"title":"reset()","slug":"reset","depth":3,"children":[]}]},{"title":"State machine","slug":"state-machine","depth":2,"children":[]},{"title":"Examples","slug":"examples","depth":2,"children":[{"title":"Basic usage","slug":"basic-usage","depth":3,"children":[]},{"title":"Custom error message","slug":"custom-error-message","depth":3,"children":[]},{"title":"Combined with RetryPolicy","slug":"combined-with-retrypolicy","depth":3,"children":[]}]}],"codeExamples":{}},"jsLinks":[],"subdomain":"bytekit","siteName":"ByteKit","title":"CircuitBreaker - ByteKit","contextualStarterQuestions":[],"navMaps":{"firstHrefInVersion":{},"firstHrefInLanguage":{}},"lastModified":"2026-03-28T02:15:40.267Z","originalFileLocation":"api-reference/circuit-breaker.mdx"},"headHtml":"\u003cscript data-mint-head>(function(){var d=document.documentElement,p=null;if(!p){try{p=localStorage.getItem(\"isDarkMode\")}catch(e){}if(p==='true')p='dark';else if(p==='false')p='light';if(p!=='light'\u0026\u0026p!=='dark'\u0026\u0026p!=='system')p=\"system\"}var r=p==='system'?(matchMedia(\"(prefers-color-scheme: dark)\").matches?'dark':'light'):p;d.classList.remove(r==='dark'?'light':'dark');d.classList.add(r);d.setAttribute(\"data-theme-preference\",p)})();\u003c/script>\u003cmeta data-mint-head name=\"description\" content=\"Protect downstream services with a three-state circuit breaker pattern.\">\u003cmeta data-mint-head name=\"application-name\" content=\"ByteKit\">\u003cmeta data-mint-head name=\"generator\" content=\"Mintlify\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"192x192\" href=\"/mintlify-assets/_mintlify/favicons/bytekit/Ihg5LYhgre_j_zHb/_generated/favicon/android-chrome-192x192.png\" media=\"(prefers-color-scheme: light)\">\u003clink data-mint-head rel=\"apple-touch-icon\" type=\"image/png\" sizes=\"180x180\" href=\"/mintlify-assets/_mintlify/favicons/bytekit/Ihg5LYhgre_j_zHb/_generated/favicon/apple-touch-icon.png\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"16x16\" href=\"/mintlify-assets/_mintlify/favicons/bytekit/Ihg5LYhgre_j_zHb/_generated/favicon/favicon-16x16.png\" media=\"(prefers-color-scheme: light)\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"32x32\" href=\"/mintlify-assets/_mintlify/favicons/bytekit/Ihg5LYhgre_j_zHb/_generated/favicon/favicon-32x32.png\" media=\"(prefers-color-scheme: light)\">\u003clink data-mint-head rel=\"shortcut icon\" type=\"image/x-icon\" href=\"/mintlify-assets/_mintlify/favicons/bytekit/Ihg5LYhgre_j_zHb/_generated/favicon/favicon.ico\" media=\"(prefers-color-scheme: light)\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"192x192\" href=\"/mintlify-assets/_mintlify/favicons/bytekit/Ihg5LYhgre_j_zHb/_generated/favicon-dark/android-chrome-192x192.png\" media=\"(prefers-color-scheme: dark)\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"16x16\" href=\"/mintlify-assets/_mintlify/favicons/bytekit/Ihg5LYhgre_j_zHb/_generated/favicon-dark/favicon-16x16.png\" media=\"(prefers-color-scheme: dark)\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"32x32\" href=\"/mintlify-assets/_mintlify/favicons/bytekit/Ihg5LYhgre_j_zHb/_generated/favicon-dark/favicon-32x32.png\" media=\"(prefers-color-scheme: dark)\">\u003clink data-mint-head rel=\"shortcut icon\" type=\"image/x-icon\" href=\"/mintlify-assets/_mintlify/favicons/bytekit/Ihg5LYhgre_j_zHb/_generated/favicon-dark/favicon.ico\" media=\"(prefers-color-scheme: dark)\">\u003cmeta data-mint-head property=\"og:title\" content=\"CircuitBreaker - ByteKit\">\u003cmeta data-mint-head property=\"og:description\" content=\"Protect downstream services with a three-state circuit breaker pattern.\">\u003cmeta data-mint-head property=\"og:site_name\" content=\"ByteKit\">\u003cmeta data-mint-head property=\"og:url\" content=\"https://bytekit.mintlify.app/api-reference/circuit-breaker\">\u003cmeta data-mint-head property=\"og:type\" content=\"website\">\u003cmeta data-mint-head property=\"og:image\" content=\"https://bytekit.mintlify.site/_mintlify/api/og/v2?division=Resilience\u0026amp;title=CircuitBreaker\u0026amp;description=Protect+downstream+services+with+a+three-state+circuit+breaker+pattern.\u0026amp;theme=346d61f686d43f0288ddef33\u0026amp;signature=5e59047a5378482d0d59f7315562f4f17cecfde813357b1c8e62454ccd5077ac\">\u003cmeta data-mint-head property=\"og:image:width\" content=\"1200\">\u003cmeta data-mint-head property=\"og:image:height\" content=\"630\">\u003cmeta data-mint-head name=\"twitter:card\" content=\"summary_large_image\">\u003cmeta data-mint-head name=\"twitter:title\" content=\"CircuitBreaker - ByteKit\">\u003cmeta data-mint-head name=\"twitter:description\" content=\"Protect downstream services with a three-state circuit breaker pattern.\">\u003cmeta data-mint-head name=\"twitter:image\" content=\"https://bytekit.mintlify.site/_mintlify/api/og/v2?division=Resilience\u0026amp;title=CircuitBreaker\u0026amp;description=Protect+downstream+services+with+a+three-state+circuit+breaker+pattern.\u0026amp;theme=346d61f686d43f0288ddef33\u0026amp;signature=5e59047a5378482d0d59f7315562f4f17cecfde813357b1c8e62454ccd5077ac\">\u003cmeta data-mint-head name=\"twitter:image:width\" content=\"1200\">\u003cmeta data-mint-head name=\"twitter:image:height\" content=\"630\">\u003cmeta data-mint-head name=\"msapplication-config\" content=\"/mintlify-assets/_mintlify/favicons/bytekit/Ihg5LYhgre_j_zHb/_generated/favicon/browserconfig.xml\">\u003cmeta data-mint-head name=\"apple-mobile-web-app-title\" content=\"ByteKit\">\u003cmeta data-mint-head name=\"msapplication-TileColor\" content=\"#a31515\">\u003clink data-mint-head rel=\"canonical\" href=\"https://bytekit.mintlify.app/api-reference/circuit-breaker\">\u003clink data-mint-head rel=\"alternate\" type=\"application/xml\" href=\"/sitemap.xml\">\u003clink data-mint-head rel=\"alternate\" type=\"text/markdown\" href=\"/api-reference/circuit-breaker.md\">\u003cscript data-mint-head type=\"application/ld+json\">{\"@context\":\"https://schema.org\",\"@graph\":[{\"@type\":\"Organization\",\"@id\":\"https://bytekit.mintlify.app/#organization\",\"name\":\"ByteKit\",\"url\":\"https://bytekit.mintlify.app\",\"logo\":{\"@type\":\"ImageObject\",\"url\":\"https://mintcdn.com/bytekit/GqAGby6knjs69u3M/logo/light.svg?fit=max\u0026auto=format\u0026n=GqAGby6knjs69u3M\u0026q=85\u0026s=0aba21795fc8b8e59ee6f5fc4e06a794\"}},{\"@type\":\"WebSite\",\"@id\":\"https://bytekit.mintlify.app#website\",\"name\":\"ByteKit\",\"url\":\"https://bytekit.mintlify.app\",\"publisher\":{\"@id\":\"https://bytekit.mintlify.app/#organization\"}},{\"@type\":\"WebPage\",\"@id\":\"https://bytekit.mintlify.app/api-reference/circuit-breaker#webpage\",\"url\":\"https://bytekit.mintlify.app/api-reference/circuit-breaker\",\"name\":\"CircuitBreaker\",\"description\":\"Protect downstream services with a three-state circuit breaker pattern.\",\"dateModified\":\"2026-03-28T02:15:40.267Z\",\"isPartOf\":{\"@id\":\"https://bytekit.mintlify.app#website\"},\"breadcrumb\":{\"@id\":\"https://bytekit.mintlify.app/api-reference/circuit-breaker#breadcrumb\"}},{\"@type\":\"BreadcrumbList\",\"@id\":\"https://bytekit.mintlify.app/api-reference/circuit-breaker#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Resilience\",\"item\":\"https://bytekit.mintlify.app/api-reference/resilience\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"CircuitBreaker\",\"item\":\"https://bytekit.mintlify.app/api-reference/circuit-breaker\"}]},{\"@type\":[\"Article\",\"TechArticle\"],\"@id\":\"https://bytekit.mintlify.app/api-reference/circuit-breaker#article\",\"headline\":\"CircuitBreaker\",\"name\":\"CircuitBreaker\",\"description\":\"Protect downstream services with a three-state circuit breaker pattern.\",\"url\":\"https://bytekit.mintlify.app/api-reference/circuit-breaker\",\"mainEntityOfPage\":{\"@id\":\"https://bytekit.mintlify.app/api-reference/circuit-breaker#webpage\"},\"dateModified\":\"2026-03-28T02:15:40.267Z\",\"publisher\":{\"@id\":\"https://bytekit.mintlify.app/#organization\"},\"isPartOf\":{\"@id\":\"https://bytekit.mintlify.app#website\"}}]}\u003c/script>\u003cscript data-mint-head type=\"application/ld+json\">{\"@context\":\"https://schema.org\",\"@type\":\"WebSite\",\"name\":\"ByteKit\",\"publisher\":{\"@type\":\"Organization\",\"name\":\"ByteKit\"},\"provider\":{\"@type\":\"Organization\",\"name\":\"Mintlify\",\"url\":\"https://mintlify.com\"}}\u003c/script>","agentPointerHref":"/api-reference/circuit-breaker.md","siteName":"ByteKit","nav":{"global":{"anchors":[{"anchor":"GitHub","icon":"github","href":"https://github.com/sebamar88/bytekit"},{"anchor":"npm","icon":"box","href":"https://www.npmjs.com/package/bytekit"}]},"tabs":[{"tab":"Guides","pages":[{"title":"Guides","href":""}]},{"tab":"Reference","pages":[{"title":"Reference","href":"/reference/overview"}]},{"tab":"API reference","groups":[{"group":"Overview","pages":[{"title":"Overview","href":"/api-reference/overview"}]},{"group":"HTTP","pages":[{"title":"HTTP client","href":"/api-reference/http-client"},{"title":"ApiClient","href":"/api-reference/api-client"},{"title":"ResponseValidator","href":"/api-reference/response-validator"},{"title":"Schema adapters","href":"/api-reference/schema-adapters"},{"title":"UrlHelper","href":"/api-reference/url-helper"}]},{"group":"Resilience","pages":[{"title":"Resilience","href":"/api-reference/resilience"},{"title":"RetryPolicy","description":"Automatic retries with exponential backoff and jitter for transient failures.","href":"/api-reference/retry-policy"},{"title":"CircuitBreaker","href":"/api-reference/circuit-breaker"},{"title":"RequestCache","description":"Class reference and examples for RequestCache — a lightweight cache for request results with TTL, stale-while-revalidate, and pattern invalidation.","href":"/api-reference/request-cache"},{"title":"RequestDeduplicator","href":"/api-reference/request-deduplicator"},{"title":"RateLimiter","href":"/api-reference/rate-limiter"},{"title":"ErrorBoundary","href":"/api-reference/error-boundary"}]},{"group":"Async","pages":[{"title":"Async","href":"/api-reference/async"},{"title":"Pipeline","href":"/api-reference/pipeline"}]},{"group":"Observability","pages":[{"title":"Observability","href":"/api-reference/observability"},{"title":"Logger","href":"/api-reference/logger"},{"title":"Profiler","href":"/api-reference/profiler"}]},{"group":"Helpers","pages":[{"title":"Helpers","href":"/api-reference/helpers"},{"title":"EnvManager","href":"/api-reference/env-manager"},{"title":"StorageManager","href":"/api-reference/storage-utils"},{"title":"FileUploadHelper","href":"/api-reference/file-upload-helper"},{"title":"StreamingHelper","href":"/api-reference/streaming-helper"},{"title":"WebSocketHelper","href":"/api-reference/websocket-helper"},{"title":"CacheManager","href":"/api-reference/cache-manager"},{"title":"CompressionUtils","href":"/api-reference/compression-utils"},{"title":"CryptoUtils","href":"/api-reference/crypto-utils"},{"title":"DiffUtils","href":"/api-reference/diff-utils"},{"title":"EventEmitter","href":"/api-reference/event-emitter"},{"title":"PollingHelper","href":"/api-reference/polling-helper"},{"title":"UrlHelper","href":"/api-reference/url-helper"}]}]}]},"basePath":"","title":"CircuitBreaker - ByteKit","deploymentId":"dpl_BLZw3QngerHodH9E8mGgC4v752y3","buildId":"dpl_BLZw3QngerHodH9E8mGgC4v752y3","dynamics":[],"chunks":[]}