API Reference¶
infuse.app
¶
-
class
Infuse
[source]¶ -
classmethod
init_app
(app)[source]¶ The initial entrypoint to initialize Infuse.
Loads Infuse specific configurations
Attaches a listener to change state to value defined in
INFUSE_INITIAL_STATE
.Patches the Service object that handles circuit state when sending requests to other services.
- Parameters
app (insanic.app.Insanic) –
- Return type
None
-
classmethod
infuse.breaker
¶
-
class
AioCircuitBreaker
(fail_max=5, reset_timeout=60, exclude=None, listeners=None, state_storage=None, name=None)[source]¶ Bases:
pybreaker.CircuitBreaker
More abstractly, circuit breakers exists to allow one subsystem to fail without destroying the entire system. This is done by wrapping dangerous operations (typically integration points) with a component that can circumvent calls when the system is not healthy. This pattern is described by Michael T. Nygard in his book ‘Release It!’.
-
async classmethod
initialize
(fail_max=5, reset_timeout=60, exclude=None, listeners=None, state_storage=None, name=None)[source]¶ - Parameters
fail_max (int) –
reset_timeout (int) –
exclude (List[Exception]) –
listeners (List) –
state_storage (pybreaker.CircuitBreakerStorage) –
name (str) –
-
property
fail_counter
¶ Returns the current number of consecutive failures.
-
property
state
¶ Returns the current state of this circuit breaker.
-
property
current_state
¶ Returns a string that identifies this circuit breaker’s state, i.e., ‘closed’, ‘open’, ‘half-open’.
-
async
call
(func, *args, **kwargs)[source]¶ Calls async func with the given args and kwargs according to the rules implemented by the current state of this circuit breaker. Return a closure to prevent import errors when using without tornado present
-
async
open
()[source]¶ Opens the circuit, e.g., the following calls will immediately fail until timeout elapses.
- Return type
None
-
async classmethod
infuse.breaker.states
¶
-
class
AioCircuitBreakerState
(cb, name)[source]¶ Bases:
pybreaker.CircuitBreakerState
Asyncio implementation for the behavior needed by all circuit breaker states.
-
async classmethod
initialize
(cb, prev_state=None, notify=False)[source]¶ - Parameters
prev_state (str) –
notify (bool) –
-
async
call
(func, *args, **kwargs)[source]¶ Calls async func with the given args and kwargs, and updates the circuit breaker state according to the result. Return a closure to prevent import errors when using without tornado present
- Parameters
func (Callable) –
-
async
before_call
(func, *args, **kwargs)[source]¶ Override this method to be notified before a call to the guarded operation is attempted.
- Parameters
func (Callable) –
-
async classmethod
-
class
AioCircuitClosedState
(cb, prev_state=None, notify=False)[source]¶ Bases:
infuse.breaker.states.AioCircuitBreakerState
In the normal “closed” state, the circuit breaker executes operations as usual. If the call succeeds, nothing happens. If it fails, however, the circuit breaker makes a note of the failure. Once the number of failures exceeds a threshold, the circuit breaker trips and “opens” the circuit.
-
class
AioCircuitOpenState
(cb, prev_state=None, notify=False)[source]¶ Bases:
infuse.breaker.states.AioCircuitBreakerState
When the circuit is “open”, calls to the circuit breaker fail immediately, without any attempt to execute the real operation. This is indicated by the
CircuitBreakerError
exception. After a suitable amount of time, the circuit breaker decides that the operation has a chance of succeeding, so it goes into the “half-open” state.-
async
before_call
(func, *args, **kwargs)[source]¶ After the timeout elapses, move the circuit breaker to the “half-open” state; otherwise, raises
CircuitBreakerError
without any attempt to execute the real operation.- Raises
CircuitBreakerError – If timeout has not elapsed.
- Parameters
func (Callable) –
- Return type
Any
-
async
-
class
AioCircuitHalfOpenState
(cb, prev_state=None, notify=False)[source]¶ Bases:
infuse.breaker.states.AioCircuitBreakerState
In the “half-open” state, the next call to the circuit breaker is allowed to execute the dangerous operation. Should the call succeed, the circuit breaker resets and returns to the “closed” state. If this trial call fails, however, the circuit breaker returns to the “open” state until another timeout elapses.
infuse.breaker.storages
¶
-
class
CircuitBreakerStorage
(name)[source]¶ Bases:
object
Defines the underlying storage for a circuit breaker - the underlying implementation should be in a subclass that overrides the method this class defines.
-
property
name
¶ Returns a human friendly name that identifies this state.
-
property
state
¶ Override this method to retrieve the current circuit breaker state.
-
property
counter
¶ Override this method to retrieve the current value of the failure counter.
-
property
opened_at
¶ Override this method to retrieve the most recent value of when the circuit was opened.
-
property
-
class
CircuitMemoryStorage
(state)[source]¶ Bases:
pybreaker.CircuitBreakerStorage
Implements a CircuitBreakerStorage in local memory.
-
property
state
¶ Returns the current circuit breaker state.
-
property
counter
¶ Returns the current value of the failure counter.
-
property
opened_at
¶ Returns the most recent value of when the circuit was opened.
-
property
-
class
CircuitAioRedisStorage
(state, redis_object, namespace=None, fallback_circuit_state='closed')[source]¶ Bases:
pybreaker.CircuitBreakerStorage
Implements a CircuitBreakerStorage using aioredis.
-
BASE_NAMESPACE
= 'infuse'¶
-
logger
= <Logger sanic.error (WARNING)>¶
-
async classmethod
initialize
(state, redis_object, namespace=None, fallback_circuit_state='closed')[source]¶ - Parameters
state (str) –
namespace (str) –
fallback_circuit_state (str) –
-
property
state
¶ Returns the current circuit breaker state.
-
async
set_state
(state)[source]¶ Set the current circuit breaker state to state. A separate method needed to be created setting with asyncio is not possible.
- Parameters
state (str) –
- Return type
None
-
property
counter
¶ Returns the current value of the failure counter.
-
property
opened_at
¶ Returns a datetime object of the most recent value of when the circuit was opened.
-
infuse.config
¶
-
INFUSE_CACHES
: Dict[str, dict] = {'infuse': {'DATABASE': 15, 'HOST': 'localhost', 'PORT': 6379}}¶ The cache for where the states will be saved.
-
INFUSE_BREAKER_RESET_TIMEOUT
: int = 15¶ The reset timeout in seconds to retry.
-
INFUSE_BREAKER_MAX_FAILURE
: int = 5¶ The number of consecutive failures to another application before the circuit breaker trips.
-
INFUSE_INITIAL_CIRCUIT_STATE
: str = 'closed'¶ The initial state when a new instance of this application is launched.
-
INFUSE_FALLBACK_CIRCUIT_STATE
: str = 'closed'¶ The fallback state when state is unable to be retrieved from storage.
-
INFUSE_BREAKER_LISTENERS
: List[str] = []¶ The paths of the listeners you would like to initialize the breaker with.
-
INFUSE_BREAKER_EXCLUDE_EXCEPTIONS
: List[str] = ['insanic.services.adapters.HTTPStatusError']¶ The paths of the exceptions to exclude from opening the circuit.
-
INFUSE_REDIS_KEY_NAMESPACE_TEMPLATE
: str = '{env}:{service_name}'¶ The default redis key template.
infuse.patch
¶
-
patch.
patch
()¶ This patches the
Service._dispatch_future
andService._dispatch_send
methods.The
dispatch_future
method is patched to extract theskip_breaker
keyword argument because current insanic doesn’t pass thekwargs
to_dispatch_send
. Maybe if this is fixed in Insanic, this can be removed”- Return type
None
-
class
RequestBreaker
[source]¶ -
-
async
breaker
(target_service)[source]¶ Returns the circuit breaker object for the respective service.
- Parameters
target_service (insanic.services.client.Service) –
- Return type
-
static
namespace
(service_name)[source]¶ A helper method to return the namespace for the service.
- Parameters
service_name (str) –
- Return type
str
-
extract_skip_breaker
(wrapped, instance, args, kwargs)[source]¶ This wraps
Service._dispatch_future
to extract theskip_breaker
keyword argument. So when_dispatch_send
gets called, it can determine if circuit breaking should be bypassed.
-
async
wrapped_request
(wrapped, instance, args, kwargs)[source]¶ The wrapper method for
_dispatch_send
. This will pass the call to the breaker where the breaker can short circuit the call if the target service is current not available. The breaker will not open forHTTPStatusError
s.- Parameters
wrapped – The wrapped function which in turns needs to be called by your wrapper function.
instance – The object to which the wrapped function was bound when it was called.
args – The list of positional arguments supplied when the decorated function was called.
kwargs – The dictionary of keyword arguments supplied when the decorated function was called.
-
async