cvs: pear /PECL/session_pgsql README config.m4 package.xml session_pgsql.c session_pgsql.h
| From: | Yasuo Ohgaki | Date: | Fri, 17 Jan 2003 10:00:49 +0000 |
| Subject: | cvs: pear /PECL/session_pgsql README config.m4 package.xml session_pgsql.c session_pgsql.h | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-8329@lists.php.net to get a copy of this message | ||
yohgaki Fri Jan 17 05:00:49 2003 EDT
Modified files:
/pear/PECL/session_pgsql README config.m4 package.xml
session_pgsql.c session_pgsql.h
Log:
All known problems are fixed.
Added short circuit option that skips updating session record
Added keep expired option that do not delete expired session record.
(Except session data, all fields are kept)
Added auto connection reset that prevents errors when db servers are
rebooted.
Fixed php.ini entry settings.
Connection to db is made in RINIT which prevents errors when httpd
is loaded before db servers in rc scripts.
Index: pear/PECL/session_pgsql/README diff -u pear/PECL/session_pgsql/README:1.9 pear/PECL/session_pgsql/README:1.10 --- pear/PECL/session_pgsql/README:1.9 Thu Jan 16 02:39:40 2003 +++ pear/PECL/session_pgsql/README Fri Jan 17 05:00:46 2003 @@ -2,7 +2,7 @@ *WARNING* This session save handler is EXPERIMENTAL. Anything may be -changed/deleted/added without notice. It is considered as alpha. +changed/deleted/added without notice. This code is considered as beta. [What is it] @@ -18,6 +18,7 @@ - Automatic db server load balancing where there are multiple PostgreSQL servers. - Automatic session table vacuum. + - Short circuit UPDATE It is design to scale any size of web sites. @@ -53,9 +54,13 @@ Following php.ini directives has special meanings for mod_pgsql -session.save_handler +session.save_handler "pgsql" for session_pgsql session. +session.* + save_path is ignored. Other session.* settings are also used for + session_pgsql. + session_pgsql.db Valid PostgreSQL database connection string. For example, "host=localhost dbname=php_session user=nobody" can @@ -67,19 +72,34 @@ use available db server automatically. NOTE: database name must be "php_session". Other connection options may be changed. + Default is "host=localhost dbname=php_session user=nobody" + +session_pgsql.keep_expired + Keep expired data except session values. Counter, created time, created + IP address, etc is kept when this is set to on. If this option is off + session record is deleted and insert record is created. + Default is off. session_pgsql.use_app_vars Use application level variable ($_APP) or not. Use different session name to change application. i.e. use session_name('AppName') Use of application variable has severe performance penalty. - Default is off + Default is off. session_pgsql.create_table Automatically create session and application variable table if set to on. session_pgsql creates tables, php_session and php_app_vers, in the database. Default is on. +session_pgsql.short_circuit + If short_circuit is set to on, it will skip UPDATE statement when + skipping is possible. Setting error or custom field value or SESSION + array will makde session_pgsql save changes to table, otherwise + it will not update table. Access counter and expire field will not + be updated when update is skipped. + Default is off. + session_pgsql.failover_mode If failover_mode is set to on, it will always use 1st db server and if 1st server fails, it will use next available db server. @@ -88,22 +108,41 @@ db server will not be enabled. Default is off. NOTE: session_pgsql do not mirror session data. It just switch to other database server to continue web services. + Default is off. session_pgsql.gc_interval - How often perform GC in seconds. Default is 3600 seconds. + How often perform GC in seconds. If you set multiple db servers to session_pgsql.db, set only one of your web server to prevent unneeded garbage collections. It is good idea set to 0 (disable) and perform garbage collection by using cron or like in db server if you have multiple web servers accessed to the session db server(s). + Default is 3600 seconds (1 hour). session_pgsql.vacuum_interval - How often perform GC in seconds. Default is 18000 seconds. + How often perform GC in seconds. If you set multiple db servers to session_pgsql.db, set only one of your web server to prevent unneeded vacuum operations. It is good idea set to 0 (disable) and perform vacuum by using cron or like in db server if you have multiple web servers accessed to the session db server(s). + Default is 18000 seconds (5 hours). + +[Functions] +/* {{{ proto array session_pgsql_status(void) + Returns current pgsql save handler status */ +/* {{{ proto bool session_pgsql_reset(void) + Reset connection to session database servsers */ +/* {{{ proto array session_pgsql_info(void) + Returns current session info */ +/* {{{ proto bool session_pgsql_set_field(string value) + Set custom field value */ +/* {{{ proto string session_pgsql_get_field(void) + Get custom field value */ +/* {{{ proto bool session_pgsql_add_error(int error_level [, string error_message]) + Increments error counts and sets last error message */ +/* {{{ proto array session_pgsql_get_error([bool with_error_message]) + Returns number of errors and last error message */ [Known Problem] Please report problem to the author. @@ -150,6 +189,15 @@ - More efficient application variable handling [History] + 0.5.2 - 2003/1/17 + - Fixed shared module build. + + 0.5.1 - 2003/1/17 + - Fixed short_circuit option. + + 0.5.0 - 2003/1/17 + - Added short_circuit option. + 0.4.1 - 2003/1/16 - Added all features planned. Alpha release. Index: pear/PECL/session_pgsql/config.m4 diff -u pear/PECL/session_pgsql/config.m4:1.10 pear/PECL/session_pgsql/config.m4:1.11 --- pear/PECL/session_pgsql/config.m4:1.10 Mon Jan 13 01:39:35 2003 +++ pear/PECL/session_pgsql/config.m4 Fri Jan 17 05:00:47 2003 @@ -1,5 +1,5 @@ dnl -dnl $Id: config.m4,v 1.10 2003/01/13 06:39:35 yohgaki Exp $ +dnl $Id: config.m4,v 1.11 2003/01/17 10:00:47 yohgaki Exp $ dnl PHP_ARG_WITH(session-pgsql,for pgsql sesssion storage support, Index: pear/PECL/session_pgsql/package.xml diff -u pear/PECL/session_pgsql/package.xml:1.2 pear/PECL/session_pgsql/package.xml:1.3 --- pear/PECL/session_pgsql/package.xml:1.2 Thu Jan 16 02:12:26 2003 +++ pear/PECL/session_pgsql/package.xml Fri Jan 17 05:00:47 2003 @@ -17,11 +17,11 @@ </description> <license>PHP</license> <release> - <state>alpha</state> - <version>0.4.1</version> - <date>2002-01-10</date> + <state>beta</state> + <version>0.5.2</version> + <date>2002-01-17</date> <notes> - - 1st alpha relase. + -3rd beta relase. </notes> <filelist> <file role="src" name="config.m4"/> Index: pear/PECL/session_pgsql/session_pgsql.c diff -u pear/PECL/session_pgsql/session_pgsql.c:1.32 pear/PECL/session_pgsql/session_pgsql.c:1.33 --- pear/PECL/session_pgsql/session_pgsql.c:1.32 Thu Jan 16 02:33:50 2003 +++ pear/PECL/session_pgsql/session_pgsql.c Fri Jan 17 05:00:47 2003 @@ -1,12 +1,13 @@ /* +----------------------------------------------------------------------+ | This source file is subject to LGPL license. | + | Copyright (c) Yasuo Ohgaki | +----------------------------------------------------------------------+ | Authors: Yasuo Ohgaki <yohgaki@php.net> | +----------------------------------------------------------------------+ */ -/* $Id: session_pgsql.c,v 1.32 2003/01/16 07:33:50 yohgaki Exp $ */ +/* $Id: session_pgsql.c,v 1.33 2003/01/17 10:00:47 yohgaki Exp $ */ #ifdef HAVE_CONFIG_H #include "config.h" @@ -19,6 +20,7 @@ #include <assert.h> #include "php.h" #include "php_ini.h" +#include "php_config.h" #include "ext/standard/info.h" #include "ext/standard/php_string.h" #include "ext/standard/php_var.h" @@ -54,7 +56,8 @@ #endif static void php_session_pgsql_init_globals(php_session_pgsql_globals *session_pgsql_globals_p TSRMLS_DC); -static int php_ps_pgsql_init_servers(TSRMLS_D); +static int php_ps_pgsql_init_servers(int force_init TSRMLS_DC); +static int php_ps_pgsql_init_mm(TSRMLS_D); static int php_ps_pgsql_create_table(int id TSRMLS_DC); static PGconn *php_ps_pgsql_connect(int id TSRMLS_DC); static PGconn *php_ps_pgsql_get_db(const char *key TSRMLS_DC); @@ -110,15 +113,17 @@ /* {{{ PHP_INI */ PHP_INI_BEGIN() -STD_PHP_INI_ENTRY("session.pgsql_disable", "0", PHP_INI_SYSTEM, OnUpdateBool, disable, php_session_pgsql_globals, session_pgsql_globals) +STD_PHP_INI_ENTRY("session_pgsql.disable", "0", PHP_INI_SYSTEM, OnUpdateBool, disable, php_session_pgsql_globals, session_pgsql_globals) STD_PHP_INI_ENTRY("session_pgsql.db", "host=localhost dbname=php_session user=nobody", PHP_INI_SYSTEM, OnUpdate_session_pgsql_db, db, php_session_pgsql_globals, session_pgsql_globals) STD_PHP_INI_ENTRY("session_pgsql.sem_file_name", PS_DEFAULT_PGSQL_FILE, PHP_INI_SYSTEM, OnUpdateString, sem_file_name, php_session_pgsql_globals, session_pgsql_globals) -STD_PHP_INI_ENTRY("session.pgsql_create_table", "1", PHP_INI_SYSTEM, OnUpdateBool, create_table, php_session_pgsql_globals, session_pgsql_globals) -STD_PHP_INI_ENTRY("session.pgsql_failover_mode", "0", PHP_INI_SYSTEM, OnUpdateBool, failover_mode, php_session_pgsql_globals, session_pgsql_globals) -STD_PHP_INI_ENTRY("session.pgsql_use_app_vars", "0", PHP_INI_SYSTEM, OnUpdateBool, use_app_vars, php_session_pgsql_globals, session_pgsql_globals) -STD_PHP_INI_ENTRY("session.pgsql_serializable", "0", PHP_INI_SYSTEM, OnUpdateBool, serializable, php_session_pgsql_globals, session_pgsql_globals) -STD_PHP_INI_ENTRY("session.pgsql_gc_interval", "3600", PHP_INI_SYSTEM, OnUpdateInt, gc_interval, php_session_pgsql_globals, session_pgsql_globals) -STD_PHP_INI_ENTRY("session.pgsql_vacuum_interval", "18000", PHP_INI_SYSTEM, OnUpdateInt, vacuum_interval, php_session_pgsql_globals, session_pgsql_globals) +STD_PHP_INI_ENTRY("session_pgsql.create_table", "1", PHP_INI_SYSTEM, OnUpdateBool, create_table, php_session_pgsql_globals, session_pgsql_globals) +STD_PHP_INI_ENTRY("session_pgsql.failover_mode", "0", PHP_INI_SYSTEM, OnUpdateBool, failover_mode, php_session_pgsql_globals, session_pgsql_globals) +STD_PHP_INI_ENTRY("session_pgsql.short_circuit", "0", PHP_INI_SYSTEM, OnUpdateBool, short_circuit, php_session_pgsql_globals, session_pgsql_globals) +STD_PHP_INI_ENTRY("session_pgsql.keep_expired", "0", PHP_INI_SYSTEM, OnUpdateBool, keep_expired, php_session_pgsql_globals, session_pgsql_globals) +STD_PHP_INI_ENTRY("session_pgsql.use_app_vars", "0", PHP_INI_SYSTEM, OnUpdateBool, use_app_vars, php_session_pgsql_globals, session_pgsql_globals) +STD_PHP_INI_ENTRY("session_pgsql.serializable", "0", PHP_INI_SYSTEM, OnUpdateBool, serializable, php_session_pgsql_globals, session_pgsql_globals) +STD_PHP_INI_ENTRY("session_pgsql.gc_interval", "3600", PHP_INI_SYSTEM, OnUpdateInt, gc_interval, php_session_pgsql_globals, session_pgsql_globals) +STD_PHP_INI_ENTRY("session_pgsql.vacuum_interval", "18000", PHP_INI_SYSTEM, OnUpdateInt, vacuum_interval, php_session_pgsql_globals, session_pgsql_globals) PHP_INI_END() /* }}} */ @@ -151,7 +156,7 @@ PHP_MINIT(session_pgsql), PHP_MSHUTDOWN(session_pgsql), PHP_RINIT(session_pgsql), PHP_RSHUTDOWN(session_pgsql), PHP_MINFO(session_pgsql), - "0.4.1", + PHP_SESSION_PGSQL_VERSION, STANDARD_MODULE_PROPERTIES }; /* }}} */ @@ -164,7 +169,6 @@ */ PHP_MINIT_FUNCTION(session_pgsql) { - smart_str buf={0}; #ifdef ZTS php_session_pgsql_globals *session_pgsql_globals; ts_allocate_id(&session_pgsql_globals_id, sizeof(php_session_pgsql_globals), @@ -189,53 +193,15 @@ PS_PGSQL(disable) = 1; return SUCCESS; /* Don't spit annoying error messages */ } - - ps_pgsql_instance = calloc(sizeof(*ps_pgsql_instance), 1); - if (!ps_pgsql_instance) { + /* init mm */ + if (php_ps_pgsql_init_mm(TSRMLS_C) == FAILURE) { return FAILURE; } - - /* create shared memory file using sapi_name */ - if (PS_PGSQL(sem_file_name) && PS_PGSQL(sem_file_name)[0]) { - smart_str_appends(&buf,PS_PGSQL(sem_file_name)); - } - else { - smart_str_appends(&buf, PS_DEFAULT_PGSQL_FILE); - } - smart_str_appends(&buf,sapi_module.name); - smart_str_append_long(&buf,getpid()); - smart_str_0(&buf); - ps_pgsql_instance->mm = mm_create(0, buf.c); - smart_str_free(&buf); - if (!ps_pgsql_instance->mm) { - mm_destroy(ps_pgsql_instance->mm); - free(ps_pgsql_instance); - php_log_err("session pgsql: MM failure" TSRMLS_CC); - return FAILURE; - } - ps_pgsql_instance->last_gc = mm_calloc(ps_pgsql_instance->mm, 1, sizeof(time_t)); - if (!ps_pgsql_instance->mm) { - mm_destroy(ps_pgsql_instance->mm); - free(ps_pgsql_instance); - php_log_err("session pgsql: MM failure" TSRMLS_CC); - return FAILURE; - } - ps_pgsql_instance->last_vacuum = mm_calloc(ps_pgsql_instance->mm, 1, sizeof(time_t)); - ps_pgsql_instance->owner = getpid(); - *(ps_pgsql_instance->last_gc) = time(NULL) + PS_PGSQL(gc_interval); - *(ps_pgsql_instance->last_vacuum) = time(NULL) + PS_PGSQL(vacuum_interval); /* init $_APP hash */ if (PS_PGSQL(use_app_vars)) { zend_register_auto_global("_APP", sizeof("_APP")-1 TSRMLS_CC); } - /* initilize postgresql server connections */ - if (php_ps_pgsql_init_servers(TSRMLS_C) == FAILURE) { - /* No servers are available */ - php_log_err("session pgsql: Cannot connect to any PostgreSQL server. Check session_pgsql.db" TSRMLS_CC); - return FAILURE; - } - /* register pgsql session save handler */ php_session_register_module(&ps_mod_pgsql); @@ -310,6 +276,13 @@ return SUCCESS; } + /* initilize postgresql server connections */ + if (php_ps_pgsql_init_servers(0 TSRMLS_CC) == FAILURE) { + /* No servers are available */ + php_log_err("session pgsql: Cannot connect to any PostgreSQL server. Check session_pgsql.db" TSRMLS_CC); + return FAILURE; + } + /* These clean up cannot be done at rshutdown, since it executed before session write. */ if (PS_PGSQL(sess_custom)) { @@ -382,15 +355,67 @@ PS_PGSQL(sess_addr_created) = NULL; PS_PGSQL(sess_addr_modified) = NULL; PS_PGSQL(remote_addr) = NULL; + PS_PGSQL(sess_val) = NULL; +} +/* }}} */ + +/* {{{ php_ps_pgsql_init_mm + */ +static int php_ps_pgsql_init_mm(TSRMLS_D) +{ + smart_str buf={0}; + + ps_pgsql_instance = calloc(sizeof(*ps_pgsql_instance), 1); + if (!ps_pgsql_instance) { + return FAILURE; + } + /* create shared memory file using sapi_name */ + if (PS_PGSQL(sem_file_name) && PS_PGSQL(sem_file_name)[0]) { + smart_str_appends(&buf,PS_PGSQL(sem_file_name)); + } + else { + smart_str_appends(&buf, PS_DEFAULT_PGSQL_FILE); + } + smart_str_appends(&buf,sapi_module.name); + smart_str_append_long(&buf,getpid()); + smart_str_0(&buf); + ps_pgsql_instance->mm = mm_create(0, buf.c); + smart_str_free(&buf); + if (!ps_pgsql_instance->mm) { + mm_destroy(ps_pgsql_instance->mm); + free(ps_pgsql_instance); + php_log_err("session pgsql: MM failure" TSRMLS_CC); + return FAILURE; + } + ps_pgsql_instance->last_gc = mm_calloc(ps_pgsql_instance->mm, 1, sizeof(time_t)); + if (!ps_pgsql_instance->mm) { + mm_destroy(ps_pgsql_instance->mm); + free(ps_pgsql_instance); + php_log_err("session pgsql: MM failure" TSRMLS_CC); + return FAILURE; + } + ps_pgsql_instance->last_vacuum = mm_calloc(ps_pgsql_instance->mm, 1, sizeof(time_t)); + ps_pgsql_instance->owner = getpid(); + *(ps_pgsql_instance->last_gc) = time(NULL) + PS_PGSQL(gc_interval); + *(ps_pgsql_instance->last_vacuum) = time(NULL) + PS_PGSQL(vacuum_interval); + + return SUCCESS; } /* }}} */ /* {{{ php_ps_pgsql_init_servers */ -static int php_ps_pgsql_init_servers(TSRMLS_D) +static int php_ps_pgsql_init_servers(int force_init TSRMLS_DC) { int id; + static int initialized = 0; + + if (!force_init && initialized) { + return SUCCESS; + } + + initialized = 1; for (id = 0; id < PS_PGSQL(servers); id++) { assert(PS_PGSQL(connstr)[id]); /* if link is bad, NULL is returned */ @@ -581,11 +606,11 @@ int ret = 1; for (p = key; (c = *p); p++) { - /* valid characters are a..z,A..Z,0..9,_ */ + /* valid characters are a..z, A..Z, 0..9, _-, */ if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || - (c == '_'))) { + (c == '_' || c == '-' || c == ','))) { ret = 0; break; } @@ -708,10 +733,24 @@ else { pg_result = PQexec(PS_PGSQL(current_db), "BEGIN;"); } + PQclear(pg_result); if (PQresultStatus(pg_result) != PGRES_COMMAND_OK) { - php_error(E_WARNING, "session pgsql: Cannot start transaction. (%s)", PQresultErrorMessage(pg_result)); + /* try again. server may be rebooted */ + PQreset(PS_PGSQL(current_db)); + if (PS_PGSQL(serializable)) { + pg_result = PQexec(PS_PGSQL(current_db), "BEGIN; SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;"); + } + else { + pg_result = PQexec(PS_PGSQL(current_db), "BEGIN;"); + } + if (PQresultStatus(pg_result) != PGRES_COMMAND_OK) { + php_error(E_WARNING, "session pgsql: Cannot start transaction. (%s)", PQresultErrorMessage(pg_result)); + PQclear(pg_result); + return FAILURE; + } + PQclear(pg_result); } - PQclear(pg_result); + PS_PGSQL(sess_new) = 0; PS_PGSQL(sess_del) = 0; @@ -727,13 +766,12 @@ else { /* session data exists */ char *expire; - time_t exp; + time_t exp, now = time(NULL); expire = PQgetvalue(pg_result, 0, 0); exp = (time_t)atoi(expire); - if ((exp < time(NULL))) { + if ((exp < now) && !PS_PGSQL(keep_expired)) { /* expired. delete and create */ - PS_PGSQL(sess_new) = 1; PS_PGSQL(sess_del) = 1; } else { @@ -746,10 +784,17 @@ PS_PGSQL(sess_error) = (int)atoi(PQgetvalue(pg_result, 0, 2)); PS_PGSQL(sess_warning) = (int)atoi(PQgetvalue(pg_result, 0, 3)); PS_PGSQL(sess_notice) = (int)atoi(PQgetvalue(pg_result, 0, 4)); - /* session data - PQgetvalue reuturns "" for NULL */ - tmp = PQgetvalue(pg_result, 0, 5); - *vallen = strlen(tmp); - *val = estrndup(tmp, *vallen); + if (exp < now) { + *vallen = 0; + *val = empty_string; + PS_PGSQL(short_circuit) = 0; /* disable short circuit */ + } + else { + /* session data - PQgetvalue reuturns "" for NULL */ + tmp = PQgetvalue(pg_result, 0, 5); + *vallen = strlen(tmp); + *val = estrndup(tmp, *vallen); + } /* custom field */ tmp = PQgetvalue(pg_result, 0, 6); PS_PGSQL(sess_custom) = strdup(tmp); @@ -764,17 +809,26 @@ } else { /* something wrong, but try to delete and insert data anyway */ - PS_PGSQL(sess_new) = 1; PS_PGSQL(sess_del) = 1; ret = SUCCESS; } PQclear(pg_result); } else { + PS_PGSQL(sess_new) = 1; php_error(E_NOTICE,"session pgsql: Invalid Session ID detected"); } if (*vallen == 0) { - *val = safe_estrndup("",0); + *val = empty_string; + } + /* save values for short circuit */ + if (PS_PGSQL(sess_new)) { + PS_PGSQL(sess_vallen) = 0; + PS_PGSQL(sess_val) = empty_string; + } + else { + PS_PGSQL(sess_vallen) = *vallen; + PS_PGSQL(sess_val) = estrndup(*val, *vallen); } return ret; @@ -804,13 +858,14 @@ key_len = strlen(key); escaped_key = (char *)emalloc(key_len*2+1); key_len = PQescapeString(escaped_key, key, key_len); - if (PS_PGSQL(sess_del)) { + if (PS_PGSQL(sess_del) && !PS_PGSQL(keep_expired)) { query_len = strlen(query_delete) + key_len; query = emalloc(query_len+1); snprintf(query, query_len, query_delete, key); pg_result = PQexec(PS_PGSQL(current_db), query); PQclear(pg_result); efree(query); + PS_PGSQL(sess_new) = 1; } now = time(NULL); @@ -847,10 +902,15 @@ snprintf(query, query_len, query_insert, escaped_key, escaped_sess_name, now, PS_PGSQL(remote_addr), now, exp, escaped_val, buf.c); pg_result = PQexec(PS_PGSQL(current_db), query); + PQclear(pg_result); + smart_str_free(&buf); efree(escaped_sess_name); + efree(query); + efree(escaped_key); + efree(escaped_val); } - else { - /* UPDATE */ + else if (!PS_PGSQL(sess_short_circuit) || vallen != PS_PGSQL(sess_vallen) || strncmp(val, PS_PGSQL(sess_val), PS_PGSQL(sess_vallen))) { + /* UPDATE - skip updating if possible */ query_len += strlen(query_update); if (PS_PGSQL(sess_custom) && PS_PGSQL(sess_custom)[0]) { smart_str_appends(&buf, ", sess_custom = '"); @@ -873,24 +933,24 @@ escaped_val, now, PS_PGSQL(remote_addr), exp, PS_PGSQL(sess_cnt), PS_PGSQL(sess_error), PS_PGSQL(sess_warning), PS_PGSQL(sess_notice), buf.c, escaped_key); pg_result = PQexec(PS_PGSQL(current_db), query); + PQclear(pg_result); + smart_str_free(&buf); + efree(query); + efree(escaped_key); + efree(escaped_val); } - smart_str_free(&buf); - PQclear(pg_result); - efree(query); - efree(escaped_key); - efree(escaped_val); /* save error message is any */ if (PS_PGSQL(sess_error_message)) { - char *escaped; - int len; + char *escaped_error_message; + int len, error_message_len; smart_str buf = {0}; len = strlen(PS_PGSQL(sess_error_message)); - escaped = (char *)emalloc(len*2+1); - len = PQescapeString(escaped, PS_PGSQL(sess_error_message), len); + escaped_error_message = (char *)emalloc(len*2+1); + error_message_len = PQescapeString(escaped_error_message, PS_PGSQL(sess_error_message), len); smart_str_appends(&buf, "UPDATE php_session SET sess_err_message = '"); - smart_str_appendl(&buf, escaped, len); + smart_str_appendl(&buf, escaped_error_message, error_message_len); smart_str_appendl(&buf, "';", 2); smart_str_0(&buf); @@ -898,7 +958,7 @@ PQclear(pg_result); smart_str_free(&buf); - efree(escaped); + efree(escaped_error_message); } pg_result = PQexec(PS_PGSQL(current_db), "END;"); if (PQresultStatus(pg_result) != PGRES_COMMAND_OK) { @@ -954,8 +1014,15 @@ PS_OPEN_FUNC(pgsql) { ELOG("OPEN CALLED"); - *mod_data = (void *)1; /* mod_data cannot be NULL to make session save handler module work */ - + /* mod_data cannot be NULL to make session save handler module work */ + *mod_data = (void *)1; + /* short circuit option */ + if (PS_PGSQL(short_circuit)) { + PS_PGSQL(sess_short_circuit) = 1; + } + else { + PS_PGSQL(sess_short_circuit) = 0; + } return SUCCESS; } /* }}} */ @@ -968,6 +1035,11 @@ ELOG("CLOSE Called"); *mod_data = (void *)0; /* mod_data should be set to NULL to avoid additional close call */ + if (PS_PGSQL(sess_val)) { + efree(PS_PGSQL(sess_val)); + } + PS_PGSQL(sess_vallen) = 0; + PS_PGSQL(sess_val) = NULL; /* GC is done here for better response when GC is performed */ ps_pgsql_sess_gc(TSRMLS_C); return SUCCESS; @@ -1075,6 +1147,7 @@ add_assoc_string(return_value, "Servers", servers, 0); add_assoc_long(return_value, "Number of Servers", PS_PGSQL(servers)); add_assoc_long(return_value, "Failover Mode", PS_PGSQL(failover_mode)); + add_assoc_long(return_value, "Short Circuit", PS_PGSQL(short_circuit)); for (i = 0; i < PS_PGSQL(servers); i++) { snprintf(buf, BUF_SIZE, "Server String #%d", i); add_assoc_string(return_value, buf, PS_PGSQL(connstr)[i], 1); @@ -1098,7 +1171,7 @@ RETURN_FALSE; } - if (php_ps_pgsql_init_servers(TSRMLS_C) == FAILURE) { + if (php_ps_pgsql_init_servers(1 TSRMLS_CC) == FAILURE) { RETURN_FALSE; } RETURN_TRUE; @@ -1167,6 +1240,7 @@ RETURN_FALSE; } + PS_PGSQL(sess_short_circuit) = 0; /* force session write */ if (PS_PGSQL(sess_custom)) { free(PS_PGSQL(sess_custom)); } @@ -1213,6 +1287,7 @@ RETURN_FALSE; } + PS_PGSQL(sess_short_circuit) = 0; /* force session write */ switch (error_level) { case E_ERROR: case E_USER_ERROR: Index: pear/PECL/session_pgsql/session_pgsql.h diff -u pear/PECL/session_pgsql/session_pgsql.h:1.18 pear/PECL/session_pgsql/session_pgsql.h:1.19 --- pear/PECL/session_pgsql/session_pgsql.h:1.18 Thu Jan 16 02:12:26 2003 +++ pear/PECL/session_pgsql/session_pgsql.h Fri Jan 17 05:00:47 2003 @@ -1,6 +1,7 @@ /* +----------------------------------------------------------------------+ | This source file is subject to LGPL license. | + | Copyright (c) Yasuo Ohgaki | +----------------------------------------------------------------------+ | Authors: yohgaki@php.net | +----------------------------------------------------------------------+ @@ -15,6 +16,8 @@ #ifdef HAVE_SESSION_PGSQL +#define PHP_SESSION_PGSQL_VERSION "0.5.2" + #include <libpq-fe.h> #include "ext/session/php_session.h" #include "main/SAPI.h" @@ -41,6 +44,8 @@ int vacuum_interval; int failover_mode; int disable; + int short_circuit; + int keep_expired; PGconn *pgsql_link[MAX_PGSQL_SERVERS]; PGconn *current_db; @@ -49,6 +54,9 @@ int servers; /* better to use prim number. i.e. 2,3,5,7,11... session db servers */ int sess_new; int sess_del; + int sess_short_circuit; + char *sess_val; + int sess_vallen; int sess_cnt; int sess_error; int sess_warning;
Index: pear/PECL/session_pgsql/README diff -u pear/PECL/session_pgsql/README:1.9 pear/PECL/session_pgsql/README:1.10 --- pear/PECL/session_pgsql/README:1.9 Thu Jan 16 02:39:40 2003 +++ pear/PECL/session_pgsql/README Fri Jan 17 05:00:46 2003 @@ -2,7 +2,7 @@ *WARNING* This session save handler is EXPERIMENTAL. Anything may be -changed/deleted/added without notice. It is considered as alpha. +changed/deleted/added without notice. This code is considered as beta. [What is it] @@ -18,6 +18,7 @@ - Automatic db server load balancing where there are multiple PostgreSQL servers. - Automatic session table vacuum. + - Short circuit UPDATE It is design to scale any size of web sites. @@ -53,9 +54,13 @@ Following php.ini directives has special meanings for mod_pgsql -session.save_handler +session.save_handler "pgsql" for session_pgsql session. +session.* + save_path is ignored. Other session.* settings are also used for + session_pgsql. + session_pgsql.db Valid PostgreSQL database connection string. For example, "host=localhost dbname=php_session user=nobody" can @@ -67,19 +72,34 @@ use available db server automatically. NOTE: database name must be "php_session". Other connection options may be changed. + Default is "host=localhost dbname=php_session user=nobody" + +session_pgsql.keep_expired + Keep expired data except session values. Counter, created time, created + IP address, etc is kept when this is set to on. If this option is off + session record is deleted and insert record is created. + Default is off. session_pgsql.use_app_vars Use application level variable ($_APP) or not. Use different session name to change application. i.e. use session_name('AppName') Use of application variable has severe performance penalty. - Default is off + Default is off. session_pgsql.create_table Automatically create session and application variable table if set to on. session_pgsql creates tables, php_session and php_app_vers, in the database. Default is on. +session_pgsql.short_circuit + If short_circuit is set to on, it will skip UPDATE statement when + skipping is possible. Setting error or custom field value or SESSION + array will makde session_pgsql save changes to table, otherwise + it will not update table. Access counter and expire field will not + be updated when update is skipped. + Default is off. + session_pgsql.failover_mode If failover_mode is set to on, it will always use 1st db server and if 1st server fails, it will use next available db server. @@ -88,22 +108,41 @@ db server will not be enabled. Default is off. NOTE: session_pgsql do not mirror session data. It just switch to other database server to continue web services. + Default is off. session_pgsql.gc_interval - How often perform GC in seconds. Default is 3600 seconds. + How often perform GC in seconds. If you set multiple db servers to session_pgsql.db, set only one of your web server to prevent unneeded garbage collections. It is good idea set to 0 (disable) and perform garbage collection by using cron or like in db server if you have multiple web servers accessed to the session db server(s). + Default is 3600 seconds (1 hour). session_pgsql.vacuum_interval - How often perform GC in seconds. Default is 18000 seconds. + How often perform GC in seconds. If you set multiple db servers to session_pgsql.db, set only one of your web server to prevent unneeded vacuum operations. It is good idea set to 0 (disable) and perform vacuum by using cron or like in db server if you have multiple web servers accessed to the session db server(s). + Default is 18000 seconds (5 hours). + +[Functions] +/* {{{ proto array session_pgsql_status(void) + Returns current pgsql save handler status */ +/* {{{ proto bool session_pgsql_reset(void) + Reset connection to session database servsers */ +/* {{{ proto array session_pgsql_info(void) + Returns current session info */ +/* {{{ proto bool session_pgsql_set_field(string value) + Set custom field value */ +/* {{{ proto string session_pgsql_get_field(void) + Get custom field value */ +/* {{{ proto bool session_pgsql_add_error(int error_level [, string error_message]) + Increments error counts and sets last error message */ +/* {{{ proto array session_pgsql_get_error([bool with_error_message]) + Returns number of errors and last error message */ [Known Problem] Please report problem to the author. @@ -150,6 +189,15 @@ - More efficient application variable handling [History] + 0.5.2 - 2003/1/17 + - Fixed shared module build. + + 0.5.1 - 2003/1/17 + - Fixed short_circuit option. + + 0.5.0 - 2003/1/17 + - Added short_circuit option. + 0.4.1 - 2003/1/16 - Added all features planned. Alpha release. Index: pear/PECL/session_pgsql/config.m4 diff -u pear/PECL/session_pgsql/config.m4:1.10 pear/PECL/session_pgsql/config.m4:1.11 --- pear/PECL/session_pgsql/config.m4:1.10 Mon Jan 13 01:39:35 2003 +++ pear/PECL/session_pgsql/config.m4 Fri Jan 17 05:00:47 2003 @@ -1,5 +1,5 @@ dnl -dnl $Id: config.m4,v 1.10 2003/01/13 06:39:35 yohgaki Exp $ +dnl $Id: config.m4,v 1.11 2003/01/17 10:00:47 yohgaki Exp $ dnl PHP_ARG_WITH(session-pgsql,for pgsql sesssion storage support, Index: pear/PECL/session_pgsql/package.xml diff -u pear/PECL/session_pgsql/package.xml:1.2 pear/PECL/session_pgsql/package.xml:1.3 --- pear/PECL/session_pgsql/package.xml:1.2 Thu Jan 16 02:12:26 2003 +++ pear/PECL/session_pgsql/package.xml Fri Jan 17 05:00:47 2003 @@ -17,11 +17,11 @@ </description> <license>PHP</license> <release> - <state>alpha</state> - <version>0.4.1</version> - <date>2002-01-10</date> + <state>beta</state> + <version>0.5.2</version> + <date>2002-01-17</date> <notes> - - 1st alpha relase. + -3rd beta relase. </notes> <filelist> <file role="src" name="config.m4"/> Index: pear/PECL/session_pgsql/session_pgsql.c diff -u pear/PECL/session_pgsql/session_pgsql.c:1.32 pear/PECL/session_pgsql/session_pgsql.c:1.33 --- pear/PECL/session_pgsql/session_pgsql.c:1.32 Thu Jan 16 02:33:50 2003 +++ pear/PECL/session_pgsql/session_pgsql.c Fri Jan 17 05:00:47 2003 @@ -1,12 +1,13 @@ /* +----------------------------------------------------------------------+ | This source file is subject to LGPL license. | + | Copyright (c) Yasuo Ohgaki | +----------------------------------------------------------------------+ | Authors: Yasuo Ohgaki <yohgaki@php.net> | +----------------------------------------------------------------------+ */ -/* $Id: session_pgsql.c,v 1.32 2003/01/16 07:33:50 yohgaki Exp $ */ +/* $Id: session_pgsql.c,v 1.33 2003/01/17 10:00:47 yohgaki Exp $ */ #ifdef HAVE_CONFIG_H #include "config.h" @@ -19,6 +20,7 @@ #include <assert.h> #include "php.h" #include "php_ini.h" +#include "php_config.h" #include "ext/standard/info.h" #include "ext/standard/php_string.h" #include "ext/standard/php_var.h" @@ -54,7 +56,8 @@ #endif static void php_session_pgsql_init_globals(php_session_pgsql_globals *session_pgsql_globals_p TSRMLS_DC); -static int php_ps_pgsql_init_servers(TSRMLS_D); +static int php_ps_pgsql_init_servers(int force_init TSRMLS_DC); +static int php_ps_pgsql_init_mm(TSRMLS_D); static int php_ps_pgsql_create_table(int id TSRMLS_DC); static PGconn *php_ps_pgsql_connect(int id TSRMLS_DC); static PGconn *php_ps_pgsql_get_db(const char *key TSRMLS_DC); @@ -110,15 +113,17 @@ /* {{{ PHP_INI */ PHP_INI_BEGIN() -STD_PHP_INI_ENTRY("session.pgsql_disable", "0", PHP_INI_SYSTEM, OnUpdateBool, disable, php_session_pgsql_globals, session_pgsql_globals) +STD_PHP_INI_ENTRY("session_pgsql.disable", "0", PHP_INI_SYSTEM, OnUpdateBool, disable, php_session_pgsql_globals, session_pgsql_globals) STD_PHP_INI_ENTRY("session_pgsql.db", "host=localhost dbname=php_session user=nobody", PHP_INI_SYSTEM, OnUpdate_session_pgsql_db, db, php_session_pgsql_globals, session_pgsql_globals) STD_PHP_INI_ENTRY("session_pgsql.sem_file_name", PS_DEFAULT_PGSQL_FILE, PHP_INI_SYSTEM, OnUpdateString, sem_file_name, php_session_pgsql_globals, session_pgsql_globals) -STD_PHP_INI_ENTRY("session.pgsql_create_table", "1", PHP_INI_SYSTEM, OnUpdateBool, create_table, php_session_pgsql_globals, session_pgsql_globals) -STD_PHP_INI_ENTRY("session.pgsql_failover_mode", "0", PHP_INI_SYSTEM, OnUpdateBool, failover_mode, php_session_pgsql_globals, session_pgsql_globals) -STD_PHP_INI_ENTRY("session.pgsql_use_app_vars", "0", PHP_INI_SYSTEM, OnUpdateBool, use_app_vars, php_session_pgsql_globals, session_pgsql_globals) -STD_PHP_INI_ENTRY("session.pgsql_serializable", "0", PHP_INI_SYSTEM, OnUpdateBool, serializable, php_session_pgsql_globals, session_pgsql_globals) -STD_PHP_INI_ENTRY("session.pgsql_gc_interval", "3600", PHP_INI_SYSTEM, OnUpdateInt, gc_interval, php_session_pgsql_globals, session_pgsql_globals) -STD_PHP_INI_ENTRY("session.pgsql_vacuum_interval", "18000", PHP_INI_SYSTEM, OnUpdateInt, vacuum_interval, php_session_pgsql_globals, session_pgsql_globals) +STD_PHP_INI_ENTRY("session_pgsql.create_table", "1", PHP_INI_SYSTEM, OnUpdateBool, create_table, php_session_pgsql_globals, session_pgsql_globals) +STD_PHP_INI_ENTRY("session_pgsql.failover_mode", "0", PHP_INI_SYSTEM, OnUpdateBool, failover_mode, php_session_pgsql_globals, session_pgsql_globals) +STD_PHP_INI_ENTRY("session_pgsql.short_circuit", "0", PHP_INI_SYSTEM, OnUpdateBool, short_circuit, php_session_pgsql_globals, session_pgsql_globals) +STD_PHP_INI_ENTRY("session_pgsql.keep_expired", "0", PHP_INI_SYSTEM, OnUpdateBool, keep_expired, php_session_pgsql_globals, session_pgsql_globals) +STD_PHP_INI_ENTRY("session_pgsql.use_app_vars", "0", PHP_INI_SYSTEM, OnUpdateBool, use_app_vars, php_session_pgsql_globals, session_pgsql_globals) +STD_PHP_INI_ENTRY("session_pgsql.serializable", "0", PHP_INI_SYSTEM, OnUpdateBool, serializable, php_session_pgsql_globals, session_pgsql_globals) +STD_PHP_INI_ENTRY("session_pgsql.gc_interval", "3600", PHP_INI_SYSTEM, OnUpdateInt, gc_interval, php_session_pgsql_globals, session_pgsql_globals) +STD_PHP_INI_ENTRY("session_pgsql.vacuum_interval", "18000", PHP_INI_SYSTEM, OnUpdateInt, vacuum_interval, php_session_pgsql_globals, session_pgsql_globals) PHP_INI_END() /* }}} */ @@ -151,7 +156,7 @@ PHP_MINIT(session_pgsql), PHP_MSHUTDOWN(session_pgsql), PHP_RINIT(session_pgsql), PHP_RSHUTDOWN(session_pgsql), PHP_MINFO(session_pgsql), - "0.4.1", + PHP_SESSION_PGSQL_VERSION, STANDARD_MODULE_PROPERTIES }; /* }}} */ @@ -164,7 +169,6 @@ */ PHP_MINIT_FUNCTION(session_pgsql) { - smart_str buf={0}; #ifdef ZTS php_session_pgsql_globals *session_pgsql_globals; ts_allocate_id(&session_pgsql_globals_id, sizeof(php_session_pgsql_globals), @@ -189,53 +193,15 @@ PS_PGSQL(disable) = 1; return SUCCESS; /* Don't spit annoying error messages */ } - - ps_pgsql_instance = calloc(sizeof(*ps_pgsql_instance), 1); - if (!ps_pgsql_instance) { + /* init mm */ + if (php_ps_pgsql_init_mm(TSRMLS_C) == FAILURE) { return FAILURE; } - - /* create shared memory file using sapi_name */ - if (PS_PGSQL(sem_file_name) && PS_PGSQL(sem_file_name)[0]) { - smart_str_appends(&buf,PS_PGSQL(sem_file_name)); - } - else { - smart_str_appends(&buf, PS_DEFAULT_PGSQL_FILE); - } - smart_str_appends(&buf,sapi_module.name); - smart_str_append_long(&buf,getpid()); - smart_str_0(&buf); - ps_pgsql_instance->mm = mm_create(0, buf.c); - smart_str_free(&buf); - if (!ps_pgsql_instance->mm) { - mm_destroy(ps_pgsql_instance->mm); - free(ps_pgsql_instance); - php_log_err("session pgsql: MM failure" TSRMLS_CC); - return FAILURE; - } - ps_pgsql_instance->last_gc = mm_calloc(ps_pgsql_instance->mm, 1, sizeof(time_t)); - if (!ps_pgsql_instance->mm) { - mm_destroy(ps_pgsql_instance->mm); - free(ps_pgsql_instance); - php_log_err("session pgsql: MM failure" TSRMLS_CC); - return FAILURE; - } - ps_pgsql_instance->last_vacuum = mm_calloc(ps_pgsql_instance->mm, 1, sizeof(time_t)); - ps_pgsql_instance->owner = getpid(); - *(ps_pgsql_instance->last_gc) = time(NULL) + PS_PGSQL(gc_interval); - *(ps_pgsql_instance->last_vacuum) = time(NULL) + PS_PGSQL(vacuum_interval); /* init $_APP hash */ if (PS_PGSQL(use_app_vars)) { zend_register_auto_global("_APP", sizeof("_APP")-1 TSRMLS_CC); } - /* initilize postgresql server connections */ - if (php_ps_pgsql_init_servers(TSRMLS_C) == FAILURE) { - /* No servers are available */ - php_log_err("session pgsql: Cannot connect to any PostgreSQL server. Check session_pgsql.db" TSRMLS_CC); - return FAILURE; - } - /* register pgsql session save handler */ php_session_register_module(&ps_mod_pgsql); @@ -310,6 +276,13 @@ return SUCCESS; } + /* initilize postgresql server connections */ + if (php_ps_pgsql_init_servers(0 TSRMLS_CC) == FAILURE) { + /* No servers are available */ + php_log_err("session pgsql: Cannot connect to any PostgreSQL server. Check session_pgsql.db" TSRMLS_CC); + return FAILURE; + } + /* These clean up cannot be done at rshutdown, since it executed before session write. */ if (PS_PGSQL(sess_custom)) { @@ -382,15 +355,67 @@ PS_PGSQL(sess_addr_created) = NULL; PS_PGSQL(sess_addr_modified) = NULL; PS_PGSQL(remote_addr) = NULL; + PS_PGSQL(sess_val) = NULL; +} +/* }}} */ + +/* {{{ php_ps_pgsql_init_mm + */ +static int php_ps_pgsql_init_mm(TSRMLS_D) +{ + smart_str buf={0}; + + ps_pgsql_instance = calloc(sizeof(*ps_pgsql_instance), 1); + if (!ps_pgsql_instance) { + return FAILURE; + } + /* create shared memory file using sapi_name */ + if (PS_PGSQL(sem_file_name) && PS_PGSQL(sem_file_name)[0]) { + smart_str_appends(&buf,PS_PGSQL(sem_file_name)); + } + else { + smart_str_appends(&buf, PS_DEFAULT_PGSQL_FILE); + } + smart_str_appends(&buf,sapi_module.name); + smart_str_append_long(&buf,getpid()); + smart_str_0(&buf); + ps_pgsql_instance->mm = mm_create(0, buf.c); + smart_str_free(&buf); + if (!ps_pgsql_instance->mm) { + mm_destroy(ps_pgsql_instance->mm); + free(ps_pgsql_instance); + php_log_err("session pgsql: MM failure" TSRMLS_CC); + return FAILURE; + } + ps_pgsql_instance->last_gc = mm_calloc(ps_pgsql_instance->mm, 1, sizeof(time_t)); + if (!ps_pgsql_instance->mm) { + mm_destroy(ps_pgsql_instance->mm); + free(ps_pgsql_instance); + php_log_err("session pgsql: MM failure" TSRMLS_CC); + return FAILURE; + } + ps_pgsql_instance->last_vacuum = mm_calloc(ps_pgsql_instance->mm, 1, sizeof(time_t)); + ps_pgsql_instance->owner = getpid(); + *(ps_pgsql_instance->last_gc) = time(NULL) + PS_PGSQL(gc_interval); + *(ps_pgsql_instance->last_vacuum) = time(NULL) + PS_PGSQL(vacuum_interval); + + return SUCCESS; } /* }}} */ /* {{{ php_ps_pgsql_init_servers */ -static int php_ps_pgsql_init_servers(TSRMLS_D) +static int php_ps_pgsql_init_servers(int force_init TSRMLS_DC) { int id; + static int initialized = 0; + + if (!force_init && initialized) { + return SUCCESS; + } + + initialized = 1; for (id = 0; id < PS_PGSQL(servers); id++) { assert(PS_PGSQL(connstr)[id]); /* if link is bad, NULL is returned */ @@ -581,11 +606,11 @@ int ret = 1; for (p = key; (c = *p); p++) { - /* valid characters are a..z,A..Z,0..9,_ */ + /* valid characters are a..z, A..Z, 0..9, _-, */ if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || - (c == '_'))) { + (c == '_' || c == '-' || c == ','))) { ret = 0; break; } @@ -708,10 +733,24 @@ else { pg_result = PQexec(PS_PGSQL(current_db), "BEGIN;"); } + PQclear(pg_result); if (PQresultStatus(pg_result) != PGRES_COMMAND_OK) { - php_error(E_WARNING, "session pgsql: Cannot start transaction. (%s)", PQresultErrorMessage(pg_result)); + /* try again. server may be rebooted */ + PQreset(PS_PGSQL(current_db)); + if (PS_PGSQL(serializable)) { + pg_result = PQexec(PS_PGSQL(current_db), "BEGIN; SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;"); + } + else { + pg_result = PQexec(PS_PGSQL(current_db), "BEGIN;"); + } + if (PQresultStatus(pg_result) != PGRES_COMMAND_OK) { + php_error(E_WARNING, "session pgsql: Cannot start transaction. (%s)", PQresultErrorMessage(pg_result)); + PQclear(pg_result); + return FAILURE; + } + PQclear(pg_result); } - PQclear(pg_result); + PS_PGSQL(sess_new) = 0; PS_PGSQL(sess_del) = 0; @@ -727,13 +766,12 @@ else { /* session data exists */ char *expire; - time_t exp; + time_t exp, now = time(NULL); expire = PQgetvalue(pg_result, 0, 0); exp = (time_t)atoi(expire); - if ((exp < time(NULL))) { + if ((exp < now) && !PS_PGSQL(keep_expired)) { /* expired. delete and create */ - PS_PGSQL(sess_new) = 1; PS_PGSQL(sess_del) = 1; } else { @@ -746,10 +784,17 @@ PS_PGSQL(sess_error) = (int)atoi(PQgetvalue(pg_result, 0, 2)); PS_PGSQL(sess_warning) = (int)atoi(PQgetvalue(pg_result, 0, 3)); PS_PGSQL(sess_notice) = (int)atoi(PQgetvalue(pg_result, 0, 4)); - /* session data - PQgetvalue reuturns "" for NULL */ - tmp = PQgetvalue(pg_result, 0, 5); - *vallen = strlen(tmp); - *val = estrndup(tmp, *vallen); + if (exp < now) { + *vallen = 0; + *val = empty_string; + PS_PGSQL(short_circuit) = 0; /* disable short circuit */ + } + else { + /* session data - PQgetvalue reuturns "" for NULL */ + tmp = PQgetvalue(pg_result, 0, 5); + *vallen = strlen(tmp); + *val = estrndup(tmp, *vallen); + } /* custom field */ tmp = PQgetvalue(pg_result, 0, 6); PS_PGSQL(sess_custom) = strdup(tmp); @@ -764,17 +809,26 @@ } else { /* something wrong, but try to delete and insert data anyway */ - PS_PGSQL(sess_new) = 1; PS_PGSQL(sess_del) = 1; ret = SUCCESS; } PQclear(pg_result); } else { + PS_PGSQL(sess_new) = 1; php_error(E_NOTICE,"session pgsql: Invalid Session ID detected"); } if (*vallen == 0) { - *val = safe_estrndup("",0); + *val = empty_string; + } + /* save values for short circuit */ + if (PS_PGSQL(sess_new)) { + PS_PGSQL(sess_vallen) = 0; + PS_PGSQL(sess_val) = empty_string; + } + else { + PS_PGSQL(sess_vallen) = *vallen; + PS_PGSQL(sess_val) = estrndup(*val, *vallen); } return ret; @@ -804,13 +858,14 @@ key_len = strlen(key); escaped_key = (char *)emalloc(key_len*2+1); key_len = PQescapeString(escaped_key, key, key_len); - if (PS_PGSQL(sess_del)) { + if (PS_PGSQL(sess_del) && !PS_PGSQL(keep_expired)) { query_len = strlen(query_delete) + key_len; query = emalloc(query_len+1); snprintf(query, query_len, query_delete, key); pg_result = PQexec(PS_PGSQL(current_db), query); PQclear(pg_result); efree(query); + PS_PGSQL(sess_new) = 1; } now = time(NULL); @@ -847,10 +902,15 @@ snprintf(query, query_len, query_insert, escaped_key, escaped_sess_name, now, PS_PGSQL(remote_addr), now, exp, escaped_val, buf.c); pg_result = PQexec(PS_PGSQL(current_db), query); + PQclear(pg_result); + smart_str_free(&buf); efree(escaped_sess_name); + efree(query); + efree(escaped_key); + efree(escaped_val); } - else { - /* UPDATE */ + else if (!PS_PGSQL(sess_short_circuit) || vallen != PS_PGSQL(sess_vallen) || strncmp(val, PS_PGSQL(sess_val), PS_PGSQL(sess_vallen))) { + /* UPDATE - skip updating if possible */ query_len += strlen(query_update); if (PS_PGSQL(sess_custom) && PS_PGSQL(sess_custom)[0]) { smart_str_appends(&buf, ", sess_custom = '"); @@ -873,24 +933,24 @@ escaped_val, now, PS_PGSQL(remote_addr), exp, PS_PGSQL(sess_cnt), PS_PGSQL(sess_error), PS_PGSQL(sess_warning), PS_PGSQL(sess_notice), buf.c, escaped_key); pg_result = PQexec(PS_PGSQL(current_db), query); + PQclear(pg_result); + smart_str_free(&buf); + efree(query); + efree(escaped_key); + efree(escaped_val); } - smart_str_free(&buf); - PQclear(pg_result); - efree(query); - efree(escaped_key); - efree(escaped_val); /* save error message is any */ if (PS_PGSQL(sess_error_message)) { - char *escaped; - int len; + char *escaped_error_message; + int len, error_message_len; smart_str buf = {0}; len = strlen(PS_PGSQL(sess_error_message)); - escaped = (char *)emalloc(len*2+1); - len = PQescapeString(escaped, PS_PGSQL(sess_error_message), len); + escaped_error_message = (char *)emalloc(len*2+1); + error_message_len = PQescapeString(escaped_error_message, PS_PGSQL(sess_error_message), len); smart_str_appends(&buf, "UPDATE php_session SET sess_err_message = '"); - smart_str_appendl(&buf, escaped, len); + smart_str_appendl(&buf, escaped_error_message, error_message_len); smart_str_appendl(&buf, "';", 2); smart_str_0(&buf); @@ -898,7 +958,7 @@ PQclear(pg_result); smart_str_free(&buf); - efree(escaped); + efree(escaped_error_message); } pg_result = PQexec(PS_PGSQL(current_db), "END;"); if (PQresultStatus(pg_result) != PGRES_COMMAND_OK) { @@ -954,8 +1014,15 @@ PS_OPEN_FUNC(pgsql) { ELOG("OPEN CALLED"); - *mod_data = (void *)1; /* mod_data cannot be NULL to make session save handler module work */ - + /* mod_data cannot be NULL to make session save handler module work */ + *mod_data = (void *)1; + /* short circuit option */ + if (PS_PGSQL(short_circuit)) { + PS_PGSQL(sess_short_circuit) = 1; + } + else { + PS_PGSQL(sess_short_circuit) = 0; + } return SUCCESS; } /* }}} */ @@ -968,6 +1035,11 @@ ELOG("CLOSE Called"); *mod_data = (void *)0; /* mod_data should be set to NULL to avoid additional close call */ + if (PS_PGSQL(sess_val)) { + efree(PS_PGSQL(sess_val)); + } + PS_PGSQL(sess_vallen) = 0; + PS_PGSQL(sess_val) = NULL; /* GC is done here for better response when GC is performed */ ps_pgsql_sess_gc(TSRMLS_C); return SUCCESS; @@ -1075,6 +1147,7 @@ add_assoc_string(return_value, "Servers", servers, 0); add_assoc_long(return_value, "Number of Servers", PS_PGSQL(servers)); add_assoc_long(return_value, "Failover Mode", PS_PGSQL(failover_mode)); + add_assoc_long(return_value, "Short Circuit", PS_PGSQL(short_circuit)); for (i = 0; i < PS_PGSQL(servers); i++) { snprintf(buf, BUF_SIZE, "Server String #%d", i); add_assoc_string(return_value, buf, PS_PGSQL(connstr)[i], 1); @@ -1098,7 +1171,7 @@ RETURN_FALSE; } - if (php_ps_pgsql_init_servers(TSRMLS_C) == FAILURE) { + if (php_ps_pgsql_init_servers(1 TSRMLS_CC) == FAILURE) { RETURN_FALSE; } RETURN_TRUE; @@ -1167,6 +1240,7 @@ RETURN_FALSE; } + PS_PGSQL(sess_short_circuit) = 0; /* force session write */ if (PS_PGSQL(sess_custom)) { free(PS_PGSQL(sess_custom)); } @@ -1213,6 +1287,7 @@ RETURN_FALSE; } + PS_PGSQL(sess_short_circuit) = 0; /* force session write */ switch (error_level) { case E_ERROR: case E_USER_ERROR: Index: pear/PECL/session_pgsql/session_pgsql.h diff -u pear/PECL/session_pgsql/session_pgsql.h:1.18 pear/PECL/session_pgsql/session_pgsql.h:1.19 --- pear/PECL/session_pgsql/session_pgsql.h:1.18 Thu Jan 16 02:12:26 2003 +++ pear/PECL/session_pgsql/session_pgsql.h Fri Jan 17 05:00:47 2003 @@ -1,6 +1,7 @@ /* +----------------------------------------------------------------------+ | This source file is subject to LGPL license. | + | Copyright (c) Yasuo Ohgaki | +----------------------------------------------------------------------+ | Authors: yohgaki@php.net | +----------------------------------------------------------------------+ @@ -15,6 +16,8 @@ #ifdef HAVE_SESSION_PGSQL +#define PHP_SESSION_PGSQL_VERSION "0.5.2" + #include <libpq-fe.h> #include "ext/session/php_session.h" #include "main/SAPI.h" @@ -41,6 +44,8 @@ int vacuum_interval; int failover_mode; int disable; + int short_circuit; + int keep_expired; PGconn *pgsql_link[MAX_PGSQL_SERVERS]; PGconn *current_db; @@ -49,6 +54,9 @@ int servers; /* better to use prim number. i.e. 2,3,5,7,11... session db servers */ int sess_new; int sess_del; + int sess_short_circuit; + char *sess_val; + int sess_vallen; int sess_cnt; int sess_error; int sess_warning;