flat_pool.c

Go to the documentation of this file.
00001 /* 
00002  * $Id: flat_pool.c 4518 2008-07-28 15:39:28Z henningw $
00003  *
00004  * Flatstore module connection pool
00005  *
00006  * Copyright (C) 2004 FhG Fokus
00007  *
00008  * This file is part of Kamailio, a free SIP server.
00009  *
00010  * Kamailio is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version
00014  *
00015  * Kamailio is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License 
00021  * along with this program; if not, write to the Free Software 
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023  */
00024 
00025 #include <unistd.h>
00026 #include "../../dprint.h"
00027 #include "flat_pool.h"
00028 #include "flat_id.h"
00029 
00030 
00031 /* The head of the pool */
00032 static struct flat_con* pool = 0;
00033 
00034 /*
00035  * Pid of the process that added the last
00036  * connection to the pool. This is used to
00037  * check for inherited database connections.
00038  */
00039 static int pool_pid;
00040 
00041 
00042 
00043 /*
00044  * Get a connection from the pool, reuse existing
00045  * if possible, otherwise create a new one
00046  */
00047 struct flat_con* flat_get_connection(char* dir, char* table)
00048 {
00049    struct flat_id* id;
00050    struct flat_con* ptr;
00051    int pid;
00052 
00053    if (!dir || !table) {
00054       LM_ERR("invalid parameter value\n");
00055       return 0;
00056    }
00057 
00058    pid = getpid();
00059    if (pool && (pool_pid != pid)) {
00060       LM_ERR("inherited open database connections, "
00061             "this is not a good idea\n");
00062       return 0;
00063    }
00064 
00065    pool_pid = pid;
00066 
00067    id = new_flat_id(dir, table);
00068    if (!id) return 0;
00069 
00070    ptr = pool;
00071    while (ptr) {
00072       if (cmp_flat_id(id, ptr->id)) {
00073          LM_DBG("connection found in the pool\n");
00074          ptr->ref++;
00075          free_flat_id(id);
00076          return ptr;
00077       }
00078       ptr = ptr->next;
00079    }
00080 
00081    LM_DBG("connection not found in the pool\n");
00082    ptr = flat_new_connection(id);
00083    if (!ptr) {
00084       free_flat_id(id);
00085       return 0;
00086    }
00087 
00088    ptr->next = pool;
00089    pool = ptr;
00090    return ptr;
00091 }
00092 
00093 
00094 /*
00095  * Release a connection, the connection will be left
00096  * in the pool if ref count != 0, otherwise it
00097  * will be delete completely
00098  */
00099 void flat_release_connection(struct flat_con* con)
00100 {
00101    struct flat_con* ptr;
00102 
00103    if (!con) return;
00104 
00105    if (con->ref > 1) {
00106            /* There are still other users, just
00107             * decrease the reference count and return
00108             */
00109       LM_DBG("connection still kept in the pool\n");
00110       con->ref--;
00111       return;
00112    }
00113 
00114    LM_DBG("removing connection from the pool\n");
00115 
00116    if (pool == con) {
00117       pool = pool->next;
00118    } else {
00119       ptr = pool;
00120       while(ptr) {
00121          if (ptr->next == con) break;
00122          ptr = ptr->next;
00123       }
00124       if (!ptr) {
00125          LM_ERR("weird, connection not found in the pool\n");
00126       } else {
00127               /* Remove the connection from the pool */
00128          ptr->next = con->next;
00129       }
00130    }
00131 
00132    flat_free_connection(con);
00133 }
00134 
00135 
00136 /*
00137  * Close and reopen all opened connections
00138  */
00139 int flat_rotate_logs(void)
00140 {
00141    struct flat_con* ptr;
00142 
00143    ptr = pool;
00144    while(ptr) {
00145       if (flat_reopen_connection(ptr)) {
00146          return -1;
00147       }
00148       ptr = ptr->next;
00149    }
00150 
00151    return 0;
00152 }

Generated on Wed May 23 06:00:45 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6