Skip to content

Commit 3235374

Browse files
committed
Merge pull request #47 from thijsdezoete/master
Add: Optional socket connect parameter
2 parents 949cd2b + 6eac0b9 commit 3235374

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

docs/mysql-statsd.conf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ include_hostname = true
1010

1111
[mysql]
1212
; specify 0 for infinite connection retries
13-
max_reconnect = 30
13+
max_reconnect = 5
1414
host = localhost
1515
username = root
1616
password =
17+
socket =
1718
stats_types = status,variables,innodb,slave
1819
query_variables = SHOW GLOBAL VARIABLES
1920
interval_variables = 10000

mysql_statsd/thread_mysql.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@ def __init__(self, *args, **kwargs):
2828
def configure(self, config_dict):
2929
self.host = config_dict.get('mysql').get('host', 'localhost')
3030
self.port = config_dict.get('mysql').get('port', 3306)
31+
self.socket = config_dict.get('mysql').get('socket', None)
3132

3233
self.username = config_dict.get('mysql').get('username', 'root')
3334
self.password = config_dict.get('mysql').get('password', '')
3435

35-
self.max_reconnect = int(config_dict.get('mysql').get('max_reconnect', 30))
36+
self.max_reconnect = int(config_dict.get('mysql').get('max_reconnect', 5))
3637
self.max_recovery = int(config_dict.get('mysql').get('max_recovery', 10))
3738

3839
#Set the stats checks for MySQL
@@ -56,17 +57,21 @@ def configure(self, config_dict):
5657
def setup_connection(self):
5758
connection_attempt = 0
5859

59-
while self.max_reconnect == 0 or connection_attempt <= self.max_reconnect:
60-
try:
61-
self.connection = mdb.connect(host=self.host, user=self.username, port=self.port, passwd=self.password)
62-
return self.connection
63-
except Exception:
64-
pass
65-
66-
# If we got here, connection failed
67-
connection_attempt += 1
68-
time.sleep(self.reconnect_delay)
69-
print('Attempting reconnect #{0}...'.format(connection_attempt))
60+
while self.max_reconnect == 0 or connection_attempt < self.max_reconnect:
61+
try:
62+
if self.socket:
63+
self.connection = mdb.connect(user=self.username, unix_socket=self.socket, passwd=self.password)
64+
else:
65+
self.connection = mdb.connect(host=self.host, user=self.username, port=self.port, passwd=self.password)
66+
67+
return self.connection
68+
except Exception:
69+
pass
70+
71+
# If we got here, connection failed
72+
connection_attempt += 1
73+
print('Attempting reconnect #{0}...'.format(connection_attempt))
74+
time.sleep(self.reconnect_delay)
7075

7176
# If we get out of the while loop, we've passed max_reconnect
7277
raise ThreadMySQLMaxReconnectException

0 commit comments

Comments
 (0)