From 686823b4fdc22e1578ee64400d84252333355858 Mon Sep 17 00:00:00 2001 From: Jon Date: Wed, 25 Jul 2018 11:04:06 -0400 Subject: [PATCH] CICO-55340 Store slave connections in instance variable to avoid repeated new connections --- lib/lhm/throttler/slave_lag.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/lhm/throttler/slave_lag.rb b/lib/lhm/throttler/slave_lag.rb index 425c7d97..6ad427a6 100644 --- a/lib/lhm/throttler/slave_lag.rb +++ b/lib/lhm/throttler/slave_lag.rb @@ -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 @@ -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' } @@ -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 }