1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activerecord/test/support/connection.rb
eileencodes d9639a211f
Allow async executor to be configurable
This is a followup/alternative to #41406. This change wouldn't work for
GitHub because we intend to implement an executor for each database and
use the database configuration to set the `min_threads` and
`max_threads` for each one.

The changes here borrow from #41406 by implementing an
`Concurrent::ImmediateExecutor` by default. Otherwise applications have
the option of having one global thread pool that is used by all connections
or a thread pool for each connection. A global thread pool can set with
`config.active_record.async_query_executor = :global_thread_pool`. This
will create a single `Concurrent::ThreadPoolExecutor` for applications
to utilize. By default the concurrency is 4, but it can be changed for the
`global_thread_pool` by setting `global_executor_concurrency` to another
number. If applications want to use a thread pool per database
connection they can set `config.active_record.async_query_executor =
:multi_thread_pool`. This will create a `Concurrent::ThreadPoolExecutor`
for each database connection and set the `min_threads` and `max_threads`
by their configuration values or the defaults.

I've also moved the async tests out of the adapter test and into their
own tests and added tests for all the new functionality. This change
would allow us at GitHub to control threads per database and per
writer/reader or other apps to use one global executor. The immediate
executor allows apps to no-op by default.

Took the immediate executor idea from Jean's PR.
Co-authored-by: Jean Boussier <jean.boussier@gmail.com>
2021-02-19 12:34:57 -05:00

30 lines
961 B
Ruby

# frozen_string_literal: true
require "active_support/logger"
require "models/college"
require "models/course"
require "models/professor"
require "models/other_dog"
module ARTest
def self.connection_name
ENV["ARCONN"] || config["default_connection"]
end
def self.test_configuration_hashes
config.fetch("connections").fetch(connection_name) do
puts "Connection #{connection_name.inspect} not found. Available connections: #{config['connections'].keys.join(', ')}"
exit 1
end
end
def self.connect
ActiveRecord::Base.legacy_connection_handling = false
ActiveRecord::Base.async_query_executor = :global_thread_pool
puts "Using #{connection_name}"
ActiveRecord::Base.logger = ActiveSupport::Logger.new("debug.log", 0, 100 * 1024 * 1024)
ActiveRecord::Base.configurations = test_configuration_hashes
ActiveRecord::Base.establish_connection :arunit
ARUnit2Model.establish_connection :arunit2
end
end