class Oxblood::Pool

Create connection pool. For the most use cases this is entrypoint API.

@example

pool = Oxblood::Pool.new(size: 8)
pool.with { |s| s.ping } # => 'PONG'

Public Class Methods

new(options = {}) click to toggle source

Initialize connection pool

@param [Hash] options Connection options

@option options [Float] :timeout (1.0) Connection acquisition timeout. @option options [Integer] :size Pool size. @option options [Hash] :connection see {Connection#initialize}

# File lib/oxblood/pool.rb, line 20
def initialize(options = {})
  timeout = options.fetch(:timeout, 1.0)
  size = options.fetch(:size)

  @pool = ConnectionPool.new(size: size, timeout: timeout) do
    Connection.new(options.fetch(:connection, {}))
  end
end

Public Instance Methods

with() { |session| ... } click to toggle source

Run commands on a connection from pool. Connection is wrapped to the {Session}. @yield [session] provide {Session} to a block @yieldreturn response from the last executed operation

@example

pool = Oxblood::Pool.new(size: 8)
pool.with do |session|
  session.set('hello', 'world')
  session.get('hello')
end # => 'world'
# File lib/oxblood/pool.rb, line 40
def with
  conn = @pool.checkout
  session = Session.new(conn)
  yield(session)
ensure
  if conn
    session.discard if conn.in_transaction?
    @pool.checkin
  end
end