From cf521c95761540f273804d23a1150dbb0af4e63b Mon Sep 17 00:00:00 2001
From: Yorick Peterse <yorickpeterse@gmail.com>
Date: Wed, 22 Feb 2017 15:43:01 +0100
Subject: [PATCH] Allow setting of a custom connection pool host

This allows you to set a custom host when calling
Gitlab::Database.create_connection_pool. This is necessary for load
balancing as in this case we want to inherit all settings except for the
hostname.
---
 lib/gitlab/database.rb           |  7 ++++++-
 spec/lib/gitlab/database_spec.rb | 21 ++++++++++++++++++---
 2 files changed, 24 insertions(+), 4 deletions(-)

diff --git a/lib/gitlab/database.rb b/lib/gitlab/database.rb
index a47d7e98a62..d160cadc2d0 100644
--- a/lib/gitlab/database.rb
+++ b/lib/gitlab/database.rb
@@ -79,11 +79,16 @@ module Gitlab
       end
     end
 
-    def self.create_connection_pool(pool_size)
+    # pool_size - The size of the DB pool.
+    # host - An optional host name to use instead of the default one.
+    def self.create_connection_pool(pool_size, host = nil)
       # 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)
+      env_config['host'] = host if host
+
       config = original_config.merge(env => env_config)
 
       spec =
diff --git a/spec/lib/gitlab/database_spec.rb b/spec/lib/gitlab/database_spec.rb
index f01c42aff91..edd01d032c8 100644
--- a/spec/lib/gitlab/database_spec.rb
+++ b/spec/lib/gitlab/database_spec.rb
@@ -119,9 +119,24 @@ describe Gitlab::Database, lib: true do
     it 'creates a new connection pool with specific pool size' do
       pool = described_class.create_connection_pool(5)
 
-      expect(pool)
-        .to be_kind_of(ActiveRecord::ConnectionAdapters::ConnectionPool)
-      expect(pool.spec.config[:pool]).to eq(5)
+      begin
+        expect(pool)
+          .to be_kind_of(ActiveRecord::ConnectionAdapters::ConnectionPool)
+
+        expect(pool.spec.config[:pool]).to eq(5)
+      ensure
+        pool.disconnect!
+      end
+    end
+
+    it 'allows setting of a custom hostname' do
+      pool = described_class.create_connection_pool(5, '127.0.0.1')
+
+      begin
+        expect(pool.spec.config[:host]).to eq('127.0.0.1')
+      ensure
+        pool.disconnect!
+      end
     end
   end
 
-- 
GitLab