Sha256: 2c6301ba72c4d4c718e728ea11879e0b41b9697661b7dd9a66cb2a4662315360

Contents?: true

Size: 2 KB

Versions: 4

Compression:

Stored size: 2 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 { |c| c.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
      yield Session.new(conn)
    ensure
      @pool.checkin if conn
    end

    # Run commands on a connection from pool. Connection is wrapped to
    # the {Pipeline}. {Pipeline#sync} operation will be executed automatically
    # at the end of a block.
    # @yield [pipeline] provide {Pipeline} to a block
    # @yieldreturn [Array] responses from all executed operations
    #
    # @example
    #  pool = Oxblood::Pool.new(size: 8)
    #  pool.pipelined do |pipeline|
    #    pipeline.set('hello', 'world')
    #    pipeline.get('hello')
    #  end # => ['OK', 'world']
    def pipelined
      conn = @pool.checkout
      pipeline = Pipeline.new(conn)
      yield pipeline
      pipeline.sync
    ensure
      @pool.checkin if conn
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
oxblood-0.1.0.dev10 lib/oxblood/pool.rb
oxblood-0.1.0.dev9 lib/oxblood/pool.rb
oxblood-0.1.0.dev8 lib/oxblood/pool.rb
oxblood-0.1.0.dev7 lib/oxblood/pool.rb