class ConnectionManager::Wrapper
Attributes
close_method[R]
connection[R]
initializer[R]
metadata[R]
mutex[R]
Public Class Methods
new(options = {}, &block)
click to toggle source
# File lib/connection-manager/wrapper.rb, line 9 def initialize(options = {}, &block) @closed = false @close_method = options.fetch(:close_method, :close) @initializer = block @metadata = options.fetch(:metadata, {}) @mutex = Mutex.new @timeout = options.fetch(:timeout, 0) end
Public Instance Methods
close()
click to toggle source
# File lib/connection-manager/wrapper.rb, line 18 def close return true if closed? return false unless connection.respond_to?(close_method) connection.public_send(close_method) @closed = true end
closed?()
click to toggle source
# File lib/connection-manager/wrapper.rb, line 25 def closed? @closed end
reset()
click to toggle source
# File lib/connection-manager/wrapper.rb, line 29 def reset @closed = false @connection = nil true end
synchronize(**options, &block)
click to toggle source
# File lib/connection-manager/wrapper.rb, line 35 def synchronize(**options, &block) @connection ||= initializer.call timeout = options.fetch(:timeout, @timeout) Timeout.timeout(*lock_timeout_args(timeout)) { mutex.lock } block.call.tap { mutex.unlock } end
Private Instance Methods
lock_timeout_args(timeout)
click to toggle source
# File lib/connection-manager/wrapper.rb, line 46 def lock_timeout_args(timeout) [timeout, ConnectionManager::Connection::LockingError].tap do |args| args << "unable to acquire lock on time" if ConnectionManager::TIMEOUT_ARITY > 2 end end