Bläddra i källkod

- using improved connection pool

Jan Janak 20 år sedan
förälder
incheckning
344f101afe
3 ändrade filer med 67 tillägg och 37 borttagningar
  1. 39 9
      modules/db_mysql/dbase.c
  2. 15 18
      modules/db_mysql/my_con.c
  3. 13 10
      modules/db_mysql/my_con.h

+ 39 - 9
modules/db_mysql/dbase.c

@@ -35,10 +35,10 @@
 #include <mysql/errmsg.h>
 #include "../../mem/mem.h"
 #include "../../dprint.h"
+#include "../../db/db_pool.h"
 #include "utils.h"
 #include "val.h"
 #include "my_con.h"
-#include "my_pool.h"
 #include "res.h"
 #include "db_mod.h"
 #include "dbase.h"
@@ -85,7 +85,7 @@ static int submit_query(db_con_t* _h, const char* _s)
 	 * value shouldn't be needed, but it doesn't hurt either, since the loop
 	 * will most of the time stop at the second or sometimes at the third
 	 * iteration.
-     */
+	 */
 	for (i=0; i<(auto_reconnect ? 3 : 1); i++) {
 		if (mysql_query(CON_CONNECTION(_h), _s)==0) {
 			return 0;
@@ -243,8 +243,13 @@ static int print_set(MYSQL* _c, char* _b, int _l, db_key_t* _k, db_val_t* _v, in
  */
 db_con_t* db_init(const char* _url)
 {
+	struct db_id* id;
+	struct my_con* con;
 	db_con_t* res;
 
+	id = 0;
+	res = 0;
+
 	if (!_url) {
 		LOG(L_ERR, "db_init: Invalid parameter value\n");
 		return 0;
@@ -257,15 +262,34 @@ db_con_t* db_init(const char* _url)
 	}
 	memset(res, 0, sizeof(db_con_t) + sizeof(struct my_con*));
 
-	res->tail = (unsigned long)get_connection(_url);
-
-	if (!res->tail) {
-		LOG(L_ERR, "db_init: Could not create a connection\n");
-		pkg_free(res);
-		return 0;
+	id = new_db_id(_url);
+	if (!id) {
+		LOG(L_ERR, "db_init: Cannot parse URL '%s'\n", _url);
+		goto err;
+	}
+
+	     /* Find the connection in the pool */
+	con = (struct my_con*)pool_get(id);
+	if (!con) {
+		DBG("db_init: Connection '%s' not found in pool\n", _url);
+		     /* Not in the pool yet */
+		con = new_connection(id);
+		if (!con) {
+			LOG(L_ERR, "db_init: No memory left\n");
+			goto err;
+		}
+		pool_insert((struct pool_con*)con);
+	} else {
+		DBG("db_init: Connection '%s' found in pool\n", _url);
 	}
 
+	res->tail = (unsigned long)con;
 	return res;
+
+ err:
+	if (id) free_db_id(id);
+	if (res) pkg_free(res);
+	return 0;
 }
 
 
@@ -275,12 +299,18 @@ db_con_t* db_init(const char* _url)
  */
 void db_close(db_con_t* _h)
 {
+	struct pool_con* con;
+
 	if (!_h) {
 		LOG(L_ERR, "db_close: Invalid parameter value\n");
 		return;
 	}
 
-	release_connection((struct my_con*)_h->tail);
+	con = (struct pool_con*)_h->tail;
+	if (pool_remove(con) != 0) {
+		free_connection((struct my_con*)con);
+	}
+
 	pkg_free(_h);
 }
 

+ 15 - 18
modules/db_mysql/my_con.c

@@ -1,7 +1,6 @@
 /* 
  * $Id$
  *
- *
  * Copyright (C) 2001-2004 iptel.org
  *
  * This file is part of ser, a free SIP server.
@@ -26,20 +25,20 @@
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
-#include <string.h>
-#include <time.h>
 #include "my_con.h"
 #include "../../mem/mem.h"
 #include "../../dprint.h"
 #include "../../ut.h"
 #include "utils.h"
+#include <string.h>
+#include <time.h>
 
 
 /*
  * Create a new connection structure,
  * open the MySQL connection and set reference count to 1
  */
-struct my_con* new_connection(struct my_id* id)
+struct my_con* new_connection(struct db_id* id)
 {
 	struct my_con* ptr;
 
@@ -66,23 +65,23 @@ struct my_con* new_connection(struct my_id* id)
 	mysql_init(ptr->con);
 
 	if (id->port) {
-		DBG("new_connection: Opening MySQL connection: mysql://%.*s:%.*s@%.*s:%d/%.*s\n",
-		    id->username.len, ZSW(id->username.s),
-		    id->password.len, ZSW(id->password.s),
-		    id->host.len, ZSW(id->host.s),
+		DBG("new_connection: Opening MySQL connection: mysql://%s:%s@%s:%d/%s\n",
+		    ZSW(id->username),
+		    ZSW(id->password),
+		    ZSW(id->host),
 		    id->port,
-		    id->database.len, ZSW(id->database.s)
+		    ZSW(id->database)
 		    );
 	} else {
-		DBG("new_connection: Opening MySQL connection: mysql://%.*s:%.*s@%.*s/%.*s\n",
-		    id->username.len, ZSW(id->username.s),
-		    id->password.len, ZSW(id->password.s),
-		    id->host.len, ZSW(id->host.s),
-		    id->database.len, ZSW(id->database.s)
+		DBG("new_connection: Opening MySQL connection: mysql://%s:%s@%s/%s\n",
+		    ZSW(id->username),
+		    ZSW(id->password),
+		    ZSW(id->host),
+		    ZSW(id->database)
 		    );
 	}
 
-	if (!mysql_real_connect(ptr->con, id->host.s, id->username.s, id->password.s, id->database.s, id->port, 0, 0)) {
+	if (!mysql_real_connect(ptr->con, id->host, id->username, id->password, id->database, id->port, 0, 0)) {
 		LOG(L_ERR, "new_connection: %s\n", mysql_error(ptr->con));
 		mysql_close(ptr->con);
 		goto err;
@@ -94,9 +93,7 @@ struct my_con* new_connection(struct my_id* id)
 
 
 	ptr->timestamp = time(0);
-
 	ptr->id = id;
-	
 	return ptr;
 
  err:
@@ -113,7 +110,7 @@ void free_connection(struct my_con* con)
 {
 	if (!con) return;
 	if (con->res) mysql_free_result(con->res);
-	if (con->id) free_my_id(con->id);
+	if (con->id) free_db_id(con->id);
 	if (con->con) {
 		mysql_close(con->con);
 		pkg_free(con->con);

+ 13 - 10
modules/db_mysql/my_con.h

@@ -1,7 +1,6 @@
 /* 
  * $Id$
  *
- *
  * Copyright (C) 2001-2003 FhG Fokus
  *
  * This file is part of ser, a free SIP server.
@@ -29,18 +28,22 @@
 #ifndef MY_CON_H
 #define MY_CON_H
 
+#include "../../db/db_pool.h"
+#include "../../db/db_id.h"
+
 #include <time.h>
 #include <mysql/mysql.h>
-#include "my_id.h"
+
 
 struct my_con {
-	struct my_id* id;    /* Connection identifier */
-	int ref;             /* Reference count */
-	MYSQL_RES* res;      /* Actual result */
-	MYSQL* con;          /* Connection representation */
-	MYSQL_ROW row;       /* Actual row in the result */
-	time_t timestamp;    /* Timestamp of last query */
-	struct my_con* next; /* Next connection in the pool */
+	struct db_id* id;        /* Connection identifier */
+	unsigned int ref;        /* Reference count */
+	struct pool_con* next;   /* Next connection in the pool */
+
+	MYSQL_RES* res;          /* Actual result */
+	MYSQL* con;              /* Connection representation */
+	MYSQL_ROW row;           /* Actual row in the result */
+	time_t timestamp;        /* Timestamp of last query */
 };
 
 
@@ -57,7 +60,7 @@ struct my_con {
  * Create a new connection structure,
  * open the MySQL connection and set reference count to 1
  */
-struct my_con* new_connection(struct my_id* id);
+struct my_con* new_connection(struct db_id* id);
 
 
 /*