Skip to content

Instantly share code, notes, and snippets.

@yhirano55
Last active June 5, 2019 23:27
Show Gist options
  • Save yhirano55/0df87c65ccd1a9c0596d4f92df67e0a9 to your computer and use it in GitHub Desktop.
Save yhirano55/0df87c65ccd1a9c0596d4f92df67e0a9 to your computer and use it in GitHub Desktop.

Generated by trace_location at 2019-06-05 10:39:18 +0900

activerecord-5.2.3/lib/active_record/connection_handling.rb:49
ActiveRecord::ConnectionHandling#establish_connection
def establish_connection(config = nil)
  raise "Anonymous class is not allowed." unless name

  config ||= DEFAULT_ENV.call.to_sym
  spec_name = self == Base ? "primary" : name
  self.connection_specification_name = spec_name

  resolver = ConnectionAdapters::ConnectionSpecification::Resolver.new(Base.configurations)
  spec = resolver.resolve(config).symbolize_keys
  spec[:name] = spec_name

  connection_handler.establish_connection(spec)
end
# called from (irb):2
activerecord-5.2.3/lib/active_record/core.rb:59
ActiveRecord::Base.configurations
def self.configurations
  @@configurations
end
# called from activerecord-5.2.3/lib/active_record/connection_handling.rb:56
activerecord-5.2.3/lib/active_record/connection_adapters/connection_specification.rb:121
ActiveRecord::ConnectionAdapters::ConnectionSpecification::Resolver#initialize
def initialize(configurations)
  @configurations = configurations
end
# called from activerecord-5.2.3/lib/active_record/connection_handling.rb:56
activerecord-5.2.3/lib/active_record/connection_adapters/connection_specification.rb:141
ActiveRecord::ConnectionAdapters::ConnectionSpecification::Resolver#resolve
def resolve(config)
  if config
    resolve_connection config
  elsif env = ActiveRecord::ConnectionHandling::RAILS_ENV.call
    resolve_symbol_connection env.to_sym
  else
    raise AdapterNotSpecified
  end
end
# called from activerecord-5.2.3/lib/active_record/connection_handling.rb:57
activerecord-5.2.3/lib/active_record/connection_adapters/connection_specification.rb:238
ActiveRecord::ConnectionAdapters::ConnectionSpecification::Resolver#resolve_connection
def resolve_connection(spec)
  case spec
  when Symbol
    resolve_symbol_connection spec
  when String
    resolve_url_connection spec
  when Hash
    resolve_hash_connection spec
  end
end
# called from activerecord-5.2.3/lib/active_record/connection_adapters/connection_specification.rb:143
activerecord-5.2.3/lib/active_record/connection_adapters/connection_specification.rb:268
ActiveRecord::ConnectionAdapters::ConnectionSpecification::Resolver#resolve_hash_connection
def resolve_hash_connection(spec)
  if spec["url"] && spec["url"] !~ /^jdbc:/
    connection_hash = resolve_url_connection(spec.delete("url"))
    spec.merge!(connection_hash)
  end
  spec
end
# called from activerecord-5.2.3/lib/active_record/connection_adapters/connection_specification.rb:245
activesupport-5.2.3/lib/active_support/core_ext/hash/keys.rb:56
Hash#symbolize_keys
def symbolize_keys
  transform_keys { |key| key.to_sym rescue key }
end
# called from activerecord-5.2.3/lib/active_record/connection_handling.rb:57
activerecord-5.2.3/lib/active_record/core.rb:130
ActiveRecord::Base.connection_handler
def self.connection_handler
  ActiveRecord::RuntimeRegistry.connection_handler || default_connection_handler
