Sha256: 5aceadd0ec1652f0bb11c1a22dad46fd6bb333bba1ff49857988f29d913c21b9

Contents?: true

Size: 1.43 KB

Versions: 1

Compression:

Stored size: 1.43 KB

Contents

require 'connection_pool'
require 'oxblood/session'
require 'oxblood/pipeline'
require 'oxblood/connection'

module Oxblood
  # 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'
  class Pool
    # 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}
    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

    # 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'
    def with
      conn = @pool.checkout
      session = Session.new(conn)
      yield(session)
    ensure
      if conn
        session.discard if conn.in_transaction?
        @pool.checkin
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
oxblood-0.3.0 lib/oxblood/pool.rb