Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _compute_state(self):
current_time = time.monotonic()
if current_time < self._stuck_until:
# Skip state changing until
return
if self._state is CircuitBreakerStates.BROKEN:
self._state = CircuitBreakerStates.RECOVERING
self._recovery_at = current_time
return
# Do not compute when not enough statistic
if (
self._state is CircuitBreakerStates.PASSING
and len(self._statistic) < self.BUCKET_COUNT
):
return
recovery_ratio = self.recovery_ratio
if self._state is CircuitBreakerStates.PASSING:
if recovery_ratio >= self.PASSING_BROKEN_THRESHOLD:
self._stuck_until = current_time + self._broken_time
self._state = CircuitBreakerStates.BROKEN
def _exec(self):
counter = self.counter()
self._compute_state()
if self._state is CircuitBreakerStates.PASSING:
yield from self._on_passing(counter)
return
elif self._state is CircuitBreakerStates.BROKEN:
yield from self._on_broken()
return
elif self._state is CircuitBreakerStates.RECOVERING:
yield from self._on_recover(counter)
return
raise NotImplementedError(self._state)