class Bundler::ConnectionPool::TimedStack

示例

ts = TimedStack.new(1) { MyConnection.new }

# fetch a connection
conn = ts.pop

# return a connection
ts.push conn

conn = ts.pop
ts.pop timeout: 5
#=> raises Bundler::ConnectionPool::TimeoutError after 5 seconds

属性

max[R]

公共类方法

new(size = 0, &block) 点击以切换源码

创建一个新的连接池,大小为size,连接通过给定的block创建。

# File bundler/vendor/connection_pool/lib/connection_pool/timed_stack.rb, line 27
def initialize(size = 0, &block)
  @create_block = block
  @created = 0
  @que = []
  @max = size
  @mutex = Thread::Mutex.new
  @resource = Thread::ConditionVariable.new
  @shutdown_block = nil
end

公共实例方法

<<(obj, options = {})
别名为: push
empty?() 点击以切换源码

如果没有任何可用的连接,则返回true

# File bundler/vendor/connection_pool/lib/connection_pool/timed_stack.rb, line 104
def empty?
  (@created - @que.length) >= @max
end
length() 点击以切换源码

堆栈上可用连接的数量。

# File bundler/vendor/connection_pool/lib/connection_pool/timed_stack.rb, line 111
def length
  @max - @created + @que.length
end
pop(timeout = 0.5, options = {}) 点击以切换源码

从堆栈中检索一个连接。如果有可用的连接,则立即返回。如果在给定的超时时间内没有可用连接,则会引发Bundler::ConnectionPool::TimeoutError异常。

:timeoutoptions中唯一检查的条目,并且优先于timeout参数(将在未来的版本中删除)。其他选项可以被扩展TimedStack的子类使用。

# File bundler/vendor/connection_pool/lib/connection_pool/timed_stack.rb, line 63
def pop(timeout = 0.5, options = {})
  options, timeout = timeout, 0.5 if Hash === timeout
  timeout = options.fetch :timeout, timeout

  deadline = current_time + timeout
  @mutex.synchronize do
    loop do
      raise Bundler::ConnectionPool::PoolShuttingDownError if @shutdown_block
      return fetch_connection(options) if connection_stored?(options)

      connection = try_create(options)
      return connection if connection

      to_wait = deadline - current_time
      raise Bundler::ConnectionPool::TimeoutError, "Waited #{timeout} sec, #{length}/#{@max} available" if to_wait <= 0
      @resource.wait(@mutex, to_wait)
    end
  end
end
push(obj, options = {}) 点击以切换源码

obj返回到堆栈。optionsTimedStack中被忽略,但可以被扩展TimedStack的子类使用。

# File bundler/vendor/connection_pool/lib/connection_pool/timed_stack.rb, line 41
def push(obj, options = {})
  @mutex.synchronize do
    if @shutdown_block
      @shutdown_block.call(obj)
    else
      store_connection obj, options
    end

    @resource.broadcast
  end
end
也别名为:<<
shutdown(reload: false, &block) 点击以切换源码

通过将每个连接传递给block,然后将其从连接池中删除来关闭TimedStack。尝试在关闭后检出连接将引发Bundler::ConnectionPool::PoolShuttingDownError,除非:reloadtrue

# File bundler/vendor/connection_pool/lib/connection_pool/timed_stack.rb, line 89
def shutdown(reload: false, &block)
  raise ArgumentError, "shutdown must receive a block" unless block

  @mutex.synchronize do
    @shutdown_block = block
    @resource.broadcast

    shutdown_connections
    @shutdown_block = nil if reload
  end
end

私有实例方法

connection_stored?(options = nil) 点击以切换源码

这是TimedStack的扩展点,并使用互斥锁调用。

如果堆栈上有可用的连接,此方法必须返回true。

# File bundler/vendor/connection_pool/lib/connection_pool/timed_stack.rb, line 126
def connection_stored?(options = nil)
  !@que.empty?
end
current_time() 点击以切换源码
# File bundler/vendor/connection_pool/lib/connection_pool/timed_stack.rb, line 117
def current_time
  Process.clock_gettime(Process::CLOCK_MONOTONIC)
end
fetch_connection(options = nil) 点击以切换源码

这是TimedStack的扩展点,并使用互斥锁调用。

此方法必须从堆栈中返回一个连接。

# File bundler/vendor/connection_pool/lib/connection_pool/timed_stack.rb, line 135
def fetch_connection(options = nil)
  @que.pop
end
shutdown_connections(options = nil) 点击以切换源码

这是TimedStack的扩展点,并使用互斥锁调用。

此方法必须关闭堆栈上的所有连接。

# File bundler/vendor/connection_pool/lib/connection_pool/timed_stack.rb, line 144
def shutdown_connections(options = nil)
  while connection_stored?(options)
    conn = fetch_connection(options)
    @shutdown_block.call(conn)
  end
  @created = 0
end
store_connection(obj, options = nil) 点击以切换源码

这是TimedStack的扩展点,并使用互斥锁调用。

此方法必须将obj返回到堆栈。

# File bundler/vendor/connection_pool/lib/connection_pool/timed_stack.rb, line 157
def store_connection(obj, options = nil)
  @que.push obj
end
try_create(options = nil) 点击以切换源码

这是TimedStack的扩展点,并使用互斥锁调用。

当且仅当允许的总连接数尚未达到时,此方法必须创建一个连接。

# File bundler/vendor/connection_pool/lib/connection_pool/timed_stack.rb, line 167
def try_create(options = nil)
  unless @created == @max
    object = @create_block.call
    @created += 1
    object
  end
end