class FiberConnectionPool

Public Class Methods

new(opts, &block) click to toggle source
Calls superclass method ConnectionPool::new
# File lib/thrift_client/pool/fiber_connection_pool.rb, line 6
    def initialize(opts, &block)
super
    end

Public Instance Methods

destroy() click to toggle source

Destroy all connections in this pool. It will waiting until all connections are idle and than close them one by one.

# File lib/thrift_client/pool/fiber_connection_pool.rb, line 25
    def destroy
while @idle.size < @size
    fiber = Fiber.current
    @pending.push fiber
    Fiber.yield
end
@idle.each do | conn |
  begin
    conn.disconnect
  rescue Exception => e
    puts "close connection error! #{e.backtrace}"
  end
end
    end
execute() { |conn| ... } click to toggle source

Choose first idle connection and pass it to the supplied block. This will block indefinitely until there is an idle connection to service the request.

# File lib/thrift_client/pool/fiber_connection_pool.rb, line 13
def execute
        f = Fiber.current
        begin
          conn = acquire(f)
          yield conn
        ensure
          release(f)
        end
end

Private Instance Methods

acquire(fiber) click to toggle source

Acquire a lock on a connection and assign it to executing fiber

  • if connection is idle, pass it back to the calling block

  • if pool is full, yield the current fiber until connection is idle

# File lib/thrift_client/pool/fiber_connection_pool.rb, line 45
def acquire(fiber)
  if conn = @idle.pop
    @active[fiber.object_id] = conn
    conn
  else
       @pending.push fiber
    Fiber.yield
    acquire(fiber)
  end
end
release(fiber) click to toggle source

Release connection assigned to the supplied fiber and resume any other pending connections (which will immediately try to run acquire on the pool)

# File lib/thrift_client/pool/fiber_connection_pool.rb, line 59
def release(fiber)
  @idle.push(@active.delete(fiber.object_id))

  if pending = @pending.shift
    pending.resume
  end
end