RabbitMQPoolEx.Worker.RabbitMQConnection (RabbitMQ Pool Ex v1.2.0)

View Source

Worker module for managing RabbitMQ connections and channel pooling using poolboy.

This module establishes and maintains a connection to RabbitMQ, manages a pool of channels, and handles reconnections and channel restarts in case of failures. It ensures that channels are efficiently reused and that clients can safely checkout and checkin channels.

Configuration Options

  • :reuse_channels? - When set to true, channels are reused when checked back into the pool. Defaults to false.
  • :channels - Number of channels to create in the pool. Defaults to 10.
  • :reconnect_interval - Interval in milliseconds to wait before attempting to reconnect. Defaults to 1000ms.

Summary

Functions

Checks a channel back into the pool.

Checks out a channel from the pool.

Returns a specification to start this module under a supervisor.

Creates a new channel outside of the pool.

Retrieves the current RabbitMQ connection.

Initializes the worker process by trapping exits, ensuring that all linked connections and multiplexed channels can be restarted by the worker.

Starts the RabbitMQ connection worker.

Functions

checkin_channel(pid, channel)

@spec checkin_channel(pid(), AMQP.Channel.t()) :: :ok

Checks a channel back into the pool.

It takes care of reintegrating the channel into the state, ensuring the channel is properly monitored, and that the connection status is properly managed.

The function behaves differently based on whether the channel reuse feature is enabled (reuse_channels?: true) in the worker state.

Parameters:

  • pid - The PID of the worker process.
  • channel - The channel to check back in.

Behavior:

  • When reuse_channels?: true:

    • If the channel's pid already exists in the pool (i.e., in the list of channels), it will simply remove the monitor for that pid and return the updated state.
    • If the pid is not already in the pool, it will add the channel to the list of channels and remove the monitor for that pid and return the updated state
  • When reuse_channels? is not enabled:

    • If the channel's pid is found in either the list of channels or monitors, it will attempt to remove the channel from the pool and handle potential errors in case the channel has been closed.
    • If the channel's connection is still valid, it will replace the channel with a new one (close the current channel and open a new one).
    • If the connection is closed ({:error, :closing}), the state will simply be updated without re-adding the channel.

checkout_channel(pid)

@spec checkout_channel(pid()) ::
  {:ok, AMQP.Channel.t()} | {:error, :disconnected} | {:error, :out_of_channels}

Checks out a channel from the pool.

It removes the channel from the available channels list and monitors the caller process (from_pid) to ensure proper cleanup in case of failure.

Parameters:

  • from_pid (pid): The process identifier of the client (caller) that is requesting the channel.

Behavior:

  • If there are available channels in the pool (channels list is non-empty), the function will:

    • Select the first channel (channel), remove it from the list of available channels (channels: rest), and add the caller process (from_pid) as a monitor for the channel.
    • The monitor reference is added to the monitors map, linking the pid of the channel to the new monitor reference.
    • The function returns a tuple containing the checked-out channel and the updated state with the modified list of channels and the updated monitor map.
  • If no channels are available, the function will raise an error or behave as defined in other parts of the system, though this behavior is not detailed here.

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

create_channel(pid)

@spec create_channel(pid()) :: {:ok, AMQP.Channel.t()} | {:error, any()}

Creates a new channel outside of the pool.

This allows clients to manually create channels without automatic management or pooling.

Parameters

  • pid - The PID of the worker process.

get_connection(pid)

@spec get_connection(pid()) :: {:ok, AMQP.Connection.t()} | {:error, :disconnected}

Retrieves the current RabbitMQ connection.

Parameters

  • pid - The PID of the worker process.

init(config)

Initializes the worker process by trapping exits, ensuring that all linked connections and multiplexed channels can be restarted by the worker.

This function also triggers an asynchronous connection attempt, ensuring that any future calls will wait until the connection is successfully established.

Parameters

  • amqp_config (keyword or string): RabbitMQ configuration settings used to establish the connection.

start_link(config)

Starts the RabbitMQ connection worker.

Parameters

  • config - Configuration options (keyword list or string).