RabbitMQPoolEx.Worker.RabbitMQConnection (RabbitMQ Pool Ex v1.1.0)
View SourceWorker 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 totrue
, channels are reused when checked back into the pool. Defaults tofalse
.: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
@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 thatpid
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 thatpid
and return the updated state
- If the channel's
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.
- If the channel's
@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 thepid
of the channel to the new monitor reference. - The function returns a tuple containing the checked-out
channel
and the updatedstate
with the modified list of channels and the updated monitor map.
- Select the first channel (
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.
Returns a specification to start this module under a supervisor.
See Supervisor
.
@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.
@spec get_connection(pid()) :: {:ok, AMQP.Connection.t()} | {:error, :disconnected}
Retrieves the current RabbitMQ connection.
Parameters
pid
- The PID of the worker process.
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.
@spec start_link(RabbitMQPoolEx.Worker.State.config()) :: GenServer.on_start()
Starts the RabbitMQ connection worker.
Parameters
config
- Configuration options (keyword list or string).