Sha256: 1e8f5f5f040a92002e6ef6951e94be4d7031ca154bd1cad14b1e2b26022abed4

Contents?: true

Size: 1.83 KB

Versions: 14

Compression:

Stored size: 1.83 KB

Contents

# frozen_string_literal: true

require "async"
require "async/notification"

class Sequel::FiberedConnectionPool < Sequel::ConnectionPool
  def initialize(db, opts = Sequel::OPTS)
    super(db, opts)

    @max_connections = opts[:max_connections]
    @available_connections = []
    @notification = Async::Notification.new
    @size = 0
  end

  def hold(*)
    connection = wait_for_connection
    return connection unless block_given?

    begin
      yield connection
    rescue Sequel::DatabaseDisconnectError, *@error_classes => error
      if disconnect_error?(error)
        disconnect_connection(connection)
        connection = nil
        @size -= 1
      end
      raise
    ensure
      if connection
        @available_connections.push(connection)
        @notification.signal if Async::Task.current?
      end
    end
  end

  def disconnect(*)
    @available_connections.each(&:close)
    @available_connections.clear

    @size = 0
  end

  def size
    @size
  end

  private

  def wait_for_connection
    until (connection = find_or_create_connection)
      @notification.wait
    end

    connection
  end

  def find_or_create_connection
    if (connection = @available_connections.shift)
      return connection
    end

    if @max_connections.nil? || @size < @max_connections
      connection = make_new(:default)
      @size += 1

      return connection
    end

    nil
  end
end

module Sequel::ConnectionPoolPatch
  def connection_pool_class(*)
    Sequel.current.is_a?(Fiber) ? Sequel::FiberedConnectionPool : super
  end
end

# NOTE: Ruby 2.7 DOES NOT SUPPORT class methods prepend in this way
# https://bugs.ruby-lang.org/issues/17423
if RUBY_VERSION > "3"
  Sequel::ConnectionPool::ClassMethods.prepend(Sequel::ConnectionPoolPatch)
else
  class Sequel::ConnectionPool
    class << self
      prepend Sequel::ConnectionPoolPatch
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
umbrellio-sequel-plugins-0.16.0.239 lib/sequel/extensions/fibered_connection_pool.rb
umbrellio-sequel-plugins-0.16.0.238 lib/sequel/extensions/fibered_connection_pool.rb
umbrellio-sequel-plugins-0.16.0.235 lib/sequel/extensions/fibered_connection_pool.rb
umbrellio-sequel-plugins-0.16.0.234 lib/sequel/extensions/fibered_connection_pool.rb
umbrellio-sequel-plugins-0.16.0.233 lib/sequel/extensions/fibered_connection_pool.rb
umbrellio-sequel-plugins-0.16.0 lib/sequel/extensions/fibered_connection_pool.rb
umbrellio-sequel-plugins-0.16.0.211 lib/sequel/extensions/fibered_connection_pool.rb
umbrellio-sequel-plugins-0.15.0.198 lib/sequel/extensions/fibered_connection_pool.rb
umbrellio-sequel-plugins-0.14.0.192 lib/sequel/extensions/fibered_connection_pool.rb
umbrellio-sequel-plugins-0.14.0.189 lib/sequel/extensions/fibered_connection_pool.rb
umbrellio-sequel-plugins-0.14.0.188 lib/sequel/extensions/fibered_connection_pool.rb
umbrellio-sequel-plugins-0.14.0.187 lib/sequel/extensions/fibered_connection_pool.rb
umbrellio-sequel-plugins-0.14.0 lib/sequel/extensions/fibered_connection_pool.rb
umbrellio-sequel-plugins-0.13.0.185 lib/sequel/extensions/fibered_connection_pool.rb