Skip to content

Commit

Permalink
CICO-55340 Store slave connections in instance variable to avoid repe…
Browse files Browse the repository at this point in the history
…ated new connections
  • Loading branch information
Jon committed Jul 25, 2018
1 parent c8b70c1 commit 686823b
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/lhm/throttler/slave_lag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ def initialize(options = {})
@timeout_seconds = INITIAL_TIMEOUT
@stride = options[:stride] || DEFAULT_STRIDE
@allowed_lag = options[:allowed_lag] || DEFAULT_MAX_ALLOWED_LAG
@slave_connections = {}
end

def execute
Expand Down Expand Up @@ -43,6 +42,10 @@ def throttle_seconds
end
end

def slave_connections
@slave_connections ||= slave_hosts.map { |slave_host| slave_connection(slave_host) }
end

def slave_hosts
slaves = get_slaves.map { |slave_host| slave_host.partition(':')[0] }
.delete_if { |slave| slave == 'localhost' || slave == '127.0.0.1' }
Expand All @@ -55,13 +58,12 @@ def get_slaves
end

def max_current_slave_lag
max = slave_hosts.map { |slave| slave_lag(slave) }.flatten.push(0).max
max = slave_connections.map { |slave_connection| slave_lag(slave_connection) }.flatten.push(0).max
Lhm.logger.info "Max current slave lag: #{max}"
max
end

def slave_lag(slave)
conn = slave_connection(slave)
def slave_lag(conn)
if conn.respond_to?(:exec_query)
result = conn.exec_query(SQL_SELECT_MAX_SLAVE_LAG)
result.map { |row| row['Seconds_Behind_Master'].to_i }
Expand Down

0 comments on commit 686823b

Please sign in to comment.