diff options
author | Lin Jen-Shin <godfat@godfat.org> | 2017-02-13 16:45:26 +0300 |
---|---|---|
committer | Lin Jen-Shin <godfat@godfat.org> | 2017-02-13 19:35:25 +0300 |
commit | 136dc79433295aded9ecabb15aae2dc1e228b903 (patch) | |
tree | 9c1e7fffafd0901103363b2cada735fe19012138 /lib/gitlab/database.rb | |
parent | 79e8e6134f89c4a09a422ca128a7579d844f040c (diff) |
Have some simple way to create connection pool
Diffstat (limited to 'lib/gitlab/database.rb')
-rw-r--r-- | lib/gitlab/database.rb | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/lib/gitlab/database.rb b/lib/gitlab/database.rb index dc2537d36aa..e6612bc3aad 100644 --- a/lib/gitlab/database.rb +++ b/lib/gitlab/database.rb @@ -69,6 +69,30 @@ module Gitlab end end + def self.with_connection_pool(pool_size) + pool = create_connection_pool(pool_size) + + yield(pool) + + ensure + pool.disconnect! + end + + def self.create_connection_pool(pool_size) + # See activerecord-4.2.7.1/lib/active_record/connection_adapters/connection_specification.rb + env = Rails.env + original_config = ActiveRecord::Base.configurations + env_config = original_config[env].merge('pool' => pool_size) + config = original_config.merge(env => env_config) + + spec = + ActiveRecord:: + ConnectionAdapters:: + ConnectionSpecification::Resolver.new(config).spec(env.to_sym) + + ActiveRecord::ConnectionAdapters::ConnectionPool.new(spec) + end + def self.connection ActiveRecord::Base.connection end |