API

Version 0.4.13.dev0

fault_tolerant()

dace.fault_tolerant(dependency_id=None, reactor=None, circuit_reset_timeout=10, load_balancer=None, http_pool_size=10, fallback=None, timeout=5, retry_attempts=3, max_open_per_netloc=10, retry_on_timeout=False, expected_in=None, statsd=None, use_proxy=False, context_factory=None, connect_timeout=1)

Wraps a function with a fault tolerant version that supports timeouts, retries, load balancing, and failovers.

The wrapped function can optionally take a dace.call.Call argument, which will contain services for the specific call.

The following services will wrap executions of the wrapped function:

  1. Logging of the operation, specifically of any errors
  2. Timeout tracking to ensure the operation doesn’t hang
  3. Rate limiting to ensure the operation doesn’t hammer a particular server
  4. Circuit breaker for making sure multiple errors cause the target service to not be contacted again until the reset timeout
  5. Load balancing function execution to have each operation spread load across multiple targets
  6. Retry handling
  7. Fallback function execution in case of failure
Parameters:
  • dependency_id – The id to use in stats and logs
  • reactor (twisted.internet.reactor.IReactorCore) – The reactor to use, otherwise the default
  • circuit_reset_timeout (int) – How long in seconds to wait until the circuit is reset and tested again
  • load_balancer – A function that returns a (protocol, netloc) pair for load balancing
  • http_pool_size (int) – The number of concurrent HTTP connections to allow
  • fallback – The function to call when the operation fails to generate the return value
  • timeout – The timeout for the entire operation execution
  • retry_attempts (int) – The number of retry attempts to make if the operation fails
  • max_open_per_netloc (int) – The maximum number of concurrent operations per netloc
  • retry_on_timeout (bool) – Whether to retry the request on HTTP timeout or not
  • expected_in (int) – The time the operation is expected to return in. Used to determine a circuit failure.
  • statsd – The statsd service instance to use for stats tracking
  • use_proxy (bool) – Whether to support calls through a proxy or not
  • context_factory (twisted.internet.ssl.ClientContextFactory) – A custom SSL context factory
  • connect_timeout – The connection timeout for the Agent when making connections

HTTP

class dace.http.Http(agent, reactor)[source]
delete(url, **kwargs)[source]

Make a DELETE request.

See treq.request()

get(url, headers=None, **kwargs)[source]

Make a GET request.

See treq.request()

head(url, **kwargs)[source]

Make a HEAD request.

See treq.request()

patch(url, data=None, **kwargs)[source]

Make a PATCH request.

See treq.request()

post(url, data=None, **kwargs)[source]

Make a POST request.

See treq.request()

put(url, data=None, **kwargs)[source]

Make a PUT request.

See treq.request()

request(method, url, **kwargs)[source]

Make an HTTP request.

Parameters:
  • method (str) – HTTP method. Example: 'GET', 'HEAD'. 'PUT', 'POST'.
  • url (str) – http or https URL, which may include query arguments.
  • headers (Headers or None) – Optional HTTP Headers to send with this request.
  • params (dict w/ str or list/tuple of str values, list of 2-tuples, or None.) – Optional parameters to be append as the query string to the URL, any query string parameters in the URL already will be preserved.
  • data (str, file-like, IBodyProducer, or None) – Optional request body.
  • reactor – Optional twisted reactor.
  • persistent (bool) – Use persistent HTTP connections. Default: True
  • allow_redirects (bool) – Follow HTTP redirects. Default: True
  • auth (tuple of (‘username’, ‘password’).) – HTTP Basic Authentication information.
  • timeout (int) – Request timeout seconds. If a response is not received within this timeframe, a connection is aborted with CancelledError.
Return type:

Deferred that fires with an IResponse provider.

Call

class dace.call.Call(reactor, dependency, run, fallback, timeout, retry_attempts, max_open_per_netloc, retry_on_timeout, expected_in, statsd, load_balancer, circuit_reset_timeout, use_proxy, context_factory, connect_timeout=1)[source]
agent[source]

The Http agent to use for lower-level HTTP services

Return type:twisted.web.client.Agent
http[source]

The HTTP service

Return type:dace.http.Http
netloc[source]

The load-balanced host to use for any calls

Error

exception dace.error.DaceCircuitOpenError[source]

The circuit breaker is open.

exception dace.error.DaceRateLimitedError[source]

The operation has hit its rate limit

exception dace.error.DaceRetryExhausedError[source]

The operation has exceeded its number of retries

exception dace.error.DaceRetryRequestedError[source]

The operation has raised an error, but would like to retry

exception dace.error.DaceTimeoutError(message='Timeout calling url', timeout=None)[source]

The operation has timed out