Jelajahi Sumber

Added the following features from Andrei:
- connect_timeout parameter which allows to limit connection timeouts to
mysql server
- send_timeout parameter which can limit the time the client library spends
sending data to the server
- receive_timeout parameter which can limit the time the library spends
waiting for the data from the server
- enable reconnect for mysql >= 5.0.13

Jan Janak 18 tahun lalu
induk
melakukan
1bc0f4e48c
3 mengubah file dengan 77 tambahan dan 0 penghapusan
  1. 35 0
      modules/db_mysql/my_con.c
  2. 36 0
      modules/db_mysql/mysql_mod.c
  3. 6 0
      modules/db_mysql/mysql_mod.h

+ 35 - 0
modules/db_mysql/my_con.c

@@ -31,6 +31,7 @@
 #include "../../ut.h"
 #include <string.h>
 #include <time.h>
+#include "mysql_mod.h"
 #include "my_uri.h"
 #include "my_con.h"
 
@@ -57,6 +58,10 @@ static int my_con_connect(db_con_t* con)
 {
 	struct my_con* mcon;
 	struct my_uri* muri;
+#if MYSQL_VERSION_ID >= 50013 
+	my_bool my_auto_reconnect;
+#endif
+
 
 	mcon = DB_GET_PAYLOAD(con);
 	muri = DB_GET_PAYLOAD(con->uri);
@@ -68,6 +73,36 @@ static int my_con_connect(db_con_t* con)
 		con->uri->scheme.len, ZSW(con->uri->scheme.s),
 		con->uri->body.len, ZSW(con->uri->body.s));
 
+#if MYSQL_VERSION_ID >= 50013 
+	my_auto_reconnect = 1;
+	if (my_client_ver >= 50013) {
+		if (mysql_options(mcon->con, MYSQL_OPT_RECONNECT , 
+						  (char*)&my_auto_reconnect))
+			WARN("mysql: failed to set MYSQL_OPT_RECONNECT\n");
+	}
+#endif
+	if (my_connect_to) {
+		if (mysql_options(mcon->con, MYSQL_OPT_CONNECT_TIMEOUT, 
+						  (char*)&my_connect_to))
+			WARN("mysql: failed to set MYSQL_OPT_CONNECT_TIMEOUT\n");
+	}
+#if MYSQL_VERSION_ID >= 40101 
+	if ((my_client_ver >= 50025) || 
+		((my_client_ver >= 40122) && 
+		 (my_client_ver < 50000))) {
+		if (my_send_to) {
+			if (mysql_options(mcon->con, MYSQL_OPT_WRITE_TIMEOUT , 
+							  (char*)&my_send_to))
+				WARN("mysql: failed to set MYSQL_OPT_WRITE_TIMEOUT\n");
+		}
+		if (my_recv_to){
+			if (mysql_options(mcon->con, MYSQL_OPT_READ_TIMEOUT , 
+							  (char*)&my_recv_to))
+				WARN("mysql: failed to set MYSQL_OPT_READ_TIMEOUT\n");
+		}
+	}
+#endif
+	
 	if (!mysql_real_connect(mcon->con, muri->host, muri->username, 
 							muri->password, muri->database, muri->port, 0, 0)) {
 		LOG(L_ERR, "my_con_connect: %s\n", mysql_error(mcon->con));

+ 36 - 0
modules/db_mysql/mysql_mod.c

@@ -44,6 +44,14 @@
 
 int ping_interval = 5 * 60; /* Default is 5 minutes */
 int auto_reconnect = 1;     /* Default is enabled */
+unsigned int my_connect_to = 2; /* 2 s by default */
+unsigned int my_send_to = 0; /*  enabled only for mysql >= 5.25  */
+unsigned int my_recv_to = 0; /* enabled only for mysql >= 5.25 */
+
+unsigned long my_client_ver = 0;
+
+#define DEFAULT_MY_SEND_TO  2   /* in seconds */
+#define DEFAULT_MY_RECV_TO  4   /* in seconds */
 
 static int mysql_mod_init(void);
 
@@ -76,6 +84,9 @@ static cmd_export_t cmds[] = {
 static param_export_t params[] = {
 	{"ping_interval", PARAM_INT, &ping_interval},
 	{"auto_reconnect", PARAM_INT, &auto_reconnect},
+	{"connect_timeout", PARAM_INT, &my_connect_to},
+	{"send_timeout", PARAM_INT, &my_send_to},
+	{"receive_timeout", PARAM_INT, &my_recv_to},
 	{0, 0, 0}
 };
 
@@ -95,5 +106,30 @@ struct module_exports exports = {
 
 static int mysql_mod_init(void)
 {
+#if MYSQL_VERSION_ID >= 40101
+	my_client_ver = mysql_get_client_version();
+	if ((my_client_ver >= 50025) || 
+		((my_client_ver >= 40122) && 
+		 (my_client_ver < 50000))) {
+		if (my_send_to == 0) {
+			my_send_to= DEFAULT_MY_SEND_TO;
+		}
+		if (my_recv_to == 0) {
+			my_recv_to= DEFAULT_MY_RECV_TO;
+		}
+	} else if (my_recv_to || my_send_to) {
+		LOG(L_WARN, "WARNING: mysql send or received timeout set, but "
+			" not supported by the installed mysql client library"
+			" (needed at least 4.1.22 or 5.0.25, but installed %ld)\n",
+			my_client_ver);
+	}
+#else
+	if (my_recv_to || my_send_to) {
+		LOG(L_WARN, "WARNING: mysql send or received timeout set, but "
+			" not supported by the mysql client library used to compile"
+			" the mysql module (needed at least 4.1.1 but "
+			" compiled against %ld)\n", MYSQL_VERSION_ID);
+	}
+#endif
 	return 0;
 }

+ 6 - 0
modules/db_mysql/mysql_mod.h

@@ -39,4 +39,10 @@
 extern int ping_interval;
 extern int auto_reconnect;
 
+extern unsigned int my_connect_to;
+extern unsigned int my_send_to;
+extern unsigned int my_recv_to;
+
+extern unsigned long my_client_ver;
+
 #endif /* _MYSQL_MOD_H */