Brama.ConnectionManager (Brama v1.1.0)

View Source

Manages connection states and implements circuit breaking logic.

This module is responsible for:

  • Tracking connection states (closed, open, half-open)
  • Implementing circuit breaking logic
  • Managing state transitions
  • Cleaning up expired connections

Summary

Functions

Checks if a connection is available (circuit is closed or half-open).

Returns a specification to start this module under a supervisor.

Manually closes the circuit for a connection.

Updates the configuration for a specific connection or globally.

Reports a failed connection attempt.

Manually opens the circuit for a connection.

Registers a new connection.

Resets the circuit for a connection (closes it and resets failure count).

Starts the connection manager.

Gets the current status of a connection.

Reports a successful connection attempt.

Unregisters a connection.

Types

connection_data()

@type connection_data() :: %{
  identifier: connection_id(),
  scope: connection_scope(),
  state: connection_state(),
  failure_count: non_neg_integer(),
  max_attempts: pos_integer() | nil,
  expiry: non_neg_integer() | nil,
  opened_at: non_neg_integer() | nil,
  last_success_time: non_neg_integer() | nil,
  last_failure_time: non_neg_integer() | nil,
  metadata: map(),
  expiry_strategy: :fixed | :progressive,
  initial_expiry: non_neg_integer(),
  max_expiry: non_neg_integer(),
  backoff_factor: float()
}

connection_id()

@type connection_id() :: String.t()

connection_scope()

@type connection_scope() :: String.t() | nil

connection_state()

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

Functions

available?(identifier, opts \\ [])

@spec available?(
  String.t(),
  keyword()
) :: boolean()

Checks if a connection is available (circuit is closed or half-open).

Options

  • :scope - Optional scope to match

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

close_circuit!(identifier, opts \\ [])

@spec close_circuit!(
  String.t(),
  keyword()
) :: :ok | {:error, term()}

Manually closes the circuit for a connection.

Options

  • :scope - Optional scope to match

configure(identifier \\ nil, opts)

@spec configure(
  String.t() | nil,
  keyword()
) :: :ok | {:error, term()}

Updates the configuration for a specific connection or globally.

Parameters

  • identifier - Optional connection identifier. If not provided, updates global settings
  • opts - Configuration options to update

failure(identifier, opts \\ [])

@spec failure(
  String.t(),
  keyword()
) :: :ok | {:error, term()}

Reports a failed connection attempt.

Options

  • :scope - Optional scope to match
  • :reason - Reason for the failure
  • :metadata - Additional metadata about the failure

open_circuit!(identifier, opts \\ [])

@spec open_circuit!(
  String.t(),
  keyword()
) :: :ok | {:error, term()}

Manually opens the circuit for a connection.

Options

  • :scope - Optional scope to match
  • :reason - Reason for opening the circuit
  • :expiry - Custom expiry time in milliseconds

register(identifier, opts \\ [])

@spec register(
  String.t(),
  keyword()
) :: {:ok, map()} | {:error, term()}

Registers a new connection.

Options

  • :scope - Optional scope for grouping connections
  • :max_attempts - Maximum number of failures before opening circuit
  • :expiry - Time in milliseconds after which an open circuit transitions to half-open
  • :metadata - Additional metadata to store with the connection

reset_circuit!(identifier, opts \\ [])

@spec reset_circuit!(
  String.t(),
  keyword()
) :: :ok | {:error, term()}

Resets the circuit for a connection (closes it and resets failure count).

Options

  • :scope - Optional scope to match

start_link(opts \\ [])

@spec start_link(keyword()) :: GenServer.on_start()

Starts the connection manager.

status(identifier, opts \\ [])

@spec status(
  String.t(),
  keyword()
) :: {:ok, map()} | {:error, term()}

Gets the current status of a connection.

Options

  • :scope - Optional scope to match

success(identifier, opts \\ [])

@spec success(
  String.t(),
  keyword()
) :: :ok | {:error, term()}

Reports a successful connection attempt.

Options

  • :scope - Optional scope to match
  • :metadata - Additional metadata about the success

unregister(identifier, opts \\ [])

@spec unregister(
  String.t(),
  keyword()
) :: :ok | {:error, term()}

Unregisters a connection.

Options

  • :scope - Optional scope to match