Class: WithConnection::ConnectionPool

Inherits:
ActiveRecord::ConnectionAdapters::ConnectionPool show all
Defined in:
lib/with_connection/connection_pool.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ActiveRecord::ConnectionAdapters::ConnectionPool

#clear_stale_cached_connections!, #connection_with_sanity_check, #valid_busy_fiber?

Constructor Details

#initialize(name, spec) ⇒ ConnectionPool

Returns a new instance of ConnectionPool.



7
8
9
10
11
12
13
# File 'lib/with_connection/connection_pool.rb', line 7

def initialize(name, spec)
  super spec
  @name = name
  @disable_warning = !! spec.config[:disable_warning]
  @debug_with_connection = !! spec.config[:debug_with_connection]
  ConnectionManagement.connection_pools << self
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/with_connection/connection_pool.rb', line 5

def name
  @name
end

Instance Method Details

#checkoutObject

copied directly from the ActiveRecord just so I could change the error message



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/with_connection/connection_pool.rb', line 39

def checkout
  # Checkout an available connection
  @connection_mutex.synchronize do
    loop do
      conn = if @checked_out.size < @connections.size
               checkout_existing_connection
             elsif @connections.size < @size
               checkout_new_connection
             end
      return conn if conn

      @queue.wait(@timeout)

      if(@checked_out.size < @connections.size)
        next
      else
        clear_stale_cached_connections!
        if @size == @checked_out.size
          raise ConnectionTimeoutError, "could not obtain a #{name} connection#{" within #{@timeout} seconds" if @timeout}.  The max pool size is currently #{@size}; consider increasing it."
        end
      end

    end
  end
end

#checkout_with_debugObject



25
26
27
28
29
30
# File 'lib/with_connection/connection_pool.rb', line 25

def checkout_with_debug
  if @debug_with_connection && ! @using_with_connection
    Rails.logger.warn "#{name} not using with_connection, backtrace: #{caller.inspect}"
  end
  checkout_without_debug
end

#has_connection?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/with_connection/connection_pool.rb', line 15

def has_connection?
  !! @reserved_connections[current_connection_id]
end

#release_connection_with_warningObject



33
34
35
36
# File 'lib/with_connection/connection_pool.rb', line 33

def release_connection_with_warning
  Rails.logger.warn "#{@name} connection was held by the request" if ! @disable_warning && has_connection?
  release_connection
end

#with_connection_with_debug(&block) ⇒ Object



19
20
21
22
# File 'lib/with_connection/connection_pool.rb', line 19

def with_connection_with_debug(&block)
  @using_with_connection = true
  with_connection_without_debug(&block).tap { @using_with_connection = false }
end