Class: Noiseless::ConnectionManager

Inherits:
Object
  • Object
show all
Defined in:
lib/noiseless/connection_manager.rb

Instance Method Summary collapse

Constructor Details

#initialize ⇒ ConnectionManager

Returns a new instance of ConnectionManager.



5
6
7
8
# File 'lib/noiseless/connection_manager.rb', line 5

def initialize
  @clients = {}
  @configs = {}
end

Instance Method Details

#client(name = :primary) ⇒ Object

Retrieve a client; defaults to :primary



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/noiseless/connection_manager.rb', line 17

def client(name = :primary)
  name = name.to_sym

  # Lazy-load the adapter only when actually used
  @clients[name] ||= begin
    config = @configs.fetch(name) { raise "Unknown connection: #{name}" }
    params = { hosts: config[:hosts] }
    params[:timeout] = config[:timeout] unless config[:timeout].nil?
    params[:request_timeout] = config[:request_timeout] unless config[:request_timeout] == :default
    Adapters.lookup(config[:adapter], **params)
  end
end

#register(name, adapter:, hosts:, timeout: nil, request_timeout: :default) ⇒ Object

Register a named client statically from YAML (boot-time only). request_timeout: :default uses the adapter default, nil disables the deadline.



12
13
14
# File 'lib/noiseless/connection_manager.rb', line 12

def register(name, adapter:, hosts:, timeout: nil, request_timeout: :default)
  @configs[name.to_sym] = { adapter: adapter, hosts: hosts, timeout: timeout, request_timeout: request_timeout }
end