class ConnectionManager

Constants

TIMEOUT_ARITY
VERSION

Attributes

connection_timeout[R]
connections[R]
manager_timeout[R]
mutex[R]

Public Class Methods

new(**options) click to toggle source
# File lib/connection-manager.rb, line 10
def initialize(**options)
  @connection_timeout = options.fetch(:timeout, 0)
  @manager_timeout = options.fetch(:manager_timeout, 0)
  @connections = {}
  @mutex = Mutex.new
end

Public Instance Methods

clear() click to toggle source
# File lib/connection-manager.rb, line 17
def clear
  execute do
    connections.delete_if do |_, wrapper|
      wrapper.synchronize do
        wrapper.closed?
      end
    end
  end
  true
end
close(key) click to toggle source
# File lib/connection-manager.rb, line 28
def close(key)
  wrapper = execute do
    connections[key.to_sym]
  end
  wrapper.synchronize do
    wrapper.close
  end if wrapper
end
closed?(key) click to toggle source
# File lib/connection-manager.rb, line 37
def closed?(key)
  wrapper = execute do
    connections[key.to_sym]
  end
  wrapper.synchronize do
    wrapper.closed?
  end if wrapper
end
delete(key) click to toggle source
# File lib/connection-manager.rb, line 46
def delete(key)
  execute do
    wrapper = connections[key.to_sym]
    wrapper.synchronize do
      connections.delete(key.to_sym)
      true
    end if wrapper
  end
end
delete_if(&block) click to toggle source
# File lib/connection-manager.rb, line 56
def delete_if(&block)
  execute do
    connections.delete_if do |_, wrapper|
      wrapper.synchronize do
        block.call(wrapper.connection, wrapper.metadata)
      end
    end
  end
  true
end
empty?() click to toggle source
# File lib/connection-manager.rb, line 67
def empty?
  size == 0
end
exists?(key) click to toggle source
# File lib/connection-manager.rb, line 71
def exists?(key)
  execute do
    connections.key? key.to_sym
  end
end
metadata(key) click to toggle source
# File lib/connection-manager.rb, line 77
def metadata(key)
  wrapper = execute do
    connections[key.to_sym]
  end
  wrapper.synchronize do
    wrapper.metadata
  end if wrapper
end
open?(key) click to toggle source
# File lib/connection-manager.rb, line 86
def open?(key)
  wrapper = execute do
    connections[key.to_sym]
  end
  wrapper.synchronize do
    !wrapper.closed?
  end if wrapper
end
pop(key) click to toggle source
# File lib/connection-manager.rb, line 95
def pop(key)
  execute do
    wrapper = connections[key.to_sym]
    wrapper.synchronize do
      connections.delete(key.to_sym).connection
    end if wrapper
  end
end
push(key, **options, &block) click to toggle source
# File lib/connection-manager.rb, line 104
def push(key, **options, &block)
  options[:timeout] ||= connection_timeout
  execute do
    previous_connection = connections[key.to_sym]
    executor = if previous_connection
      -> { previous_connection.synchronize { connections[key.to_sym] = Wrapper.new(options, &block) } }
    else
      -> { connections[key.to_sym] = Wrapper.new(options, &block) }
    end
    executor.call
  end
  true
end
reset(key) click to toggle source
# File lib/connection-manager.rb, line 118
def reset(key)
  execute do
    wrapper = connections[key.to_sym]
    wrapper.synchronize do
      wrapper.reset
    end if wrapper
  end
end
shutdown() click to toggle source
# File lib/connection-manager.rb, line 127
def shutdown
  execute do
    connections.values.map do |wrapper|
      Thread.new do
        # Keep compatibility with ruby < 2.4
        Thread.current.report_on_exception = false if Thread.current.respond_to?(:report_on_exception=)
        wrapper.synchronize { wrapper.close }
      end
    end.each(&:join)
  end
  true
end
size() click to toggle source
# File lib/connection-manager.rb, line 140
def size
  execute do
    connections.keys.size
  end
end
with(key, **options, &block) click to toggle source
# File lib/connection-manager.rb, line 146
def with(key, **options, &block)
  wrapper = execute do
    connections[key.to_sym]
  end
  raise Connection::ClosedError if wrapper && wrapper.closed?

  wrapper.synchronize(options) do
    block.call(wrapper.connection, wrapper.metadata)
  end if wrapper
end

Private Instance Methods

execute(&block) click to toggle source
# File lib/connection-manager.rb, line 161
def execute(&block)
  Timeout.timeout(*lock_timeout_args) { mutex.lock }
  block.call.tap do
    mutex.unlock
  end
end
lock_timeout_args() click to toggle source
# File lib/connection-manager.rb, line 168
def lock_timeout_args
  [manager_timeout, LockingError].tap do |args|
    args << "unable to acquire lock on time" if TIMEOUT_ARITY > 2
  end
end