Raxol.Core.ErrorRecovery (Raxol v2.0.1)

View Source

Error recovery strategies for the Raxol application.

Provides various recovery mechanisms for different types of errors, including circuit breakers, fallback mechanisms, and graceful degradation.

REFACTORED: All try/catch/rescue blocks replaced with functional patterns.

Features

  • Circuit breaker pattern for external services
  • Fallback strategies
  • Graceful degradation
  • Resource cleanup on errors
  • State recovery mechanisms

Summary

Functions

Returns a specification to start this module under a supervisor.

Initializes a circuit breaker with optional configuration.

Resets a circuit breaker to its initial state.

Gets the current state of a circuit breaker.

Implements graceful degradation for feature availability.

Implements bulkhead pattern to isolate failures.

Executes a function with circuit breaker protection.

Ensures cleanup is performed even on error.

Provides fallback mechanism for failed operations.

Implements exponential backoff retry strategy.

Types

circuit_state()

@type circuit_state() :: :closed | :open | :half_open

recovery_strategy()

@type recovery_strategy() ::
  :retry | :fallback | :circuit_breaker | :degrade | :cleanup

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

circuit_breaker_init(opts \\ [])

Initializes a circuit breaker with optional configuration.

circuit_breaker_reset(circuit_breaker)

Resets a circuit breaker to its initial state.

circuit_breaker_state(circuit_breaker)

Gets the current state of a circuit breaker.

degrade_gracefully(feature, full_fn, degraded_fn)

Implements graceful degradation for feature availability.

This is now a regular function instead of a macro to avoid try/rescue.

Examples

degrade_gracefully(:advanced_search, 
  fn -> AdvancedSearch.execute(query) end,
  fn -> BasicSearch.execute(query) end
)

feature_available?(feature)

handle_manager_cast(msg, state)

Callback implementation for Raxol.Core.Behaviours.BaseManager.handle_manager_cast/2.

handle_manager_info(msg, state)

Callback implementation for Raxol.Core.Behaviours.BaseManager.handle_manager_info/2.

mark_feature_degraded(feature, error)

start_link(init_opts \\ [])

with_bulkhead(pool_name, fun, opts \\ [])

Implements bulkhead pattern to isolate failures.

with_circuit_breaker(circuit_name, fun, opts \\ [])

Executes a function with circuit breaker protection.

Examples

with_circuit_breaker(:external_api, fn ->
  ExternalAPI.call()
end)

with_cleanup(fun, cleanup_fun)

Ensures cleanup is performed even on error.

Examples

with_cleanup fn ->
  resource = acquire_resource()
  process(resource)
end, fn resource ->
  release_resource(resource)
end

with_fallback(primary_fun, fallback_fun)

Provides fallback mechanism for failed operations.

Examples

with_fallback fn ->
  fetch_from_primary()
end, fn ->
  fetch_from_cache()
end

with_retry(fun, opts \\ [])

Implements exponential backoff retry strategy.

Options

  • :max_retries - Maximum number of retry attempts (default: 3)
  • :base_delay - Base delay in milliseconds (default: 100)
  • :max_delay - Maximum delay in milliseconds (default: 5000)
  • :jitter - Add randomness to delays (default: true)