Sha256: 439d7e92dcd0fded6c86cdb02c39199d23b1254ad740aad61aa0175debdf9b6a

Contents?: true

Size: 1.56 KB

Versions: 13

Compression:

Stored size: 1.56 KB

Contents

# frozen_string_literal: true

require_relative '../../polyphony'
require 'sequel'

module Polyphony

  # Sequel ConnectionPool that delegates to Polyphony::ResourcePool.
  class FiberConnectionPool < Sequel::ConnectionPool

    # Initializes the connection pool.
    #
    # @param db [any] db to connect to
    # @paral opts [Hash] connection pool options
    def initialize(db, opts = OPTS)
      super
      max_size = Integer(opts[:max_connections] || 4)
      @pool = Polyphony::ResourcePool.new(limit: max_size) { make_new(:default) }
    end

    # Holds a connection from the pool, passing it to the given block.
    #
    # @return [any] block's return value
    def hold(_server = nil)
      @pool.acquire do |conn|
        yield conn
      rescue Polyphony::BaseException
        # The connection may be in an unrecoverable state if interrupted,
        # discard the connection from the pool so it isn't reused.
        @pool.discard!
        raise
      end
    end

    # Returns the pool's size.
    #
    # @return [Integer] size of pool
    def size
      @pool.size
    end

    # Returns the pool's maximal size.
    #
    # @return [Integer] maximum pool size
    def max_size
      @pool.limit
    end

    # Fills pool and preconnects all db instances in pool.
    #
    # @return [void]
    def preconnect(_concurrent = false)
      @pool.fill!
    end
  end

  # Override Sequel::Database to use FiberConnectionPool by default.
  Sequel::Database.prepend(Module.new do
    def connection_pool_default_options
      { pool_class: FiberConnectionPool }
    end
  end)
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
polyphony-1.6 lib/polyphony/adapters/sequel.rb
polyphony-1.5 lib/polyphony/adapters/sequel.rb
polyphony-1.4 lib/polyphony/adapters/sequel.rb
polyphony-1.3 lib/polyphony/adapters/sequel.rb
polyphony-1.2.1 lib/polyphony/adapters/sequel.rb
polyphony-1.2 lib/polyphony/adapters/sequel.rb
polyphony-1.1.1 lib/polyphony/adapters/sequel.rb
polyphony-1.1 lib/polyphony/adapters/sequel.rb
polyphony-1.0.2 lib/polyphony/adapters/sequel.rb
polyphony-1.0.1 lib/polyphony/adapters/sequel.rb
polyphony-1.0 lib/polyphony/adapters/sequel.rb
polyphony-0.99.6 lib/polyphony/adapters/sequel.rb
polyphony-0.99.5 lib/polyphony/adapters/sequel.rb