Module: FiberedMysql2::FiberedDatabaseConnectionPool
- Defined in:
- lib/fibered_mysql2/fibered_database_connection_pool.rb
Instance Method Summary collapse
- #cached_connections ⇒ Object
- #checkout(checkout_timeout = @checkout_timeout) ⇒ Object
- #connection ⇒ Object
- #current_connection_id ⇒ Object
- #initialize(connection_spec, *args, **keyword_args) ⇒ Object
- #reap_connections ⇒ Object
- #release_connection(owner = Fiber.current) ⇒ Object
Instance Method Details
#cached_connections ⇒ Object
7 8 9 |
# File 'lib/fibered_mysql2/fibered_database_connection_pool.rb', line 7 def cached_connections @thread_cached_conns end |
#checkout(checkout_timeout = @checkout_timeout) ⇒ Object
15 16 17 18 19 20 21 22 |
# File 'lib/fibered_mysql2/fibered_database_connection_pool.rb', line 15 def checkout(checkout_timeout = @checkout_timeout) begin reap_connections rescue => ex ActiveRecord::Base.logger.error("Exception occurred while executing reap_connections: #{ex.class}: #{ex.}") end super end |
#connection ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/fibered_mysql2/fibered_database_connection_pool.rb', line 45 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_id ⇒ Object
11 12 13 |
# File 'lib/fibered_mysql2/fibered_database_connection_pool.rb', line 11 def current_connection_id connection_cache_key(current_thread) end |
#initialize(connection_spec, *args, **keyword_args) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/fibered_mysql2/fibered_database_connection_pool.rb', line 30 def initialize(connection_spec, *args, **keyword_args) if ActiveRecord.gem_version < "6.1" 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 elsif connection_spec.db_config.reaping_frequency connection_spec.db_config.reaping_frequency > 0 and raise "reaping_frequency is not supported (the ActiveRecord Reaper is thread-based)" end 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_connections ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/fibered_mysql2/fibered_database_connection_pool.rb', line 61 def reap_connections cached_connections.values.each do |connection| unless connection.owner.alive? checkin(connection) end end end |
#release_connection(owner = Fiber.current) ⇒ Object
24 25 26 27 28 |
# File 'lib/fibered_mysql2/fibered_database_connection_pool.rb', line 24 def release_connection(owner = Fiber.current) if (conn = @thread_cached_conns.delete(connection_cache_key(owner))) checkin(conn) end end |