Skip to content

Commit

Permalink
feat(worker): scale connection pool with worker threads
Browse files Browse the repository at this point in the history
This will automatically increase the DB connection pool size if the number of threads needed in a worker is less than the maximum pool size configured.
  • Loading branch information
adamcooke committed Mar 18, 2024
1 parent 7e2accc commit ea542a0
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
18 changes: 18 additions & 0 deletions app/lib/worker/process.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def initialize(thread_count: Postal::Config.worker.threads,
def run
logger.tagged(component: "worker") do
setup_traps
ensure_connection_pool_size_is_suitable
start_work_threads
start_tasks_thread
wait_for_threads
Expand Down Expand Up @@ -96,6 +97,23 @@ def shutdown_after_wait?(wait_time)
@exit_pipe_read.wait_readable(wait_time) ? true : false
end

# Ensure that the connection pool is big enough for the number of threads
# configured.
#
# @return [void]
def ensure_connection_pool_size_is_suitable
current_pool_size = ActiveRecord::Base.connection_pool.size
desired_pool_size = @thread_count + 3

return if current_pool_size >= desired_pool_size

logger.warn "number of worker threads (#{@thread_count}) is more " \
"than the db connection pool size (#{current_pool_size}+3), " \
"increasing connection pool size to #{desired_pool_size}"

Postal.change_database_connection_pool_size(desired_pool_size)
end

# Wait for all threads to complete
#
# @return [void]
Expand Down
17 changes: 17 additions & 0 deletions lib/postal/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ module Postal

class << self

attr_writer :current_process_type

# Return the path to the config file
#
# @return [String]
Expand Down Expand Up @@ -136,6 +138,21 @@ def graylog_logging_destination
end
end

# Change the connection pool size to the given size.
#
# @param new_size [Integer]
# @return [void]
def change_database_connection_pool_size(new_size)
ActiveRecord::Base.connection_pool.disconnect!

config = ActiveRecord::Base.configurations
.configs_for(env_name: Rails.env)
.first
.configuration_hash

ActiveRecord::Base.establish_connection(config.merge(pool: new_size))
end

end

Config = initialize_config
Expand Down
6 changes: 6 additions & 0 deletions spec/lib/postal_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,10 @@
expect(Postal.signer.private_key.to_pem).to eq OpenSSL::PKey::RSA.new(File.read(Postal::Config.postal.signing_key_path)).to_pem
end
end

describe "#change_database_connection_pool_size" do
it "changes the connection pool size" do
expect { Postal.change_database_connection_pool_size(8) }.to change { ActiveRecord::Base.connection_pool.size }.from(5).to(8)
end
end
end

0 comments on commit ea542a0

Please sign in to comment.