class Pod4::ConnectionPool

Constants

DEFAULT_MAX_CLIENTS
PoolItem

Attributes

max_clients[R]
max_wait[R]

Public Class Methods

new(args) click to toggle source

As Connection, but with some options you can set.

  • max_clients – if this many clients are assigned to threads, wait until one is freed.

pass nil for no maximum. Tries to default to something sensible.

Pass nil to wait forever. Default is nil, because you would need to handle that timeout.

Calls superclass method Pod4::Connection::new
# File lib/pod4/connection_pool.rb, line 73
def initialize(args)
  super(args)

  @max_clients = args[:max_clients] || DEFAULT_MAX_CLIENTS
  @max_wait    = args[:max_wait]
  @pool        = Pool.new
end

Public Instance Methods

_pool() click to toggle source

Dump the internal pool (for test purposes only)

# File lib/pod4/connection_pool.rb, line 146
def _pool
  @pool._dump
end
client(interface) click to toggle source

Return a client for the interface to use.

Return the client we gave this thread before. Failing that, assign a free one from the pool. Failing that, ask the interface to give us a new client.

Note: The interface passes itself in case we want to call it back to get a new client; but clients are assigned to a thread. Every interface in a given thread gets the same pool item, the same client object.

# File lib/pod4/connection_pool.rb, line 92
def client(interface)
  time = Time.now
  cl   = nil

  # NB: We are constrained to use loop in order for our test to work
  loop do
    if (pi = @pool.get_current)
      cl = pi.client
      break
    end

    if (pi = @pool.get_free)
      cl = pi.client
      break
    end

    if @max_clients && @pool.size >= @max_clients 
      raise Pod4::PoolTimeout if @max_wait && (Time.now - time > @max_wait)
      sleep 1
      next
    end

    cl = interface.new_connection(@data_layer_options)
    @pool << cl
    break
  end # of loop

  cl
end
close(interface) click to toggle source

De-assign the client for the current thread from that thread.

We never ask the interface to close the connection to the database. There is no advantage in doing that for us.

Note: The interface passes itself in case we want to call it back to actually close the client; but clients are assigned to a thread.

# File lib/pod4/connection_pool.rb, line 131
def close(interface)
  @pool.release
end
drop(interface) click to toggle source

Remove the client object entirely – for example, because the connection to the database has expired.

# File lib/pod4/connection_pool.rb, line 139
def drop(interface)
  @pool.drop
end