API Reference

infuse.app

class Infuse[source]
classmethod init_app(app)[source]

The initial entrypoint to initialize Infuse.

  1. Loads Infuse specific configurations

  2. Attaches a listener to change state to value defined in INFUSE_INITIAL_STATE.

  3. Patches the Service object that handles circuit state when sending requests to other services.

Parameters

app (insanic.app.Insanic) –

Return type

None

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.

async set_state(state_str)[source]
Parameters

state_str (str) –

Return type

None

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 half_open()[source]

Half-opens the circuit, e.g. lets the following call pass through and opens the circuit if the call fails (or closes the circuit if the call succeeds).

Return type

None

async close()[source]

Closes the circuit, e.g. lets the following calls execute as usual.

Return type

None

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 on_success()[source]

Override this method to be notified when a call to the guarded operation succeeds.

async on_failure(exc)[source]

Override this method to be notified when a call to the guarded operation fails.

Parameters

exc (Exception) –

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.

async on_failure(exc)[source]

Moves the circuit breaker to the “open” state once the failures threshold is reached.

Raises

CircuitBreakerError – If the failure threshold has been reached.

Parameters

exc (Exception) –

Return type

None

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 call(func, *args, **kwargs)[source]

Delegate the call to before_call, if the time out is not elapsed it will throw an exception, otherwise we get the results from the call performed after the state is switch to half-open

Parameters

func (Callable) –

Return type

Any

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.

async on_failure(exc)[source]

Opens the circuit breaker.

Raises

CircuitBreakerError – “Trial call failed, circuit breaker opened”

Parameters

exc (Exception) –

Return type

None

async on_success()[source]

Closes the circuit breaker.

Return type

None

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.

increment_counter()[source]

Override this method to increase the failure counter by one.

reset_counter()[source]

Override this method to set the failure counter to zero.

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.

class CircuitMemoryStorage(state)[source]

Bases: pybreaker.CircuitBreakerStorage

Implements a CircuitBreakerStorage in local memory.

property state

Returns the current circuit breaker state.

increment_counter()[source]

Increases the failure counter by one.

reset_counter()[source]

Sets the failure counter to zero.

property counter

Returns the current value of the failure counter.

property opened_at

Returns the most recent value of when the circuit was opened.

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

async increment_counter()[source]

Increases the failure counter by one.

async reset_counter()[source]

Sets the failure counter to zero.

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.

async set_opened_at(now)[source]

Atomically sets the most recent value of when the circuit was opened to now. Stored in redis as a simple integer of unix epoch time. To avoid timezone issues between different systems, the passed in datetime should be in UTC.

Parameters

now (datetime.datetime) –

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 and Service._dispatch_send methods.

The dispatch_future method is patched to extract the skip_breaker keyword argument because current insanic doesn’t pass the kwargs to _dispatch_send. Maybe if this is fixed in Insanic, this can be removed”

Return type

None

class RequestBreaker[source]
reset()[source]

Resets all instance variables.

async breaker(target_service)[source]

Returns the circuit breaker object for the respective service.

Parameters

target_service (insanic.services.client.Service) –

Return type

infuse.breaker.AioCircuitBreaker

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 the skip_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 for HTTPStatusError 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.