lib/basquiat/adapters/rabbitmq/connection.rb in basquiat-1.4.0 vs lib/basquiat/adapters/rabbitmq/connection.rb in basquiat-1.5.0
- old
+ new
@@ -5,13 +5,16 @@
class RabbitMq
# Control the connection to the RabitMQ server. Delegates calls to {Bunny::Connection}
class Connection < SimpleDelegator
# @param hosts: [Array<String>] IPs or FQDN of the RabbitMQ instances
# @param port [Fixnum] Port that the RabbitMQ instances run
+ # @option failover: [Fixnum|Symbol] :heartbeat (:server) Heartbeat timeout to offer to the server
# @option failover: [Fixnum] :max_retries (5) Maximum number of reconnection retries
# @option failover: [Fixnum] :default_timeout (5) Interval between to reconnect attempts
# @option failover: [Fixnum] :connection_timeout (5) Allowed time before a connection attempt timeouts
+ # @option failover: [Fixnum] :read_timeout (30) TCP socket read timeout in seconds
+ # @option failover: [Fixnum] :write_timeout (30) TCP socket write timeout in seconds
# @option auth: [String] :user ('guest')
# @option auth: [String] :password ('guest')
def initialize(hosts:, port: 5672, failover: {}, auth: {})
@hosts = hosts
@port = port
@@ -20,11 +23,11 @@
end
# Creates a channel
# @return [Bunny::Channel]
def create_channel
- connection.start
+ connection.start unless connected?
Basquiat.logger.debug 'Creating a new channel'
connection.create_channel
end
# Starts the connection if needed
@@ -51,17 +54,20 @@
@connection = nil
__setobj__(nil)
end
def connection
- @connection ||= Bunny.new(
+ @connection ||= Basquiat.configuration.connection || Bunny.new(
hosts: @hosts,
port: @port,
username: @auth.fetch(:user, 'guest'),
password: @auth.fetch(:password, 'guest'),
+ heartbeat: @failover.fetch(:heartbeat, :server),
recovery_attempts: @failover.fetch(:max_retries, 5),
network_recovery_interval: @failover.fetch(:default_timeout, 5),
connection_timeout: @failover.fetch(:connection_timeout, 5),
+ read_timeout: @failover.fetch(:read_timeout, 30),
+ write_timeout: @failover.fetch(:write_timeout, 30),
logger: Basquiat.logger
)
__setobj__(@connection)
end
end