Class: Oxblood::Pool

Inherits:
Object
  • Object
show all
Defined in:
lib/oxblood/pool.rb

Overview

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

Examples:

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

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Pool

Initialize connection pool

Parameters:

  • options (Hash) (defaults to: {})

    Connection options

Options Hash (options):

  • :timeout (Float) — default: 1.0

    Connection acquisition timeout.

  • :size (Integer)

    Pool size.

  • :connection (Hash)


20
21
22
23
24
25
26
27
# 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

Instance Method Details

#with {|session| ... } ⇒ Object

Run commands on a connection from pool. Connection is wrapped to the Session.

Examples:

pool = Oxblood::Pool.new(size: 8)
pool.with do |session|
  session.set('hello', 'world')
  session.get('hello')
end # => 'world'

Yields:

  • (session)

    provide Session to a block

Yield Returns:

  • response from the last executed operation



40
41
42
43
44
45
46
47
48
49
# 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