end
# called from activerecord-5.2.3/lib/active_record/connection_handling.rb:60
activerecord-5.2.3/lib/active_record/runtime_registry.rb:20
ActiveRecord::RuntimeRegistry.connection_handler
class_eval %{ def self.#{val}; instance.#{val}; end }, __FILE__, __LINE__
# called from activerecord-5.2.3/lib/active_record/core.rb:131
activesupport-5.2.3/lib/active_support/per_thread_registry.rb:46
ActiveSupport::PerThreadRegistry#instance
def instance
  Thread.current[@per_thread_registry_key] ||= new
end
# called from activerecord-5.2.3/lib/active_record/runtime_registry.rb:20
activesupport-5.2.3/lib/active_support/core_ext/class/attribute.rb:106
ActiveRecord::Base.default_connection_handler
redefine_method(name) { val }
# called from activerecord-5.2.3/lib/active_record/core.rb:131
activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:954
ActiveRecord::ConnectionAdapters::ConnectionHandler#establish_connection
def establish_connection(config)
  resolver = ConnectionSpecification::Resolver.new(Base.configurations)
  spec = resolver.spec(config)

  remove_connection(spec.name)

  message_bus = ActiveSupport::Notifications.instrumenter
  payload = {
    connection_id: object_id
  }
  if spec
    payload[:spec_name] = spec.name
    payload[:config] = spec.config
  end

  message_bus.instrument("!connection.active_record", payload) do
    owner_to_pool[spec.name] = ConnectionAdapters::ConnectionPool.new(spec)
  end

  owner_to_pool[spec.name]
end
# called from activerecord-5.2.3/lib/active_record/connection_handling.rb:60
activerecord-5.2.3/lib/active_record/core.rb:59
ActiveRecord::Base.configurations
def self.configurations
  @@configurations
end
# called from activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:955
activerecord-5.2.3/lib/active_record/connection_adapters/connection_specification.rb:121
ActiveRecord::ConnectionAdapters::ConnectionSpecification::Resolver#initialize
def initialize(configurations)
  @configurations = configurations
end
# called from activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:955
activerecord-5.2.3/lib/active_record/connection_adapters/connection_specification.rb:181
ActiveRecord::ConnectionAdapters::ConnectionSpecification::Resolver#spec
def spec(config)
  spec = resolve(config).symbolize_keys

  raise(AdapterNotSpecified, "database configuration does not specify adapter") unless spec.key?(:adapter)

  # Require the adapter itself and give useful feedback about
  #   1. Missing adapter gems and
  #   2. Adapter gems' missing dependencies.
  path_to_adapter = "active_record/connection_adapters/#{spec[:adapter]}_adapter"
  begin
    require path_to_adapter
  rescue LoadError => e
    # We couldn't require the adapter itself. Raise an exception that
    # points out config typos and missing gems.
    if e.path == path_to_adapter
      # We can assume that a non-builtin adapter was specified, so it's
      # either misspelled or missing from Gemfile.
      raise LoadError, "Could not load the '#{spec[:adapter]}' Active Record adapter. Ensure that the adapter is spelled correctly in config/database.yml and that you've added the necessary adapter gem to your Gemfile.", e.backtrace

    # Bubbled up from the adapter require. Prefix the exception message
    # with some guidance about how to address it and reraise.
    else
      raise LoadError, "Error loading the '#{spec[:adapter]}' Active Record adapter. Missing a gem it depends on? #{e.message}", e.backtrace
    end
  end

  adapter_method = "#{spec[:adapter]}_connection"

  unless ActiveRecord::Base.respond_to?(adapter_method)
    raise AdapterNotFound, "database configuration specifies nonexistent #{spec.config[:adapter]} adapter"
  end

  ConnectionSpecification.new(spec.delete(:name) || "primary", spec, adapter_method)
end
# called from activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:956
activerecord-5.2.3/lib/active_record/connection_adapters/connection_specification.rb:141
ActiveRecord::ConnectionAdapters::ConnectionSpecification::Resolver#resolve
def resolve(config)
  if config
    resolve_connection config
  elsif env = ActiveRecord::ConnectionHandling::RAILS_ENV.call
    resolve_symbol_connection env.to_sym
  else
    raise AdapterNotSpecified
  end
end
# called from activerecord-5.2.3/lib/active_record/connection_adapters/connection_specification.rb:182
activerecord-5.2.3/lib/active_record/connection_adapters/connection_specification.rb:238
ActiveRecord::ConnectionAdapters::ConnectionSpecification::Resolver#resolve_connection
def resolve_connection(spec)
  case spec
  when Symbol
    resolve_symbol_connection spec
  when String
    resolve_url_connection spec
  when Hash
    resolve_hash_connection spec
  end
end
# called from activerecord-5.2.3/lib/active_record/connection_adapters/connection_specification.rb:143
activerecord-5.2.3/lib/active_record/connection_adapters/connection_specification.rb:268
ActiveRecord::ConnectionAdapters::ConnectionSpecification::Resolver#resolve_hash_connection
def resolve_hash_connection(spec)
  if spec["url"] && spec["url"] !~ /^jdbc:/
    connection_hash = resolve_url_connection(spec.delete("url"))
    spec.merge!(connection_hash)
  end
  spec
end
# called from activerecord-5.2.3/lib/active_record/connection_adapters/connection_specification.rb:245
activesupport-5.2.3/lib/active_support/core_ext/hash/keys.rb:56
Hash#symbolize_keys
def symbolize_keys
  transform_keys { |key| key.to_sym rescue key }
end
# called from activerecord-5.2.3/lib/active_record/connection_adapters/connection_specification.rb:182
activesupport-5.2.3/lib/active_support/dependencies.rb:289
ActiveSupport::Dependencies::Loadable#require
def require(file)
  result = false
  load_dependency(file) { result = super }
  result
end
# called from activerecord-5.2.3/lib/active_record/connection_adapters/connection_specification.rb:191
activesupport-5.2.3/lib/active_support/dependencies.rb:251
ActiveSupport::Dependencies::Loadable#load_dependency
def load_dependency(file)
  if Dependencies.load? && Dependencies.constant_watch_stack.watching?
    descs = Dependencies.constant_watch_stack.watching.flatten.uniq

    Dependencies.new_constants_in(*descs) { yield }
  else
    yield
  end
rescue Exception => exception  # errors from loading file
  exception.blame_file! file if exception.respond_to? :blame_file!
  raise
end
# called from activesupport-5.2.3/lib/active_support/dependencies.rb:291
activesupport-5.2.3/lib/active_support/dependencies.rb:328
ActiveSupport::Dependencies#load?
def load?
  mechanism == :load
end
# called from activesupport-5.2.3/lib/active_support/dependencies.rb:252
activesupport-5.2.3/lib/active_support/core_ext/module/attribute_accessors.rb:60
ActiveSupport::Dependencies.mechanism
def self.#{sym}
  @@#{sym}
end
# called from activesupport-5.2.3/lib/active_support/dependencies.rb:329
activesupport-5.2.3/lib/active_support/core_ext/module/attribute_accessors.rb:60
ActiveSupport::Dependencies.constant_watch_stack
def self.#{sym}
  @@#{sym}
end
# called from activesupport-5.2.3/lib/active_support/dependencies.rb:252
activesupport-5.2.3/lib/active_support/dependencies.rb:111
ActiveSupport::Dependencies::WatchStack#watching?
def watching?
  !@watching.empty?
end
# called from activesupport-5.2.3/lib/active_support/dependencies.rb:252
activerecord-5.2.3/lib/active_record/connection_adapters/connection_specification.rb:10
ActiveRecord::ConnectionAdapters::ConnectionSpecification#initialize
def initialize(name, config, adapter_method)
  @name, @config, @adapter_method = name, config, adapter_method
end
# called from activerecord-5.2.3/lib/active_record/connection_adapters/connection_specification.rb:213
activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:1028
ActiveRecord::ConnectionAdapters::ConnectionHandler#remove_connection
def remove_connection(spec_name)
  if pool = owner_to_pool.delete(spec_name)
    pool.automatic_reconnect = false
    pool.disconnect!
    pool.spec.config
  end
end
# called from activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:958
activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:1058
ActiveRecord::ConnectionAdapters::ConnectionHandler#owner_to_pool
def owner_to_pool
  @owner_to_pool[Process.pid]
end
# called from activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:1029
concurrent-ruby-1.1.5/lib/concurrent/map.rb:132
Concurrent::Map#[]
def [](key)
  if value = super # non-falsy value is an existing mapping, return it right away
    value
    # re-check is done with get_or_default(key, NULL) instead of a simple !key?(key) in order to avoid a race condition, whereby by the time the current thread gets to the key?(key) call
    # a key => value mapping might have already been created by a different thread (key?(key) would then return true, this elsif branch wouldn't be taken and an incorrent +nil+ value
    # would be returned)
    # note: nil == value check is not technically necessary
  elsif @default_proc && nil == value && NULL == (value = get_or_default(key, NULL))
    @default_proc.call(self, key)
  else
    value
  end
end
# called from activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:1059
concurrent-ruby-1.1.5/lib/concurrent/collection/map/non_concurrent_map_backend.rb:19
Concurrent::Collection::NonConcurrentMapBackend#[]
def [](key)
  if value = super # non-falsy value is an existing mapping, return it right away
    value
    # re-check is done with get_or_default(key, NULL) instead of a simple !key?(key) in order to avoid a race condition, whereby by the time the current thread gets to the key?(key) call
    # a key => value mapping might have already been created by a different thread (key?(key) would then return true, this elsif branch wouldn't be taken and an incorrent +nil+ value
    # would be returned)
    # note: nil == value check is not technically necessary
  elsif @default_proc && nil == value && NULL == (value = get_or_default(key, NULL))
    @default_proc.call(self, key)
  else
    value
  end
end
# called from concurrent-ruby-1.1.5/lib/concurrent/map.rb:133
concurrent-ruby-1.1.5/lib/concurrent/collection/map/mri_map_backend.rb:53
Concurrent::Collection::MriMapBackend#delete
def delete(key)
  @write_lock.synchronize { super }
end
# called from activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:1029
concurrent-ruby-1.1.5/lib/concurrent/collection/map/non_concurrent_map_backend.rb:79
Concurrent::Collection::NonConcurrentMapBackend#delete
def delete(key)
  @write_lock.synchronize { super }
end
# called from concurrent-ruby-1.1.5/lib/concurrent/collection/map/mri_map_backend.rb:54
activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:454
ActiveRecord::ConnectionAdapters::ConnectionPool#disconnect!
def disconnect!
  disconnect(false)
end
# called from activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:1031
activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:432
ActiveRecord::ConnectionAdapters::ConnectionPool#disconnect
def disconnect(raise_on_acquisition_timeout = true)
  with_exclusively_acquired_all_connections(raise_on_acquisition_timeout) do
    synchronize do
      @connections.each do |conn|
        if conn.in_use?
          conn.steal!
          checkin conn
        end
        conn.disconnect!
      end
      @connections = []
      @available.clear
    end
  end
end
# called from activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:455
activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:675
ActiveRecord::ConnectionAdapters::ConnectionPool#with_exclusively_acquired_all_connections
def with_exclusively_acquired_all_connections(raise_on_acquisition_timeout = true)
  with_new_connections_blocked do
    attempt_to_checkout_all_existing_connections(raise_on_acquisition_timeout)
    yield
  end
end
# called from activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:433
activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:748
ActiveRecord::ConnectionAdapters::ConnectionPool#with_new_connections_blocked
def with_new_connections_blocked
  synchronize do
    @threads_blocking_new_connections += 1
  end

  yield
ensure
  num_new_conns_required = 0

  synchronize do
    @threads_blocking_new_connections -= 1

    if @threads_blocking_new_connections.zero?
      @available.clear

      num_new_conns_required = num_waiting_in_queue

      @connections.each do |conn|
        next if conn.in_use?

        @available.add conn
        num_new_conns_required -= 1
      end
    end
  end

  bulk_make_new_connections(num_new_conns_required) if num_new_conns_required > 0
end
# called from activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:676
/Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:227
MonitorMixin#mon_synchronize
def mon_synchronize
  mon_enter
  begin
    yield
  ensure
    mon_exit
  end
end
# called from activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:749
/Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:187
MonitorMixin#mon_enter
def mon_enter
  if @mon_owner != Thread.current
    @mon_mutex.lock
    @mon_owner = Thread.current
    @mon_count = 0
  end
  @mon_count += 1
end
# called from /Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:228
/Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:199
MonitorMixin#mon_exit
def mon_exit
  mon_check_owner
  @mon_count -=1
  if @mon_count == 0
    @mon_owner = nil
    @mon_mutex.unlock
  end
end
# called from /Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:232
/Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:267
MonitorMixin#mon_check_owner
def mon_check_owner
  if @mon_owner != Thread.current
    raise ThreadError, "current thread not owner"
  end
end
# called from /Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:200
activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:682
ActiveRecord::ConnectionAdapters::ConnectionPool#attempt_to_checkout_all_existing_connections
def attempt_to_checkout_all_existing_connections(raise_on_acquisition_timeout = true)
  collected_conns = synchronize do
    # account for our own connections
    @connections.select { |conn| conn.owner == Thread.current }
  end

  newly_checked_out = []
  timeout_time      = Time.now + (@checkout_timeout * 2)

  @available.with_a_bias_for(Thread.current) do
    loop do
      synchronize do
        return if collected_conns.size == @connections.size && @now_connecting == 0
        remaining_timeout = timeout_time - Time.now
        remaining_timeout = 0 if remaining_timeout < 0
        conn = checkout_for_exclusive_access(remaining_timeout)
        collected_conns   << conn
        newly_checked_out << conn
      end
    end
  end
rescue ExclusiveConnectionTimeoutError
  # <tt>raise_on_acquisition_timeout == false</tt> means we are directed to ignore any
  # timeouts and are expected to just give up: we've obtained as many connections
  # as possible, note that in a case like that we don't return any of the
  # +newly_checked_out+ connections.

  if raise_on_acquisition_timeout
    release_newly_checked_out = true
    raise
  end
rescue Exception # if something else went wrong
  # this can't be a "naked" rescue, because we have should return conns
  # even for non-StandardErrors
  release_newly_checked_out = true
  raise
ensure
  if release_newly_checked_out && newly_checked_out
    # releasing only those conns that were checked out in this method, conns
    # checked outside this method (before it was called) are not for us to release
    newly_checked_out.each { |conn| checkin(conn) }
  end
end
# called from activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:677
/Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:227
MonitorMixin#mon_synchronize
def mon_synchronize
  mon_enter
  begin
    yield
  ensure
    mon_exit
  end
end
# called from activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:683
/Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:187
MonitorMixin#mon_enter
def mon_enter
  if @mon_owner != Thread.current
    @mon_mutex.lock
    @mon_owner = Thread.current
    @mon_count = 0
  end
  @mon_count += 1
end
# called from /Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:228
/Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:199
MonitorMixin#mon_exit
def mon_exit
  mon_check_owner
  @mon_count -=1
  if @mon_count == 0
    @mon_owner = nil
    @mon_mutex.unlock
  end
end
# called from /Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:232
/Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:267
MonitorMixin#mon_check_owner
def mon_check_owner
  if @mon_owner != Thread.current
    raise ThreadError, "current thread not owner"
  end
end
# called from /Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:200
activesupport-5.2.3/lib/active_support/core_ext/time/calculations.rb:261
Time#plus_with_duration
def plus_with_duration(other) #:nodoc:
  if ActiveSupport::Duration === other
    other.since(self)
  else
    plus_without_duration(other)
  end
end
# called from activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:689
activesupport-5.2.3/lib/active_support/duration.rb:144
ActiveSupport::Duration.===
def ===(other) #:nodoc:
  other.is_a?(Duration)
rescue ::NoMethodError
  false
end
# called from activesupport-5.2.3/lib/active_support/core_ext/time/calculations.rb:262
activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:251
ActiveRecord::ConnectionAdapters::ConnectionPool::BiasableQueue#with_a_bias_for
def with_a_bias_for(thread)
  previous_cond = nil
  new_cond      = nil
  synchronize do
    previous_cond = @cond
    @cond = new_cond = BiasedConditionVariable.new(@lock, @cond, thread)
  end
  yield
ensure
  synchronize do
    @cond = previous_cond if previous_cond
    new_cond.broadcast_on_biased if new_cond # wake up any remaining sleepers
  end
end
# called from activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:691
activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:154
ActiveRecord::ConnectionAdapters::ConnectionPool::Queue#synchronize
def synchronize(&block)
  @lock.synchronize(&block)
end
# called from activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:254
/Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:227
MonitorMixin#mon_synchronize
def mon_synchronize
  mon_enter
  begin
    yield
  ensure
    mon_exit
  end
end
# called from activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:155
/Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:187
MonitorMixin#mon_enter
def mon_enter
  if @mon_owner != Thread.current
    @mon_mutex.lock
    @mon_owner = Thread.current
    @mon_count = 0
  end
  @mon_count += 1
end
# called from /Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:228
activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:215
ActiveRecord::ConnectionAdapters::ConnectionPool::BiasableQueue::BiasedConditionVariable#initialize
def initialize(lock, other_cond, preferred_thread)
  @real_cond = lock.new_cond
  @other_cond = other_cond
  @preferred_thread = preferred_thread
  @num_waiting_on_real_cond = 0
end
# called from activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:256
/Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:241
MonitorMixin#new_cond
def new_cond
  return ConditionVariable.new(self)
end
# called from activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:216
/Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:156
MonitorMixin::ConditionVariable#initialize
def initialize(monitor)
  @monitor = monitor
  @cond = Thread::ConditionVariable.new
end
# called from /Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:242
/Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:199
MonitorMixin#mon_exit
def mon_exit
  mon_check_owner
  @mon_count -=1
  if @mon_count == 0
    @mon_owner = nil
    @mon_mutex.unlock
  end
end
# called from /Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:232
/Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:267
MonitorMixin#mon_check_owner
def mon_check_owner
  if @mon_owner != Thread.current
    raise ThreadError, "current thread not owner"
  end
end
# called from /Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:200
/Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:227
MonitorMixin#mon_synchronize
def mon_synchronize
  mon_enter
  begin
    yield
  ensure
    mon_exit
  end
end
# called from activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:693
/Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:187
MonitorMixin#mon_enter
def mon_enter
  if @mon_owner != Thread.current
    @mon_mutex.lock
    @mon_owner = Thread.current
    @mon_count = 0
  end
  @mon_count += 1
end
# called from /Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:228
/Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:199
MonitorMixin#mon_exit
def mon_exit
  mon_check_owner
  @mon_count -=1
  if @mon_count == 0
    @mon_owner = nil
    @mon_mutex.unlock
  end
end
# called from /Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:232
/Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:267
MonitorMixin#mon_check_owner
def mon_check_owner
  if @mon_owner != Thread.current
    raise ThreadError, "current thread not owner"
  end
end
# called from /Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:200
activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:154
ActiveRecord::ConnectionAdapters::ConnectionPool::Queue#synchronize
def synchronize(&block)
  @lock.synchronize(&block)
end
# called from activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:260
/Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:227
MonitorMixin#mon_synchronize
def mon_synchronize
  mon_enter
  begin
    yield
  ensure
    mon_exit
  end
end
# called from activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:155
/Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:187
MonitorMixin#mon_enter
def mon_enter
  if @mon_owner != Thread.current
    @mon_mutex.lock
    @mon_owner = Thread.current
    @mon_count = 0
  end
  @mon_count += 1
end
# called from /Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:228
activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:227
ActiveRecord::ConnectionAdapters::ConnectionPool::BiasableQueue::BiasedConditionVariable#broadcast_on_biased
def broadcast_on_biased
  @num_waiting_on_real_cond = 0
  @real_cond.broadcast
end
# called from activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:262
/Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:149
MonitorMixin::ConditionVariable#broadcast
def broadcast
  @monitor.__send__(:mon_check_owner)
  @cond.broadcast
end
# called from activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:229
/Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:267
MonitorMixin#mon_check_owner
def mon_check_owner
  if @mon_owner != Thread.current
    raise ThreadError, "current thread not owner"
  end
end
# called from /Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:150
/Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:199
MonitorMixin#mon_exit
def mon_exit
  mon_check_owner
  @mon_count -=1
  if @mon_count == 0
    @mon_owner = nil
    @mon_mutex.unlock
  end
end
# called from /Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:232
/Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:267
MonitorMixin#mon_check_owner
def mon_check_owner
  if @mon_owner != Thread.current
    raise ThreadError, "current thread not owner"
  end
end
# called from /Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:200
/Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:227
MonitorMixin#mon_synchronize
def mon_synchronize
  mon_enter
  begin
    yield
  ensure
    mon_exit
  end
end
# called from activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:434
/Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:187
MonitorMixin#mon_enter
def mon_enter
  if @mon_owner != Thread.current
    @mon_mutex.lock
    @mon_owner = Thread.current
    @mon_count = 0
  end
  @mon_count += 1
end
# called from /Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:228
activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:124
ActiveRecord::ConnectionAdapters::ConnectionPool::Queue#clear
def clear
  synchronize do
    @queue.clear
  end
end
# called from activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:443
activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:154
ActiveRecord::ConnectionAdapters::ConnectionPool::Queue#synchronize
def synchronize(&block)
  @lock.synchronize(&block)
end
# called from activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:125
/Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:227
MonitorMixin#mon_synchronize
def mon_synchronize
  mon_enter
  begin
    yield
  ensure
    mon_exit
  end
end
# called from activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:155
/Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:187
MonitorMixin#mon_enter
def mon_enter
  if @mon_owner != Thread.current
    @mon_mutex.lock
    @mon_owner = Thread.current
    @mon_count = 0
  end
  @mon_count += 1
end
# called from /Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:228
/Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:199
MonitorMixin#mon_exit
def mon_exit
  mon_check_owner
  @mon_count -=1
  if @mon_count == 0
    @mon_owner = nil
    @mon_mutex.unlock
  end
end
# called from /Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:232
/Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:267
MonitorMixin#mon_check_owner
def mon_check_owner
  if @mon_owner != Thread.current
    raise ThreadError, "current thread not owner"
  end
end
# called from /Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:200
/Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:199
MonitorMixin#mon_exit
def mon_exit
  mon_check_owner
  @mon_count -=1
  if @mon_count == 0
    @mon_owner = nil
    @mon_mutex.unlock
  end
end
# called from /Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:232
/Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:267
MonitorMixin#mon_check_owner
def mon_check_owner
  if @mon_owner != Thread.current
    raise ThreadError, "current thread not owner"
  end
end
# called from /Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:200
/Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:227
MonitorMixin#mon_synchronize
def mon_synchronize
  mon_enter
  begin
    yield
  ensure
    mon_exit
  end
end
# called from activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:757
/Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:187
MonitorMixin#mon_enter
def mon_enter
  if @mon_owner != Thread.current
    @mon_mutex.lock
    @mon_owner = Thread.current
    @mon_count = 0
  end
  @mon_count += 1
end
# called from /Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:228
activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:124
ActiveRecord::ConnectionAdapters::ConnectionPool::Queue#clear
def clear
  synchronize do
    @queue.clear
  end
end
# called from activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:761
activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:154
ActiveRecord::ConnectionAdapters::ConnectionPool::Queue#synchronize
def synchronize(&block)
  @lock.synchronize(&block)
end
# called from activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:125
/Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:227
MonitorMixin#mon_synchronize
def mon_synchronize
  mon_enter
  begin
    yield
  ensure
    mon_exit
  end
end
# called from activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:155
/Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:187
MonitorMixin#mon_enter
def mon_enter
  if @mon_owner != Thread.current
    @mon_mutex.lock
    @mon_owner = Thread.current
    @mon_count = 0
  end
  @mon_count += 1
end
# called from /Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:228
/Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:199
MonitorMixin#mon_exit
def mon_exit
  mon_check_owner
  @mon_count -=1
  if @mon_count == 0
    @mon_owner = nil
    @mon_mutex.unlock
  end
end
# called from /Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:232
/Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:267
MonitorMixin#mon_check_owner
def mon_check_owner
  if @mon_owner != Thread.current
    raise ThreadError, "current thread not owner"
  end
end
# called from /Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:200
activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:627
ActiveRecord::ConnectionAdapters::ConnectionPool#num_waiting_in_queue
def num_waiting_in_queue # :nodoc:
  @available.num_waiting
end
# called from activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:763
activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:102
ActiveRecord::ConnectionAdapters::ConnectionPool::Queue#num_waiting
def num_waiting
  synchronize do
    @num_waiting
  end
end
# called from activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:628
activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:154
ActiveRecord::ConnectionAdapters::ConnectionPool::Queue#synchronize
def synchronize(&block)
  @lock.synchronize(&block)
end
# called from activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:103
/Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:227
MonitorMixin#mon_synchronize
def mon_synchronize
  mon_enter
  begin
    yield
  ensure
    mon_exit
  end
end
# called from activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:155
/Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:187
MonitorMixin#mon_enter
def mon_enter
  if @mon_owner != Thread.current
    @mon_mutex.lock
    @mon_owner = Thread.current
    @mon_count = 0
  end
  @mon_count += 1
end
# called from /Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:228
/Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:199
MonitorMixin#mon_exit
def mon_exit
  mon_check_owner
  @mon_count -=1
  if @mon_count == 0
    @mon_owner = nil
    @mon_mutex.unlock
  end
end
# called from /Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:232
/Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:267
MonitorMixin#mon_check_owner
def mon_check_owner
  if @mon_owner != Thread.current
    raise ThreadError, "current thread not owner"
  end
end
# called from /Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:200
/Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:199
MonitorMixin#mon_exit
def mon_exit
  mon_check_owner
  @mon_count -=1
  if @mon_count == 0
    @mon_owner = nil
    @mon_mutex.unlock
  end
end
# called from /Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:232
/Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:267
MonitorMixin#mon_check_owner
def mon_check_owner
  if @mon_owner != Thread.current
    raise ThreadError, "current thread not owner"
  end
end
# called from /Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:200
activesupport-5.2.3/lib/active_support/notifications.rb:189
ActiveSupport::Notifications.instrumenter
def instrumenter
  InstrumentationRegistry.instance.instrumenter_for(notifier)
end
# called from activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:960
activesupport-5.2.3/lib/active_support/per_thread_registry.rb:46
ActiveSupport::PerThreadRegistry#instance
def instance
  Thread.current[@per_thread_registry_key] ||= new
end
# called from activesupport-5.2.3/lib/active_support/notifications.rb:190
activesupport-5.2.3/lib/active_support/notifications.rb:209
ActiveSupport::Notifications::InstrumentationRegistry#instrumenter_for
def instrumenter_for(notifier)
  @registry[notifier] ||= Instrumenter.new(notifier)
end
# called from activesupport-5.2.3/lib/active_support/notifications.rb:190
activesupport-5.2.3/lib/active_support/notifications/instrumenter.rb:19
ActiveSupport::Notifications::Instrumenter#instrument
def instrument(name, payload = {})
  # some of the listeners might have state
  listeners_state = start name, payload
  begin
    yield payload
  rescue Exception => e
    payload[:exception] = [e.class.name, e.message]
    payload[:exception_object] = e
    raise e
  ensure
    finish_with_state listeners_state, name, payload
  end
end
# called from activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:969
activesupport-5.2.3/lib/active_support/notifications/instrumenter.rb:34
ActiveSupport::Notifications::Instrumenter#start
def start(name, payload)
  @notifier.start name, @id, payload
end
# called from activesupport-5.2.3/lib/active_support/notifications/instrumenter.rb:21
activesupport-5.2.3/lib/active_support/notifications/fanout.rb:43
ActiveSupport::Notifications::Fanout#start
def start(name, id, payload)
  listeners_for(name).each { |s| s.start(name, id, payload) }
end
# called from activesupport-5.2.3/lib/active_support/notifications/instrumenter.rb:35
activesupport-5.2.3/lib/active_support/notifications/fanout.rb:55
ActiveSupport::Notifications::Fanout#listeners_for
def listeners_for(name)
  # this is correctly done double-checked locking (Concurrent::Map's lookups have volatile semantics)
  @listeners_for[name] || synchronize do
    # use synchronisation when accessing @subscribers
    @listeners_for[name] ||= @subscribers.select { |s| s.subscribed_to?(name) }
  end
end
# called from activesupport-5.2.3/lib/active_support/notifications/fanout.rb:44
concurrent-ruby-1.1.5/lib/concurrent/map.rb:132
Concurrent::Map#[]
def [](key)
  if value = super # non-falsy value is an existing mapping, return it right away
    value
    # re-check is done with get_or_default(key, NULL) instead of a simple !key?(key) in order to avoid a race condition, whereby by the time the current thread gets to the key?(key) call
    # a key => value mapping might have already been created by a different thread (key?(key) would then return true, this elsif branch wouldn't be taken and an incorrent +nil+ value
    # would be returned)
    # note: nil == value check is not technically necessary
  elsif @default_proc && nil == value && NULL == (value = get_or_default(key, NULL))
    @default_proc.call(self, key)
  else
    value
  end
end
# called from activesupport-5.2.3/lib/active_support/notifications/fanout.rb:57
concurrent-ruby-1.1.5/lib/concurrent/collection/map/non_concurrent_map_backend.rb:19
Concurrent::Collection::NonConcurrentMapBackend#[]
def [](key)
  if value = super # non-falsy value is an existing mapping, return it right away
    value
    # re-check is done with get_or_default(key, NULL) instead of a simple !key?(key) in order to avoid a race condition, whereby by the time the current thread gets to the key?(key) call
    # a key => value mapping might have already been created by a different thread (key?(key) would then return true, this elsif branch wouldn't be taken and an incorrent +nil+ value
    # would be returned)
    # note: nil == value check is not technically necessary
  elsif @default_proc && nil == value && NULL == (value = get_or_default(key, NULL))
    @default_proc.call(self, key)
  else
    value
  end
end
# called from concurrent-ruby-1.1.5/lib/concurrent/map.rb:133
activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:1058
ActiveRecord::ConnectionAdapters::ConnectionHandler#owner_to_pool
def owner_to_pool
  @owner_to_pool[Process.pid]
end
# called from activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:970
concurrent-ruby-1.1.5/lib/concurrent/map.rb:132
Concurrent::Map#[]
def [](key)
  if value = super # non-falsy value is an existing mapping, return it right away
    value
    # re-check is done with get_or_default(key, NULL) instead of a simple !key?(key) in order to avoid a race condition, whereby by the time the current thread gets to the key?(key) call
    # a key => value mapping might have already been created by a different thread (key?(key) would then return true, this elsif branch wouldn't be taken and an incorrent +nil+ value
    # would be returned)
    # note: nil == value check is not technically necessary
  elsif @default_proc && nil == value && NULL == (value = get_or_default(key, NULL))
    @default_proc.call(self, key)
  else
    value
  end
end
# called from activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:1059
concurrent-ruby-1.1.5/lib/concurrent/collection/map/non_concurrent_map_backend.rb:19
Concurrent::Collection::NonConcurrentMapBackend#[]
def [](key)
  if value = super # non-falsy value is an existing mapping, return it right away
    value
    # re-check is done with get_or_default(key, NULL) instead of a simple !key?(key) in order to avoid a race condition, whereby by the time the current thread gets to the key?(key) call
    # a key => value mapping might have already been created by a different thread (key?(key) would then return true, this elsif branch wouldn't be taken and an incorrent +nil+ value
    # would be returned)
    # note: nil == value check is not technically necessary
  elsif @default_proc && nil == value && NULL == (value = get_or_default(key, NULL))
    @default_proc.call(self, key)
  else
    value
  end
end
# called from concurrent-ruby-1.1.5/lib/concurrent/map.rb:133
activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:321
ActiveRecord::ConnectionAdapters::ConnectionPool#initialize
def initialize(spec)
  super()

  @spec = spec

  @checkout_timeout = (spec.config[:checkout_timeout] && spec.config[:checkout_timeout].to_f) || 5
  if @idle_timeout = spec.config.fetch(:idle_timeout, 300)
    @idle_timeout = @idle_timeout.to_f
    @idle_timeout = nil if @idle_timeout <= 0
  end

  # default max pool size to 5
  @size = (spec.config[:pool] && spec.config[:pool].to_i) || 5

  # This variable tracks the cache of threads mapped to reserved connections, with the
  # sole purpose of speeding up the +connection+ method. It is not the authoritative
  # registry of which thread owns which connection. Connection ownership is tracked by
  # the +connection.owner+ attr on each +connection+ instance.
  # The invariant works like this: if there is mapping of <tt>thread => conn</tt>,
  # then that +thread+ does indeed own that +conn+. However, an absence of a such
  # mapping does not mean that the +thread+ doesn't own the said connection. In
  # that case +conn.owner+ attr should be consulted.
  # Access and modification of <tt>@thread_cached_conns</tt> does not require
  # synchronization.
  @thread_cached_conns = Concurrent::Map.new(initial_capacity: @size)

  @connections         = []
  @automatic_reconnect = true

  # Connection pool allows for concurrent (outside the main +synchronize+ section)
  # establishment of new connections. This variable tracks the number of threads
  # currently in the process of independently establishing connections to the DB.
  @now_connecting = 0

  @threads_blocking_new_connections = 0

  @available = ConnectionLeasingQueue.new self

  @lock_thread = false

  # +reaping_frequency+ is configurable mostly for historical reasons, but it could
  # also be useful if someone wants a very low +idle_timeout+.
  reaping_frequency = spec.config.fetch(:reaping_frequency, 60)
  @reaper = Reaper.new(self, reaping_frequency && reaping_frequency.to_f)
  @reaper.run
end
# called from activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:970
activerecord-5.2.3/lib/active_record/connection_adapters/abstract/query_cache.rb:29
ActiveRecord::ConnectionAdapters::QueryCache::ConnectionPoolConfiguration#initialize
def initialize(spec)
  super()

  @spec = spec

  @checkout_timeout = (spec.config[:checkout_timeout] && spec.config[:checkout_timeout].to_f) || 5
  if @idle_timeout = spec.config.fetch(:idle_timeout, 300)
    @idle_timeout = @idle_timeout.to_f
    @idle_timeout = nil if @idle_timeout <= 0
  end

  # default max pool size to 5
  @size = (spec.config[:pool] && spec.config[:pool].to_i) || 5

  # This variable tracks the cache of threads mapped to reserved connections, with the
  # sole purpose of speeding up the +connection+ method. It is not the authoritative
  # registry of which thread owns which connection. Connection ownership is tracked by
  # the +connection.owner+ attr on each +connection+ instance.
  # The invariant works like this: if there is mapping of <tt>thread => conn</tt>,
  # then that +thread+ does indeed own that +conn+. However, an absence of a such
  # mapping does not mean that the +thread+ doesn't own the said connection. In
  # that case +conn.owner+ attr should be consulted.
  # Access and modification of <tt>@thread_cached_conns</tt> does not require
  # synchronization.
  @thread_cached_conns = Concurrent::Map.new(initial_capacity: @size)

  @connections         = []
  @automatic_reconnect = true

  # Connection pool allows for concurrent (outside the main +synchronize+ section)
  # establishment of new connections. This variable tracks the number of threads
  # currently in the process of independently establishing connections to the DB.
  @now_connecting = 0

  @threads_blocking_new_connections = 0

  @available = ConnectionLeasingQueue.new self

  @lock_thread = false

  # +reaping_frequency+ is configurable mostly for historical reasons, but it could
  # also be useful if someone wants a very low +idle_timeout+.
  reaping_frequency = spec.config.fetch(:reaping_frequency, 60)
  @reaper = Reaper.new(self, reaping_frequency && reaping_frequency.to_f)
  @reaper.run
end
# called from activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:322
/Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:250
MonitorMixin#initialize
def initialize(spec)
  super()

  @spec = spec

  @checkout_timeout = (spec.config[:checkout_timeout] && spec.config[:checkout_timeout].to_f) || 5
  if @idle_timeout = spec.config.fetch(:idle_timeout, 300)
    @idle_timeout = @idle_timeout.to_f
    @idle_timeout = nil if @idle_timeout <= 0
  end

  # default max pool size to 5
  @size = (spec.config[:pool] && spec.config[:pool].to_i) || 5

  # This variable tracks the cache of threads mapped to reserved connections, with the
  # sole purpose of speeding up the +connection+ method. It is not the authoritative
  # registry of which thread owns which connection. Connection ownership is tracked by
  # the +connection.owner+ attr on each +connection+ instance.
  # The invariant works like this: if there is mapping of <tt>thread => conn</tt>,
  # then that +thread+ does indeed own that +conn+. However, an absence of a such
  # mapping does not mean that the +thread+ doesn't own the said connection. In
  # that case +conn.owner+ attr should be consulted.
  # Access and modification of <tt>@thread_cached_conns</tt> does not require
  # synchronization.
  @thread_cached_conns = Concurrent::Map.new(initial_capacity: @size)

  @connections         = []
  @automatic_reconnect = true

  # Connection pool allows for concurrent (outside the main +synchronize+ section)
  # establishment of new connections. This variable tracks the number of threads
  # currently in the process of independently establishing connections to the DB.
  @now_connecting = 0

  @threads_blocking_new_connections = 0

  @available = ConnectionLeasingQueue.new self

  @lock_thread = false

  # +reaping_frequency+ is configurable mostly for historical reasons, but it could
  # also be useful if someone wants a very low +idle_timeout+.
  reaping_frequency = spec.config.fetch(:reaping_frequency, 60)
  @reaper = Reaper.new(self, reaping_frequency && reaping_frequency.to_f)
  @reaper.run
end
# called from activerecord-5.2.3/lib/active_record/connection_adapters/abstract/query_cache.rb:30
/Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:257
MonitorMixin#mon_initialize
def mon_initialize
  if defined?(@mon_mutex) && @mon_mutex_owner_object_id == object_id
    raise ThreadError, "already initialized"
  end
  @mon_mutex = Thread::Mutex.new
  @mon_mutex_owner_object_id = object_id
  @mon_owner = nil
  @mon_count = 0
end
# called from /Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:252
concurrent-ruby-1.1.5/lib/concurrent/map.rb:118
Concurrent::Map#initialize
def initialize(options = nil, &block)
  if options.kind_of?(::Hash)
    validate_options_hash!(options)
  else
    options = nil
  end

  super(options)
  @default_proc = block
end
# called from activerecord-5.2.3/lib/active_record/connection_adapters/abstract/query_cache.rb:31
concurrent-ruby-1.1.5/lib/concurrent/collection/map/mri_map_backend.rb:12
Concurrent::Collection::MriMapBackend#initialize
def initialize(options = nil, &block)
  if options.kind_of?(::Hash)
    validate_options_hash!(options)
  else
    options = nil
  end

  super(options)
  @default_proc = block
end
# called from concurrent-ruby-1.1.5/lib/concurrent/map.rb:125
concurrent-ruby-1.1.5/lib/concurrent/collection/map/non_concurrent_map_backend.rb:15
Concurrent::Collection::NonConcurrentMapBackend#initialize
def initialize(options = nil, &block)
  if options.kind_of?(::Hash)
    validate_options_hash!(options)
  else
    options = nil
  end

  super(options)
  @default_proc = block
end
# called from concurrent-ruby-1.1.5/lib/concurrent/collection/map/mri_map_backend.rb:13
concurrent-ruby-1.1.5/lib/concurrent/map.rb:118
Concurrent::Map#initialize
def initialize(options = nil, &block)
  if options.kind_of?(::Hash)
    validate_options_hash!(options)
  else
    options = nil
  end

  super(options)
  @default_proc = block
end
# called from activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:345
concurrent-ruby-1.1.5/lib/concurrent/map.rb:328
Concurrent::Map#validate_options_hash!
def validate_options_hash!(options)
  if (initial_capacity = options[:initial_capacity]) && (!initial_capacity.kind_of?(Integer) || initial_capacity < 0)
    raise ArgumentError, ":initial_capacity must be a positive Integer"
  end
  if (load_factor = options[:load_factor]) && (!load_factor.kind_of?(Numeric) || load_factor <= 0 || load_factor > 1)
    raise ArgumentError, ":load_factor must be a number between 0 and 1"
  end
end
# called from concurrent-ruby-1.1.5/lib/concurrent/map.rb:120
concurrent-ruby-1.1.5/lib/concurrent/collection/map/mri_map_backend.rb:12
Concurrent::Collection::MriMapBackend#initialize
def initialize(options = nil, &block)
  if options.kind_of?(::Hash)
    validate_options_hash!(options)
  else
    options = nil
  end

  super(options)
  @default_proc = block
end
# called from concurrent-ruby-1.1.5/lib/concurrent/map.rb:125
concurrent-ruby-1.1.5/lib/concurrent/collection/map/non_concurrent_map_backend.rb:15
Concurrent::Collection::NonConcurrentMapBackend#initialize
def initialize(options = nil, &block)
  if options.kind_of?(::Hash)
    validate_options_hash!(options)
  else
    options = nil
  end

  super(options)
  @default_proc = block
end
# called from concurrent-ruby-1.1.5/lib/concurrent/collection/map/mri_map_backend.rb:13
activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:86
ActiveRecord::ConnectionAdapters::ConnectionPool::Queue#initialize
def initialize(lock = Monitor.new)
  @lock = lock
  @cond = @lock.new_cond
  @num_waiting = 0
  @queue = []
end
# called from activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:357
/Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:241
MonitorMixin#new_cond
def new_cond
  return ConditionVariable.new(self)
end
# called from activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:88
/Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:156
MonitorMixin::ConditionVariable#initialize
def initialize(monitor)
  @monitor = monitor
  @cond = Thread::ConditionVariable.new
end
# called from /Users/yhirano/.rbenv/versions/2.6.3/lib/ruby/2.6.0/monitor.rb:242
activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:292
ActiveRecord::ConnectionAdapters::ConnectionPool::Reaper#initialize
def initialize(pool, frequency)
  @pool      = pool
  @frequency = frequency
end
# called from activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:364
activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:297
ActiveRecord::ConnectionAdapters::ConnectionPool::Reaper#run
def run
  return unless frequency && frequency > 0
  Thread.new(frequency, pool) { |t, p|
    loop do
      sleep t
      p.reap
      p.flush
    end
  }
end
# called from activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:365
concurrent-ruby-1.1.5/lib/concurrent/collection/map/mri_map_backend.rb:17
Concurrent::Collection::MriMapBackend#[]=
def []=(key, value)
  @write_lock.synchronize { super }
end
# called from activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:970
concurrent-ruby-1.1.5/lib/concurrent/collection/map/non_concurrent_map_backend.rb:23
Concurrent::Collection::NonConcurrentMapBackend#[]=
def []=(key, value)
  @write_lock.synchronize { super }
end
# called from concurrent-ruby-1.1.5/lib/concurrent/collection/map/mri_map_backend.rb:18
activesupport-5.2.3/lib/active_support/notifications/instrumenter.rb:43
ActiveSupport::Notifications::Instrumenter#finish_with_state
def finish_with_state(listeners_state, name, payload)
  @notifier.finish name, @id, payload, listeners_state
end
# called from activesupport-5.2.3/lib/active_support/notifications/instrumenter.rb:29
activesupport-5.2.3/lib/active_support/notifications/fanout.rb:47
ActiveSupport::Notifications::Fanout#finish
def finish(name, id, payload, listeners = listeners_for(name))
  listeners.each { |s| s.finish(name, id, payload) }
end
# called from activesupport-5.2.3/lib/active_support/notifications/instrumenter.rb:44
activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:1058
ActiveRecord::ConnectionAdapters::ConnectionHandler#owner_to_pool
def owner_to_pool
  @owner_to_pool[Process.pid]
end
# called from activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:973
concurrent-ruby-1.1.5/lib/concurrent/map.rb:132
Concurrent::Map#[]
def [](key)
  if value = super # non-falsy value is an existing mapping, return it right away
    value
    # re-check is done with get_or_default(key, NULL) instead of a simple !key?(key) in order to avoid a race condition, whereby by the time the current thread gets to the key?(key) call
    # a key => value mapping might have already been created by a different thread (key?(key) would then return true, this elsif branch wouldn't be taken and an incorrent +nil+ value
    # would be returned)
    # note: nil == value check is not technically necessary
  elsif @default_proc && nil == value && NULL == (value = get_or_default(key, NULL))
    @default_proc.call(self, key)
  else
    value
  end
end
# called from activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:1059
concurrent-ruby-1.1.5/lib/concurrent/collection/map/non_concurrent_map_backend.rb:19
Concurrent::Collection::NonConcurrentMapBackend#[]
def [](key)
  if value = super # non-falsy value is an existing mapping, return it right away
    value
    # re-check is done with get_or_default(key, NULL) instead of a simple !key?(key) in order to avoid a race condition, whereby by the time the current thread gets to the key?(key) call
    # a key => value mapping might have already been created by a different thread (key?(key) would then return true, this elsif branch wouldn't be taken and an incorrent +nil+ value
    # would be returned)
    # note: nil == value check is not technically necessary
  elsif @default_proc && nil == value && NULL == (value = get_or_default(key, NULL))
    @default_proc.call(self, key)
  else
    value
  end
end
# called from concurrent-ruby-1.1.5/lib/concurrent/map.rb:133
concurrent-ruby-1.1.5/lib/concurrent/map.rb:132
Concurrent::Map#[]
def [](key)
  if value = super # non-falsy value is an existing mapping, return it right away
    value
    # re-check is done with get_or_default(key, NULL) instead of a simple !key?(key) in order to avoid a race condition, whereby by the time the current thread gets to the key?(key) call
    # a key => value mapping might have already been created by a different thread (key?(key) would then return true, this elsif branch wouldn't be taken and an incorrent +nil+ value
    # would be returned)
    # note: nil == value check is not technically necessary
  elsif @default_proc && nil == value && NULL == (value = get_or_default(key, NULL))
    @default_proc.call(self, key)
  else
    value
  end
end
# called from activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:973
concurrent-ruby-1.1.5/lib/concurrent/collection/map/non_concurrent_map_backend.rb:19
Concurrent::Collection::NonConcurrentMapBackend#[]
def [](key)
  if value = super # non-falsy value is an existing mapping, return it right away
    value
    # re-check is done with get_or_default(key, NULL) instead of a simple !key?(key) in order to avoid a race condition, whereby by the time the current thread gets to the key?(key) call
    # a key => value mapping might have already been created by a different thread (key?(key) would then return true, this elsif branch wouldn't be taken and an incorrent +nil+ value
    # would be returned)
    # note: nil == value check is not technically necessary
  elsif @default_proc && nil == value && NULL == (value = get_or_default(key, NULL))
    @default_proc.call(self, key)
  else
    value
  end
end
# called from concurrent-ruby-1.1.5/lib/concurrent/map.rb:133
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment