Search documentation of macula

# macula_provider_selector (macula v0.44.2)

View Source

Provider selection strategies for multi-provider RPC load balancing.

Supports multiple strategies for choosing which provider to use when multiple providers advertise the same service.

Strategies: - round_robin: Distribute calls evenly across providers - random: Random provider selection - first: Always use first provider (default/simple)

# Summary

## Types

provider_info/0

Provider information returned from DHT.

selection_state/0
strategy/0

Selection strategy for choosing a provider from multiple options.

## Functions

select_provider(Providers, State)

Select a provider from a list using the default strategy (random).

select_provider(Providers, Strategy, State)

Select a provider from a list using a specific strategy.

# Types

# provider_info/0

    -type provider_info() ::
              #{node_id := binary(),
                endpoint := binary(),
                metadata := map(),
                advertised_at => integer(),
                ttl => pos_integer()}.

Provider information returned from DHT.

# selection_state/0

    -type selection_state() :: #{strategy => strategy(), counters => #{binary() => non_neg_integer()}}.

# strategy/0

    -type strategy() :: round_robin | random | first.

Selection strategy for choosing a provider from multiple options.

# Functions

# select_provider(Providers, State)

    -spec select_provider([provider_info()], selection_state()) ->
                             {ok, provider_info(), selection_state()} | {error, no_providers}.

Select a provider from a list using the default strategy (random).

Returns the selected provider or error if list is empty.

# select_provider(Providers, Strategy, State)

    -spec select_provider([provider_info()], strategy(), selection_state()) ->
                             {ok, provider_info(), selection_state()} | {error, no_providers}.

Select a provider from a list using a specific strategy.

Strategies: - first: Always select the first provider (simple, no state) - random: Randomly select a provider - round_robin: Distribute calls evenly using a counter

Examples:

      %% Random selection
      State = new_state(random),
      {ok, Provider, State2} = select_provider(Providers, random, State).

      %% Round-robin selection
      State = new_state(round_robin),
      {ok, P1, State2} = select_provider(Providers, round_robin, State),
      {ok, P2, State3} = select_provider(Providers, round_robin, State2),
      %% P1 and P2 will be different providers (if multiple available)