Core¶
이 페이지는 사용자에게 노출되는 주요 클래스와 예외를 문서화합니다. 이는 Fluxgate 사용을 위한 주요 진입점입니다.
Circuit Breaker 클래스¶
이것은 Circuit Breaker를 생성하기 위해 인스턴스화할 주요 클래스입니다.
fluxgate.circuitbreaker.CircuitBreaker
¶
CircuitBreaker(
name: str,
window: IWindow | None = None,
tracker: ITracker | None = None,
tripper: ITripper | None = None,
retry: IRetry | None = None,
permit: IPermit | None = None,
slow_threshold: float = 60.0,
listeners: Iterable[IListener] = (),
)
Synchronous circuit breaker implementation.
Protects your service from cascading failures by monitoring call failures and temporarily blocking calls when a failure threshold is reached.
The circuit breaker operates in three main states:
- CLOSED: Normal operation, calls pass through
- OPEN: Failure threshold exceeded, calls are blocked
- HALF_OPEN: Testing if the service recovered, limited calls allowed
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Circuit breaker identifier |
required |
window
|
IWindow | None
|
Sliding window for metrics collection (default: CountWindow(100)) |
None
|
tracker
|
ITracker | None
|
Determines which exceptions to track as failures (default: All()) |
None
|
tripper
|
ITripper | None
|
Condition to open/close the circuit based on metrics (default: MinRequests(100) & (FailureRate(0.5) | SlowRate(1.0))) |
None
|
retry
|
IRetry | None
|
Strategy for transitioning from OPEN to HALF_OPEN (default: Cooldown(60.0)) |
None
|
permit
|
IPermit | None
|
Strategy for allowing calls in HALF_OPEN state (default: RampUp(0.0, 1.0, 60.0)) |
None
|
slow_threshold
|
float
|
Duration threshold in seconds to mark calls as slow (default: 60.0) |
60.0
|
listeners
|
Iterable[IListener]
|
Event listeners for state transitions (default: empty) |
()
|
Examples:
Basic usage with defaults:
>>> cb = CircuitBreaker("my-service")
>>> @cb
... def call_api():
... return requests.get("https://api.example.com")
Custom configuration:
>>> cb = CircuitBreaker(
... name="payment_api",
... tracker=TypeOf(ConnectionError),
... tripper=MinRequests(10) & FailureRate(0.5),
... slow_threshold=1.0,
... )
Note
This implementation is NOT thread-safe. Each process maintains its own independent circuit breaker state. For asyncio applications, use AsyncCircuitBreaker instead.
Source code in fluxgate/circuitbreaker.py
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | |
__call__
¶
__call__(func: Callable[P, R]) -> Callable[P, R]
__call__(
func: None = None,
*,
fallback: Callable[[Exception], R] | None = None,
) -> Callable[[Callable[P, R]], Callable[P, R]]
__call__(
func: Callable[P, R] | None = None,
*,
fallback: Callable[[Exception], R] | None = None,
) -> (
Callable[P, R]
| Callable[[Callable[P, R]], Callable[P, R]]
)
Decorate a function with circuit breaker protection.
Examples:
>>> @cb
... def api_call():
... return requests.get("https://api.example.com")
>>> @cb(fallback=lambda e: cached_value)
... def api_call():
... return requests.get("https://api.example.com")
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[P, R] | None
|
Function to protect |
None
|
fallback
|
Callable[[Exception], R] | None
|
Optional function to call on exception. Receives the exception as argument and should return a fallback value or re-raise. |
None
|
Returns:
| Type | Description |
|---|---|
Callable[P, R] | Callable[[Callable[P, R]], Callable[P, R]]
|
Wrapped function with circuit breaker behavior |
Raises:
| Type | Description |
|---|---|
CallNotPermittedError
|
When circuit is OPEN or FORCED_OPEN (if no fallback) |
Source code in fluxgate/circuitbreaker.py
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 | |
call
¶
call(
func: Callable[P, R], /, *args: args, **kwargs: kwargs
) -> R
Execute a function with circuit breaker protection.
Examples:
>>> cb.call(requests.get, "https://api.example.com")
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[P, R]
|
Function to execute |
required |
*args
|
args
|
Positional arguments for the function |
()
|
**kwargs
|
kwargs
|
Keyword arguments for the function |
{}
|
Returns:
| Type | Description |
|---|---|
R
|
Function result |
Raises:
| Type | Description |
|---|---|
CallNotPermittedError
|
When circuit is OPEN or FORCED_OPEN |
Source code in fluxgate/circuitbreaker.py
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 | |
call_with_fallback
¶
call_with_fallback(
func: Callable[P, R],
fallback: Callable[[Exception], R],
/,
*args: args,
**kwargs: kwargs,
) -> R
Execute a function with circuit breaker protection and fallback.
Examples:
>>> cb.call_with_fallback(fetch_data, lambda e: cached_data)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[P, R]
|
Function to execute |
required |
fallback
|
Callable[[Exception], R]
|
Function to call on exception. Receives the exception as argument and should return a fallback value or re-raise. |
required |
*args
|
args
|
Positional arguments for the function |
()
|
**kwargs
|
kwargs
|
Keyword arguments for the function |
{}
|
Returns:
| Type | Description |
|---|---|
R
|
Function result or fallback result |
Source code in fluxgate/circuitbreaker.py
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 | |
info
¶
info() -> CircuitBreakerInfo
Get current circuit breaker state and metrics.
Returns:
| Type | Description |
|---|---|
CircuitBreakerInfo
|
Dictionary with circuit breaker state information |
Source code in fluxgate/circuitbreaker.py
370 371 372 373 374 375 376 377 378 379 380 381 382 | |
reset
¶
reset() -> None
Reset circuit breaker to CLOSED state and clear metrics.
Source code in fluxgate/circuitbreaker.py
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 | |
disable
¶
disable() -> None
Disable circuit breaker (all calls pass through without tracking).
Source code in fluxgate/circuitbreaker.py
401 402 403 | |
metrics_only
¶
metrics_only() -> None
Enable metrics-only mode (track metrics but never open circuit).
Source code in fluxgate/circuitbreaker.py
405 406 407 | |
force_open
¶
force_open() -> None
Force circuit breaker to OPEN state (all calls blocked).
Source code in fluxgate/circuitbreaker.py
409 410 411 | |
fluxgate.circuitbreaker.AsyncCircuitBreaker
¶
AsyncCircuitBreaker(
name: str,
window: IWindow | None = None,
tracker: ITracker | None = None,
tripper: ITripper | None = None,
retry: IRetry | None = None,
permit: IPermit | None = None,
slow_threshold: float = 60.0,
max_half_open_calls: int = 10,
listeners: Iterable[IListener | IAsyncListener] = (),
)
Asynchronous circuit breaker implementation for asyncio applications.
Thread-safe circuit breaker with async/await support. Uses asyncio locks to coordinate state transitions and metric updates across concurrent tasks.
The circuit breaker operates in three main states:
- CLOSED: Normal operation, calls pass through
- OPEN: Failure threshold exceeded, calls are blocked
- HALF_OPEN: Testing if the service recovered, limited calls allowed
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Circuit breaker identifier |
required |
window
|
IWindow | None
|
Sliding window for metrics collection (default: CountWindow(100)) |
None
|
tracker
|
ITracker | None
|
Determines which exceptions to track as failures (default: All()) |
None
|
tripper
|
ITripper | None
|
Condition to open/close the circuit based on metrics (default: MinRequests(100) & (FailureRate(0.5) | SlowRate(1.0))) |
None
|
retry
|
IRetry | None
|
Strategy for transitioning from OPEN to HALF_OPEN (default: Cooldown(60.0)) |
None
|
permit
|
IPermit | None
|
Strategy for allowing calls in HALF_OPEN state (default: RampUp(0.0, 1.0, 60.0)) |
None
|
slow_threshold
|
float
|
Duration threshold in seconds to mark calls as slow (default: 60.0) |
60.0
|
max_half_open_calls
|
int
|
Maximum concurrent calls allowed in HALF_OPEN state (default: 10) |
10
|
listeners
|
Iterable[IListener | IAsyncListener]
|
Event listeners for state transitions (default: empty) |
()
|
Examples:
Basic usage with defaults:
>>> cb = AsyncCircuitBreaker("my-service")
>>> @cb
... async def call_api():
... async with httpx.AsyncClient() as client:
... return await client.get("https://api.example.com")
Custom configuration:
>>> cb = AsyncCircuitBreaker(
... name="payment_api",
... tracker=TypeOf(httpx.ConnectError),
... tripper=MinRequests(10) & FailureRate(0.5),
... slow_threshold=1.0,
... )
Note
Uses asyncio locks for thread safety within a single event loop. Each process maintains its own independent circuit breaker state.
Source code in fluxgate/circuitbreaker.py
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 | |
__call__
¶
__call__(
func: Callable[P, Awaitable[R]],
) -> Callable[P, Awaitable[R]]
__call__(
func: None = None,
*,
fallback: Callable[[Exception], R] | None = None,
) -> Callable[
[Callable[P, Awaitable[R]]],
Callable[P, Awaitable[R]],
]
__call__(
func: Callable[P, Awaitable[R]] | None = None,
*,
fallback: Callable[[Exception], R] | None = None,
) -> (
Callable[P, Awaitable[R]]
| Callable[
[Callable[P, Awaitable[R]]],
Callable[P, Awaitable[R]],
]
)
Decorate an awaitable function with circuit breaker protection.
Examples:
>>> @cb
... async def api_call():
... async with httpx.AsyncClient() as client:
... return await client.get("https://api.example.com")
>>> @cb(fallback=lambda e: cached_value)
... async def api_call():
... return await fetch_data()
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[P, Awaitable[R]] | None
|
Awaitable function to protect |
None
|
fallback
|
Callable[[Exception], R] | None
|
Optional function to call on exception. Receives the exception as argument and should return a fallback value or re-raise. |
None
|
Returns:
| Type | Description |
|---|---|
Callable[P, Awaitable[R]] | Callable[[Callable[P, Awaitable[R]]], Callable[P, Awaitable[R]]]
|
Wrapped awaitable function with circuit breaker behavior |
Raises:
| Type | Description |
|---|---|
CallNotPermittedError
|
When circuit is OPEN or FORCED_OPEN (if no fallback) |
Source code in fluxgate/circuitbreaker.py
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 | |
call
async
¶
call(
func: Callable[P, Awaitable[R]],
/,
*args: args,
**kwargs: kwargs,
) -> R
Execute an awaitable function with circuit breaker protection.
Examples:
>>> await cb.call(client.get, "https://api.example.com")
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[P, Awaitable[R]]
|
Awaitable function to execute |
required |
*args
|
args
|
Positional arguments for the function |
()
|
**kwargs
|
kwargs
|
Keyword arguments for the function |
{}
|
Returns:
| Type | Description |
|---|---|
R
|
Function result |
Raises:
| Type | Description |
|---|---|
CallNotPermittedError
|
When circuit is OPEN or FORCED_OPEN |
Source code in fluxgate/circuitbreaker.py
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 | |
call_with_fallback
async
¶
call_with_fallback(
func: Callable[P, Awaitable[R]],
fallback: Callable[[Exception], R],
/,
*args: args,
**kwargs: kwargs,
) -> R
Execute an awaitable function with circuit breaker protection and fallback.
Examples:
>>> await cb.call_with_fallback(fetch_data, lambda e: cached_data)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[P, Awaitable[R]]
|
Awaitable function to execute |
required |
fallback
|
Callable[[Exception], R]
|
Function to call on exception. Receives the exception as argument and should return a fallback value or re-raise. |
required |
*args
|
args
|
Positional arguments for the function |
()
|
**kwargs
|
kwargs
|
Keyword arguments for the function |
{}
|
Returns:
| Type | Description |
|---|---|
R
|
Function result or fallback result |
Source code in fluxgate/circuitbreaker.py
770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 | |
info
¶
info() -> CircuitBreakerInfo
Get current circuit breaker state and metrics.
Returns:
| Type | Description |
|---|---|
CircuitBreakerInfo
|
Dictionary with circuit breaker state information |
Source code in fluxgate/circuitbreaker.py
798 799 800 801 802 803 804 805 806 807 808 809 810 | |
reset
async
¶
reset() -> None
Reset circuit breaker to CLOSED state and clear metrics.
Source code in fluxgate/circuitbreaker.py
812 813 814 815 | |
disable
async
¶
disable() -> None
Disable circuit breaker (all calls pass through without tracking).
Source code in fluxgate/circuitbreaker.py
817 818 819 820 | |
metrics_only
async
¶
metrics_only() -> None
Enable metrics-only mode (track metrics but never open circuit).
Source code in fluxgate/circuitbreaker.py
822 823 824 825 | |
force_open
async
¶
force_open() -> None
Force circuit breaker to OPEN state (all calls blocked).
Source code in fluxgate/circuitbreaker.py
827 828 829 830 | |
상태 및 예외¶
이것은 가장 일반적으로 상호 작용할 열거형(enum) 및 예외입니다.
fluxgate.state.StateEnum
¶
Bases: Enum
fluxgate.errors.CallNotPermittedError
¶
CallNotPermittedError(message: str)
Bases: Exception
Raised when a call is not permitted due to circuit breaker state.
Source code in fluxgate/errors.py
4 5 6 | |