Module: FiberedMysql2::FiberedDatabaseConnectionPool

Defined in:
lib/fibered_mysql2/fibered_database_connection_pool.rb

Instance Method Summary collapse

Instance Method Details

#cached_connectionsObject



9
10
11
# File 'lib/fibered_mysql2/fibered_database_connection_pool.rb', line 9

def cached_connections
  @thread_cached_conns
end

#checkout(checkout_timeout = @checkout_timeout) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/fibered_mysql2/fibered_database_connection_pool.rb', line 17

def checkout(checkout_timeout = @checkout_timeout)
  begin
    reap_connections
  rescue => ex
    ActiveRecord::Base.logger.error("Exception occurred while executing reap_connections: #{ex.class}: #{ex.message}")
  end
  super
end

#connectionObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/fibered_mysql2/fibered_database_connection_pool.rb', line 43

def connection
  # this is correctly done double-checked locking
  # (ThreadSafe::Cache's lookups have volatile semantics)
  if (result = cached_connections[current_connection_id])
    result
  else
    synchronize do
      if (result = cached_connections[current_connection_id])
        result
      else
        cached_connections[current_connection_id] = checkout
      end
    end
  end
end

#current_connection_idObject



13
14
15
# File 'lib/fibered_mysql2/fibered_database_connection_pool.rb', line 13

def current_connection_id
  connection_cache_key(current_thread)
end

#initialize(connection_spec, *args, **keyword_args) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/fibered_mysql2/fibered_database_connection_pool.rb', line 32

def initialize(connection_spec, *args, **keyword_args)
  connection_spec.config[:reaping_frequency] and raise "reaping_frequency is not supported (the ActiveRecord Reaper is thread-based)"
  connection_spec.config[:reaping_frequency] = nil # starting in Rails 5, this defaults to 60 if not explicitly set

  super(connection_spec, *args, **keyword_args)

  @reaper = nil # no need to keep a reference to this since it does nothing in this sub-class

  # note that @reserved_connections is a ThreadSafe::Cache which is overkill in a fibered world, but harmless
end

#reap_connectionsObject



59
60
61
62
63
64
65
# File 'lib/fibered_mysql2/fibered_database_connection_pool.rb', line 59

def reap_connections
  cached_connections.values.each do |connection|
    unless connection.owner.alive?
      checkin(connection)
    end
  end
end

#release_connection(owner = Fiber.current) ⇒ Object



26
27
28
29
30
# File 'lib/fibered_mysql2/fibered_database_connection_pool.rb', line 26

def release_connection(owner = Fiber.current)
  if (conn = @thread_cached_conns.delete(connection_cache_key(owner)))
    checkin(conn)
  end
end