Parent

ConnectionPool::TimedStack

Public Class Methods

new(size = 0) click to toggle source
# File lib/connection_pool/timed_stack.rb, line 7
def initialize(size = 0)
  @que = Array.new(size) { yield }
  @mutex = Mutex.new
  @resource = ConditionVariable.new
  @shutdown_block = nil
end

Public Instance Methods

<<(obj) click to toggle source
Alias for: push
clear() click to toggle source
# File lib/connection_pool/timed_stack.rb, line 58
def clear
  @que.clear
end
empty?() click to toggle source
# File lib/connection_pool/timed_stack.rb, line 54
def empty?
  @que.empty?
end
length() click to toggle source
# File lib/connection_pool/timed_stack.rb, line 62
def length
  @que.length
end
pop(timeout=0.5) click to toggle source
# File lib/connection_pool/timed_stack.rb, line 27
def pop(timeout=0.5)
  deadline = Time.now + timeout
  @mutex.synchronize do
    loop do
      raise ConnectionPool::PoolShuttingDownError if @shutdown_block
      return @que.pop unless @que.empty?
      to_wait = deadline - Time.now
      raise Timeout::Error, "Waited #{timeout} sec" if to_wait <= 0
      @resource.wait(@mutex, to_wait)
    end
  end
end
push(obj) click to toggle source
# File lib/connection_pool/timed_stack.rb, line 14
def push(obj)
  @mutex.synchronize do
    if @shutdown_block
      @shutdown_block.call(obj)
    else
      @que.push obj
    end

    @resource.broadcast
  end
end
Also aliased as: <<
shutdown(&block) click to toggle source
# File lib/connection_pool/timed_stack.rb, line 40
def shutdown(&block)
  raise ArgumentError, "shutdown must receive a block" unless block_given?

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

    @que.size.times do
      conn = @que.pop
      block.call(conn)
    end
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.