You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Document the circuit breaker interceptor in the Dispatcher API docs with
configuration options, usage examples, and error handling guidance.
Add CircuitBreakerError to the Errors documentation.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Signed-off-by: Matteo Collina <hello@matteocollina.com>
Copy file name to clipboardExpand all lines: docs/docs/api/Dispatcher.md
+113Lines changed: 113 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1202,6 +1202,119 @@ const client = new Client("http://example.com").compose(
1202
1202
- Handles case-insensitive encoding names
1203
1203
- Supports streaming decompression without buffering
1204
1204
1205
+
##### `circuitBreaker`
1206
+
1207
+
The `circuitBreaker` interceptor implements the [circuit breaker pattern](https://martinfowler.com/bliki/CircuitBreaker.html) to prevent cascading failures when upstream services are unavailable or responding with errors.
1208
+
1209
+
The circuit breaker has three states:
1210
+
-**Closed** - Requests flow normally. Failures are counted, and when the threshold is reached, the circuit opens.
1211
+
-**Open** - All requests fail immediately with `CircuitBreakerError` without contacting the upstream service.
1212
+
-**Half-Open** - After the timeout period, a limited number of requests are allowed through to test if the service has recovered.
1213
+
1214
+
**Options**
1215
+
1216
+
-`threshold` - Number of consecutive failures before opening the circuit. Default: `5`.
1217
+
-`timeout` - How long (in milliseconds) the circuit stays open before transitioning to half-open. Default: `30000` (30 seconds).
1218
+
-`successThreshold` - Number of successful requests in half-open state needed to close the circuit. Default: `1`.
1219
+
-`maxHalfOpenRequests` - Maximum number of concurrent requests allowed in half-open state. Default: `1`.
1220
+
-`statusCodes` - Array or Set of HTTP status codes that count as failures. Default: `[500, 502, 503, 504]`.
1221
+
-`errorCodes` - Array or Set of error codes that count as failures. Default: `['UND_ERR_CONNECT_TIMEOUT', 'UND_ERR_HEADERS_TIMEOUT', 'UND_ERR_BODY_TIMEOUT', 'UND_ERR_SOCKET', 'ECONNREFUSED', 'ECONNRESET', 'ETIMEDOUT', 'EPIPE', 'ENOTFOUND', 'ENETUNREACH', 'EHOSTUNREACH', 'EAI_AGAIN']`.
1222
+
-`getKey` - Function to extract a circuit key from request options. Default: uses origin only. Signature: `(opts: DispatchOptions) => string`.
1223
+
-`storage` - Custom `CircuitBreakerStorage` instance for storing circuit states. Useful for sharing state across multiple dispatchers.
Copy file name to clipboardExpand all lines: docs/docs/api/Errors.md
+22Lines changed: 22 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -26,6 +26,7 @@ import { errors } from 'undici'
26
26
|`InformationalError`|`UND_ERR_INFO`| expected error with reason |
27
27
|`ResponseExceededMaxSizeError`|`UND_ERR_RES_EXCEEDED_MAX_SIZE`| response body exceed the max size allowed |
28
28
|`SecureProxyConnectionError`|`UND_ERR_PRX_TLS`| tls connection to a proxy failed |
29
+
|`CircuitBreakerError`|`UND_ERR_CIRCUIT_BREAKER`| circuit breaker is open or half-open, request rejected |
29
30
30
31
Be aware of the possible difference between the global dispatcher version and the actual undici version you might be using. We recommend to avoid the check `instanceof errors.UndiciError` and seek for the `error.code === '<error_code>'` instead to avoid inconsistencies.
31
32
### `SocketError`
@@ -46,3 +47,24 @@ interface SocketInfo {
46
47
```
47
48
48
49
Be aware that in some cases the `.socket` property can be `null`.
50
+
51
+
### `CircuitBreakerError`
52
+
53
+
The `CircuitBreakerError` is thrown when a request is rejected by the circuit breaker interceptor. It has the following properties:
54
+
55
+
-`state` - The current state of the circuit breaker when the error was thrown. Either `'open'` or `'half-open'`.
56
+
-`key` - The circuit key identifying which circuit rejected the request (e.g., the origin URL).
0 commit comments