class Bundler::ConnectionPool
def self.wrap(options, &block)
def self.wrap(options, &block) Wrapper.new(options, &block) end
def available
def available @available.length end
def checkin
def checkin if ::Thread.current[@key] if ::Thread.current[@key_count] == 1 @available.push(::Thread.current[@key]) ::Thread.current[@key] = nil ::Thread.current[@key_count] = nil else ::Thread.current[@key_count] -= 1 end else raise Bundler::ConnectionPool::Error, "no connections are checked out" end nil end
def checkout(options = {})
def checkout(options = {}) if ::Thread.current[@key] ::Thread.current[@key_count] += 1 ::Thread.current[@key] else ::Thread.current[@key_count] = 1 ::Thread.current[@key] = @available.pop(options[:timeout] || @timeout) end end
def initialize(options = {}, &block)
def initialize(options = {}, &block) raise ArgumentError, "Connection pool requires a block" unless block options = DEFAULTS.merge(options) @size = Integer(options.fetch(:size)) @timeout = options.fetch(:timeout) @available = TimedStack.new(@size, &block) @key = :"pool-#{@available.object_id}" @key_count = :"pool-#{@available.object_id}-count" end
def reload(&block)
def reload(&block) @available.shutdown(reload: true, &block) end
def shutdown(&block)
def shutdown(&block) @available.shutdown(&block) end
def with(options = {})
def with(options = {}) Thread.handle_interrupt(Exception => :never) do conn = checkout(options) begin Thread.handle_interrupt(Exception => :immediate) do yield conn end ensure checkin end end end