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)
  @name = name
  super spec
  @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

#checkout_with_debugObject



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/with_connection/connection_pool.rb', line 33

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

  begin
    checkout_without_debug
  rescue ActiveRecord::ConnectionTimeoutError => e
    raise ActiveRecord::ConnectionTimeoutError, "could not obtain a #{name} connection#{" within #{@timeout} seconds" if @timeout}.  The max pool size is currently #{@size}; consider increasing it."
  end
end

#create_all_connectionsObject



51
52
53
# File 'lib/with_connection/connection_pool.rb', line 51

def create_all_connections
  @size.times { @connections << new_connection }
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



46
47
48
49
# File 'lib/with_connection/connection_pool.rb', line 46

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(key = nil, read_write = nil) ⇒ Object



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

def with_connection(key=nil, read_write=nil)
  connection_id = current_connection_id
  fresh_connection = true unless @reserved_connections[connection_id]
  yield connection
ensure
  release_connection(connection_id) if fresh_connection
end

#with_connection_with_debug(key = nil, read_write = nil, &block) ⇒ Object



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

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