Author: ahristov
Date: 2007-08-22 16:59:54 +0200 (Wed, 22 Aug 2007)
New Revision: 941
Modified:
trunk/php5/ext/mysqli/mysqli.c
trunk/php5/ext/mysqli/mysqli_nonapi.c
trunk/php5/ext/mysqli/php_mysqli_structs.h
Log:
multiple pconns, port to PHP5
Modified: trunk/php5/ext/mysqli/mysqli.c
- trunk/php5/ext/mysqli/mysqli.c 2007-08-22 14:59:22 UTC (rev 940)
+++ trunk/php5/ext/mysqli/mysqli.c 2007-08-22 14:59:54 UTC (rev 941)
@@ -70,18 +70,55 @@
static int le_pmysqli;
-/* Destructor for Persistent Connections */
+static int php_mysqli_persistent_on_rshut(zend_rsrc_list_entry *le TSRMLS_DC)
+{
+ if (le->type == le_pmysqli) {
+ mysqli_plist_entry *plist = (mysqli_plist_entry *) le->ptr;
+ HashPosition pos;
+ MYSQL **mysql;
+ ulong idx;
+ dtor_func_t pDestructor = plist->used_links.pDestructor;
+ plist->used_links.pDestructor = NULL; /* Don't call pDestructor now */
+
+ zend_hash_internal_pointer_reset_ex(&plist->used_links, &pos);
+ while (SUCCESS == zend_hash_get_current_data_ex(&plist->used_links, (void **)&mysql, &pos)) {
+ zend_hash_get_current_key_ex(&plist->used_links, NULL, NULL, &idx, FALSE, &pos);
+ zend_hash_next_index_insert(&plist->free_links, mysql, sizeof(MYSQL *), NULL);
+ /* First move forward */
+ zend_hash_move_forward_ex(&plist->used_links, &pos);
+ /* The delete, because del will free memory, but we need it's ->nextItem */
+ zend_hash_index_del(&plist->used_links, idx);
+ }
+
+ /* restore pDestructor, which should be php_mysqli_dtor_p_elements() */
+ plist->used_links.pDestructor = pDestructor;
+ }
+ return ZEND_HASH_APPLY_KEEP;
+}
+
+/* Destructor for mysqli entries in free_links/used_links */
+void php_mysqli_dtor_p_elements(void *data)
+{
+ MYSQL **mysql = (MYSQL **) data;
+ TSRMLS_FETCH();
+#if defined(HAVE_MYSQLND)
+ mysqlnd_end_psession(*mysql);
+#endif
+ MyG(num_persistent)--;
+ mysqli_close(*mysql, MYSQLI_CLOSE_IMPLICIT);
+}
+
ZEND_RSRC_DTOR_FUNC(php_mysqli_dtor)
{
if (rsrc->ptr) {
- MYSQL *mysql = (MYSQL *)rsrc->ptr;
-#if defined(HAVE_MYSQLND)
- mysqlnd_end_psession(mysql);
-#endif
- mysqli_close(mysql, MYSQLI_CLOSE_IMPLICIT);
+ mysqli_plist_entry *plist = (mysqli_plist_entry *) rsrc->ptr;
+ zend_hash_destroy(&plist->free_links);
+ zend_hash_destroy(&plist->used_links);
+ free(plist);
}
}
+
int php_le_pmysqli(void)
{
return le_pmysqli;
@@ -792,6 +829,10 @@
*/
PHP_RSHUTDOWN_FUNCTION(mysqli)
{
+ /* check persistent connections, move used to free */
+ zend_hash_apply(&EG(persistent_list), (apply_func_t) php_mysqli_persistent_on_rshut TSRMLS_CC);
+
+
#if !defined(HAVE_MYSQLND) && defined(ZTS)
mysql_thread_end();
#endif
Modified: trunk/php5/ext/mysqli/mysqli_nonapi.c
- trunk/php5/ext/mysqli/mysqli_nonapi.c 2007-08-22 14:59:22 UTC (rev 940)
+++ trunk/php5/ext/mysqli/mysqli_nonapi.c 2007-08-22 14:59:54 UTC (rev 941)
@@ -47,6 +47,7 @@
char *hash_key = NULL;
zend_bool new_connection = FALSE;
zend_rsrc_list_entry *le;
+ mysqli_plist_entry *plist = NULL;
if (getThis() && !ZEND_NUM_ARGS()) {
RETURN_NULL();
@@ -98,22 +99,57 @@
/* check if we can reuse exisiting connection ... */
if (zend_hash_find(&EG(persistent_list), hash_key, hash_len + 1, (void **)&le) == SUCCESS) {
if (Z_TYPE_P(le) == php_le_pmysqli()) {
- mysql->mysql = (MYSQL *)le->ptr;
+ plist = (mysqli_plist_entry *) le->ptr;
- /* reset variables */
- /* todo: option for ping or change_user */
+ do {
+ if (zend_hash_num_elements(&plist->free_links)) {
+ HashPosition pos;
+ MYSQL *free_mysql;
+ ulong idx;
+
+ zend_hash_internal_pointer_reset_ex(&plist->free_links, &pos);
+ if (SUCCESS != zend_hash_get_current_data_ex(&plist->free_links,
+ (void **)&free_mysql, &pos)) {
+ break;
+ }
+ if (HASH_KEY_IS_LONG != zend_hash_get_current_key_ex(&plist->free_links, NULL,
+ NULL, &idx, FALSE, &pos)) {
+ break;
+ }
+ if (SUCCESS != zend_hash_index_del(&plist->free_links, idx)) {
+ break;
+ }
+ mysql->mysql = free_mysql;
+
+ /* reset variables */
+ /* todo: option for ping or change_user */
#if G0
- if (!mysql_change_user(mysql->mysql, username, passwd, dbname)) {
+ if (!mysql_change_user(mysql->mysql, username, passwd, dbname)) {
+#else
+ if (!mysql_ping(mysql->mysql)) {
#endif
- if (!mysql_ping(mysql->mysql)) {
#ifdef HAVE_MYSQLND
- mysqlnd_restart_psession(mysql->mysql);
+ mysqlnd_restart_psession(mysql->mysql);
#endif
- goto end;
- }
- MyG(num_links)--;
- zend_hash_del(&EG(persistent_list), hash_key, hash_len + 1);
- }
+ if (SUCCESS != zend_hash_next_index_insert(&plist->used_links, &free_mysql,
+ sizeof(MYSQL *), NULL)) {
+ php_mysqli_dtor_p_elements(free_mysql);
+ break;
+ }
+ goto end;
+ }
+ MyG(num_links)--;
+ }
+ } while (0);
+ }
+ } else {
+ zend_rsrc_list_entry le;
+ le.type = php_le_pmysqli();
+ le.ptr = plist = calloc(1, sizeof(mysqli_plist_entry));
+
+ zend_hash_init(&plist->free_links, MAX(10, MyG(max_persistent)), NULL, php_mysqli_dtor_p_elements, 1);
+ zend_hash_init(&plist->used_links, MAX(10, MyG(max_persistent)), NULL, php_mysqli_dtor_p_elements, 1);
+ zend_hash_update(&EG(persistent_list), hash_key, hash_len + 1, (void *)&le, sizeof(le), NULL);
}
}
@@ -179,19 +215,14 @@
/* store persistent connection */
if (persistent && new_connection) {
- zend_rsrc_list_entry le;
-
- le.type = php_le_pmysqli();
- le.ptr = mysql->mysql;
-
/* save persistent connection */
- if (zend_hash_update(&EG(persistent_list), hash_key, hash_len + 1, (void *)&le,
- sizeof(le), NULL) == FAILURE) {
+ if (SUCCESS != zend_hash_next_index_insert(&plist->used_links, &mysql->mysql,
+ sizeof(MYSQL *), NULL)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Can't store persistent connection");
- }
+ }
+ MyG(num_persistent)++;
}
if (persistent) {
- MyG(num_persistent)++;
efree(hash_key);
}
MyG(num_links)++;
@@ -214,7 +245,7 @@
if (persistent) {
efree(hash_key);
}
- RETURN_FALSE;
+ RETVAL_FALSE;
}
/* }}} */
Modified: trunk/php5/ext/mysqli/php_mysqli_structs.h
- trunk/php5/ext/mysqli/php_mysqli_structs.h 2007-08-22 14:59:22 UTC (rev 940)
+++ trunk/php5/ext/mysqli/php_mysqli_structs.h 2007-08-22 14:59:54 UTC (rev 941)
@@ -133,6 +133,11 @@
} mysqli_local_infile;
#endif
+typedef struct {
+ HashTable free_links;
+ HashTable used_links;
+} mysqli_plist_entry;
+
#ifdef PHP_WIN32
#define PHP_MYSQLI_API __declspec(dllexport)
#define MYSQLI_LLU_SPEC "%I64u"
@@ -193,6 +198,7 @@
extern zend_class_entry *mysqli_warning_class_entry;
extern zend_class_entry *mysqli_exception_class_entry;
extern int php_le_pmysqli(void);
+extern void php_mysqli_dtor_p_elements(void *data);
#ifdef HAVE_SPL
extern PHPAPI zend_class_entry *spl_ce_RuntimeException;
--
MySQL Code Commits Mailing List
For list archives:
http://lists.mysql.com/commits
To unsubscribe:
http://lists.mysql.com/commits?unsub=lists@pantek.com
Received on Wed Aug 22 11:00:06 2007