Add optional open-circuit fallback callable#105
Open
Louisvranderick wants to merge 3 commits into
Open
Conversation
- Add asyncio.Lock (lazy, thread-safe) and async acall() on CircuitBreaker - Implement state.acall with awaitable support; reject sync/async generators - CircuitOpenState.acall delegates to half-open without re-entering acall - Tests: CircuitBreakerAsyncioTestCase (stdlib asyncio.run) - CHANGELOG: unreleased 1.5.0 note; .gitignore .venv-ci/ Made-with: Cursor
- Add __pybreaker_call_acall kwarg to decorator, wrapping async functions via acall - Raise ValueError when both __pybreaker_call_async and __pybreaker_call_acall are set - Add "Native asyncio (stdlib)" section to README with usage examples - Update features list in README - Tests: decorator wrapping, partial application, mutual exclusion guard Made-with: Cursor
Allow users to pass a `fallback` callable to CircuitBreaker that is invoked with the original arguments when the circuit is open, instead of raising CircuitBreakerError. Works for both sync `call` and async `acall` paths. The fallback is purely opt-in — omitting it preserves existing raise-on-open behaviour. Made-with: Cursor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds an optional
fallbackparameter toCircuitBreaker. When the circuit is open and a fallback is provided, it is called with the original arguments instead of raisingCircuitBreakerError. Works for both sync (call) and async (acall) paths, including awaiting async fallbacks. Purely opt-in — omitting it preserves existing raise-on-open behaviour.Depends on #103 and the decorator/docs PR being merged first.
What changed
CircuitBreaker.__init__acceptsfallback: Callable | None._open_circuit_fallback_or_raise(sync) and_open_circuit_fallback_or_raise_asyncmethods.CircuitOpenState.before_callandCircuitOpenState.acalldelegate to fallback helpers when the timeout has not elapsed.fallbackread-only property.tests/typechecks.pyupdated for mypy coverage.Test plan