Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added custom configuration to slave connections #113

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/lhm/invoker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ def normalize_options(options)
end

if options[:throttler]
options[:throttler] = Throttler::Factory.create_throttler(options[:throttler])
throttler_options = options[:throttler_options] || {}
options[:throttler] = Throttler::Factory.create_throttler(options[:throttler], throttler_options)
else
options[:throttler] = Lhm.throttler
end
Expand Down
14 changes: 9 additions & 5 deletions lib/lhm/throttler/slave_lag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def initialize(options = {})
@timeout_seconds = INITIAL_TIMEOUT
@stride = options[:stride] || DEFAULT_STRIDE
@allowed_lag = options[:allowed_lag] || DEFAULT_MAX_ALLOWED_LAG
@slave_connections = {}
@slave_connection = options[:slave_connection]
end

def execute
Expand Down Expand Up @@ -74,10 +74,14 @@ def slave_lag(slave)
end

def slave_connection(slave)
adapter_method = defined?(Mysql2) ? 'mysql2_connection' : 'mysql_connection'
config = ActiveRecord::Base.connection_pool.spec.config.dup
config[:host] = slave
ActiveRecord::Base.send(adapter_method, config)
unless @slave_connection
adapter_method = defined?(Mysql2) ? 'mysql2_connection' : 'mysql_connection'
config = ActiveRecord::Base.connection_pool.spec.config.dup
config[:host] = slave
ActiveRecord::Base.send(adapter_method, config)
else
@slave_connection.call(slave)
end
end

# This method fetch the Seconds_Behind_Master, when exec_query is no available, on AR 2.3.
Expand Down
13 changes: 13 additions & 0 deletions spec/unit/throttler/slave_lag_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,19 @@ def @throttler.max_current_slave_lag
end
end

describe '#custom_slave_connection' do
describe 'with a custom slave connection' do
before do
@connection = Object.new # just an object to see if the method returns what we return
@throttler = Lhm::Throttler::SlaveLag.new(slave_connection: lambda {|host| @connection})
end

it 'should use the custom slave connection' do
assert_equal(@connection, @throttler.send(:slave_connection, "slave.db.local"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assert_ and friends are normally used sans parentheses

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how does this assertion warranty that the connection is custom?

end
end
end

describe '#slave_hosts' do
describe 'with no slaves' do
before do
Expand Down