Skip to content

Commit 0c991ea

Browse files
feat: adding circuit breaker feature
1 parent 6e79f2e commit 0c991ea

1 file changed

Lines changed: 25 additions & 7 deletions

File tree

  • aws_lambda_powertools/utilities/circuit_breaker_alpha

aws_lambda_powertools/utilities/circuit_breaker_alpha/config.py

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,31 @@ def __init__(
6161
ignored_exceptions: tuple[type[Exception], ...] | None = None,
6262
local_cache_max_age: int = 5,
6363
):
64+
self._validate(
65+
failure_threshold=failure_threshold,
66+
recovery_timeout=recovery_timeout,
67+
success_threshold=success_threshold,
68+
handled_exceptions=handled_exceptions,
69+
ignored_exceptions=ignored_exceptions,
70+
local_cache_max_age=local_cache_max_age,
71+
)
72+
73+
self.failure_threshold = failure_threshold
74+
self.recovery_timeout = recovery_timeout
75+
self.success_threshold = success_threshold
76+
self.handled_exceptions = handled_exceptions
77+
self.ignored_exceptions = ignored_exceptions
78+
self.local_cache_max_age = local_cache_max_age
79+
80+
@staticmethod
81+
def _validate(
82+
failure_threshold: int,
83+
recovery_timeout: int,
84+
success_threshold: int,
85+
handled_exceptions: tuple[type[Exception], ...] | None,
86+
ignored_exceptions: tuple[type[Exception], ...] | None,
87+
local_cache_max_age: int,
88+
) -> None:
6489
if handled_exceptions and ignored_exceptions:
6590
raise CircuitBreakerConfigError(
6691
"handled_exceptions and ignored_exceptions are mutually exclusive; pass only one.",
@@ -80,13 +105,6 @@ def __init__(
80105
f"local_cache_max_age must be a non-negative integer, got {local_cache_max_age!r}.",
81106
)
82107

83-
self.failure_threshold = failure_threshold
84-
self.recovery_timeout = recovery_timeout
85-
self.success_threshold = success_threshold
86-
self.handled_exceptions = handled_exceptions
87-
self.ignored_exceptions = ignored_exceptions
88-
self.local_cache_max_age = local_cache_max_age
89-
90108
def counts_as_failure(self, exception: Exception) -> bool:
91109
"""
92110
Decide whether an exception raised by the protected call counts as a circuit failure.

0 commit comments

Comments
 (0)