[Mod-auth-commit] mod_dbi_pool/src mod_dbi_pool.c,1.3,1.4
Brought to you by:
firechipmunk,
honx
From: <fir...@us...> - 2004-02-29 08:58:52
|
Update of /cvsroot/mod-auth/mod_dbi_pool/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv14614/src Modified Files: mod_dbi_pool.c Log Message: initial work to make mod_dbi_pool support multiple connection pools. Index: mod_dbi_pool.c =================================================================== RCS file: /cvsroot/mod-auth/mod_dbi_pool/src/mod_dbi_pool.c,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -r1.3 -r1.4 --- mod_dbi_pool.c 8 Feb 2004 22:00:18 -0000 1.3 +++ mod_dbi_pool.c 29 Feb 2004 08:40:52 -0000 1.4 @@ -1,73 +1,29 @@ /* ==================================================================== - * The Apache Software License, Version 1.1 - * - * Copyright (c) 2000-2003 The Apache Software Foundation. All rights - * reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the - * distribution. + * Copyright 2003-2004 Paul Querna * - * 3. The end-user documentation included with the redistribution, - * if any, must include the following acknowledgment: - * "This product includes software developed by the - * Apache Software Foundation (http://www.apache.org/)." - * Alternately, this acknowledgment may appear in the software itself, - * if and wherever such third-party acknowledgments normally appear. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * 4. The names "Apache" and "Apache Software Foundation" must - * not be used to endorse or promote products derived from this - * software without prior written permission. For written - * permission, please contact ap...@ap.... + * http://www.apache.org/licenses/LICENSE-2.0 * - * 5. Products derived from this software may not be called "Apache", - * nor may "Apache" appear in their name, without prior written - * permission of the Apache Software Foundation. + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. * - * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR - * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF - * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND - * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT - * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. * ==================================================================== */ -/* - Author: Paul Querna <chip force-elite.com> - - Originaly Based on mod_mysql_pool by Nick Kew: - http://apache.webthing.com/ -*/ - - -/* - mod_dbi_pool: manage a pool of libdbi connections. - EXPORTS: - dbi_conn* dbipool_open(server_rec*) - - retrieve a connection from the pool and check it's valid - - May return null and log a message on error. - void dbipool_close(server_rec*, dbi_conn*) - - return a connection to the pool after use - - CONFIG: - See the code round about line 106 - -*/ +/** + * @author Paul Querna <chip force-elite.com> + * + * Originaly Based on mod_mysql_pool by Nick Kew: + * http://apache.webthing.com/ + * + * $Id$ + */ #include <ctype.h> @@ -80,109 +36,151 @@ #include <apr_reslist.h> #include <apr_strings.h> -extern module AP_MODULE_DECLARE_DATA dbi_pool_module; +#include "mod_dbi_pool.h" -/************ svr cfg: manage db connection pool ****************/ +module AP_MODULE_DECLARE_DATA dbi_pool_module; -typedef struct svr_cfg +static int dbi_conn_count = 0; +static apr_hash_t *authn_dbi_config; + +dbi_config *create_new_conf(conn_id conn_id, apr_pool_t * p) { - apr_reslist_t *dbpool; - int nmin; - int nkeep; - int nmax; - int exptime; - const char *host; - int port; - const char *db; - const char *dbi_driver; - const char *user; - const char *pass; -} svr_cfg; + dbi_config *conf; + conf = (dbi_config *) apr_pcalloc(p, sizeof(dbi_config)); + if (conf == NULL) { + return NULL; + } + conf->rec.dbi_dbname = DFLT_DBI_DBNAME; + conf->rec.dbi_driver = DFLT_DBI_DRIVER; + conf->rec.dbi_host = DFLT_DBI_HOST; + conf->rec.dbi_user = DFLT_DBI_USER; + conf->rec.dbi_pass = DFLT_DBI_PASS; + conf->rec.conn_min = DFLT_CONN_MIN; + conf->rec.conn_soft = DFLT_CONN_SOFT; + conf->rec.conn_max = DFLT_CONN_MAX; + conf->rec.conn_ttl = DFLT_CONN_TTL; + apr_hash_set(authn_dbi_config, conn_id, APR_HASH_KEY_STRING, conf); + ap_log_perror(APLOG_MARK, APLOG_DEBUG, 0, p, + "[mod_authn_dbi.c] Creating Config for %s", conn_id); + return conf; +} -typedef enum -{ cmd_host, cmd_port, cmd_db, cmd_dbi_driver, cmd_user, cmd_pass, - cmd_min, cmd_keep, cmd_max, cmd_exp -} cmd_parts; +static apr_status_t get_or_create_dbi_conf(const char *conn_id, + apr_pool_t * p, + dbi_config ** confname) +{ + dbi_config *temp; + unsigned int c; + + /* some sanity checks on conn_id..limits are liberal and are more or less random */ + if (strlen(conn_id) > 255) { + return APR_EGENERAL; + } + for (c = 0; c < strlen(conn_id); c++) { + if (conn_id[c] < ' ') { + return APR_EGENERAL; + } + } + temp = apr_hash_get(authn_dbi_config, conn_id, APR_HASH_KEY_STRING); + if (temp == NULL) { + /* no such server yet... */ + temp = create_new_conf(conn_id, p); + } + *confname = temp; + return APR_SUCCESS; +} #define ISINT(val) \ for ( p = val; *p; ++p) \ if ( ! isdigit(*p) ) \ return "Argument must be numeric!" -static const char *set_param(cmd_parms * cmd, void *cfg, const char *val) + +static const char *set_dbi_switch_conf(cmd_parms * cmd, void *config, + const char *conn_id, const char *value) { - const char *p; - svr_cfg *svr = (svr_cfg *) ap_get_module_config - (cmd->server->module_config, &dbi_pool_module); + apr_ssize_t pos = (apr_ssize_t) cmd->info; - switch ((int) cmd->info) { - case cmd_host: - svr->host = val; - break; - case cmd_port: - ISINT(val); - svr->port = atoi(val); - break; - case cmd_db: - svr->db = val; - break; - case cmd_dbi_driver: - svr->dbi_driver = val; - break; - case cmd_user: - svr->user = val; - break; - case cmd_pass: - svr->pass = val; - break; - case cmd_min: - ISINT(val); - svr->nmin = atoi(val); - break; - case cmd_keep: - ISINT(val); - svr->nkeep = atoi(val); - break; - case cmd_max: - ISINT(val); - svr->nmax = atoi(val); - break; - case cmd_exp: - ISINT(val); - svr->exptime = atoi(val); - break; + dbi_config *temp; + if ((get_or_create_dbi_conf + (conn_id, cmd->pool, &temp)) == APR_SUCCESS) { + + /* Overwriting an existing value technically is a memory leak, since the pconf pool is only + * destroyed at the termination of the whole apache process. Otoh, when processing htaccess, + * we get handed the request-pool instead which is freed afterwards, so we should be fine. */ + // XXXX: Since mod_pool_dbi will be around alot longer, this needs to be fixed! + + switch (pos) { + case CONF_DBI_DRIVER: + temp->rec.dbi_driver = value; + break; + case CONF_DBI_HOST: + temp->rec.dbi_host = value; + break; + case CONF_DBI_USERNAME: + temp->rec.dbi_user = value; + break; + case CONF_DBI_PASSWORD: + temp->rec.dbi_pass = value; + break; + case CONF_DBI_DBNAME: + temp->rec.dbi_name = value; + break; + case CONF_DBI_CONN_MIN: + temp->rec.conn_min = atoi(value); + break; + case CONF_DBI_CONN_SOFTMAX: + temp->rec.conn_soft = atoi(value); + break; + case CONF_DBI_CONN_HARDMAX: + temp->rec.conn_max = atoi(value); + break; + case CONF_DBI_CONN_TTL: + ISINT(value); + temp->rec.conn_ttl = atoi(value); + break; + default: + // unknown config directive? + break; + } } return NULL; } -static const command_rec dbi_pool_cmds[] = { - AP_INIT_TAKE1("DbiPoolHost", set_param, - (void *) cmd_host, RSRC_CONF, "DBI Host"), - AP_INIT_TAKE1("DbiPoolPort", set_param, - (void *) cmd_port, RSRC_CONF, "DBI Port"), - AP_INIT_TAKE1("DbiPoolDB", set_param, - (void *) cmd_db, RSRC_CONF, "DBI Database"), - AP_INIT_TAKE1("DbiPoolUser", set_param, - (void *) cmd_user, RSRC_CONF, "DBI Username"), - AP_INIT_TAKE1("DbiPoolPass", set_param, - (void *) cmd_pass, RSRC_CONF, "DBI Password"), - AP_INIT_TAKE1("DbiPoolMin", set_param, - (void *) cmd_min, RSRC_CONF, - "Minimum number of connections"), - AP_INIT_TAKE1("DbiPoolKeep", set_param, - (void *) cmd_keep, RSRC_CONF, - "Maximum number of sustained connections"), - AP_INIT_TAKE1("DbiPoolMax", set_param, - (void *) cmd_max, RSRC_CONF, - "Maximum number of connections"), - AP_INIT_TAKE1("DbiPoolTTL", set_param, - (void *) cmd_exp, RSRC_CONF, - "Keepalive time for idle connections"), + +static const command_rec authn_dbi_cmds[] = { + /* global config items */ + AP_INIT_TAKE2("PoolDbiDriver", set_dbi_switch_conf, + (void *) CONF_DBI_DRIVER, RSRC_CONF, + "The DBI Driver"), + AP_INIT_TAKE1("PoolDbiDriverDir", set_dbi_driverdir, + (void *) CONF_DBI_DRIVER_DIR, RSRC_CONF, + "The directory containing the DBI drivers"), + AP_INIT_TAKE2("PoolDbiHost", set_dbi_switch_conf, (void *) CONF_DBI_HOST, + RSRC_CONF, + "The host for the database connection"), + AP_INIT_TAKE2("PoolDbiUsername", set_dbi_switch_conf, + (void *) CONF_DBI_USERNAME, RSRC_CONF, + "The username for the database connection"), + AP_INIT_TAKE2("PoolDbiPassword", set_dbi_switch_conf, + (void *) CONF_DBI_PASSWORD, RSRC_CONF, + "The password for the database connection"), + AP_INIT_TAKE2("PoolDbiDBName", set_dbi_switch_conf, (void *) CONF_DBI_NAME, + RSRC_CONF, + "The name of the database containing the tables"), + AP_INIT_TAKE2("PoolDbiConnMin", set_dbi_switch_conf, + (void *) CONF_DBI_CONN_MIN, RSRC_CONF, + "The Minimum Number of Database Connections"), + AP_INIT_TAKE2("PoolDbiConnSoftMax", set_dbi_switch_conf, + (void *) CONF_DBI_CONN_SOFTMAX, RSRC_CONF, + "The Soft Maximum Number of Database Connections"), + AP_INIT_TAKE2("PoolDbiConnHardMax", set_dbi_switch_conf, + (void *) CONF_DBI_CONN_HARDMAX, RSRC_CONF, + "The Hard Maximum Number of Database Connections"), + AP_INIT_TAKE2("PoolDbiConnTTL", set_dbi_switch_conf, + (void *) CONF_DBI_CONN_TTL, RSRC_CONF, + "The Database Pool Time To Live for Each Connection."), {NULL} }; -static void *dbi_pool_cfg(apr_pool_t * p, server_rec * x) -{ - svr_cfg *svr = (svr_cfg *) apr_pcalloc(p, sizeof(svr_cfg)); - return svr; -} + /************ svr cfg: manage db connection pool ****************/ |