1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
/*
* $Id: flat_pool.c,v 1.1.1.1 2005/06/13 16:47:38 bogdan_iancu Exp $
*
* Flatstore module connection pool
*
* Copyright (C) 2004 FhG Fokus
*
* This file is part of openser, a free SIP server.
*
* openser is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* openser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <unistd.h>
#include "../../dprint.h"
#include "flat_pool.h"
#include "flat_id.h"
/* The head of the pool */
static struct flat_con* pool = 0;
/*
* Pid of the process that added the last
* connection to the pool. This is used to
* check for inherited database connections.
*/
static int pool_pid;
/*
* Get a connection from the pool, reuse existing
* if possible, otherwise create a new one
*/
struct flat_con* flat_get_connection(char* dir, char* table)
{
struct flat_id* id;
struct flat_con* ptr;
int pid;
if (!dir || !table) {
LOG(L_ERR, "flat_get_connection: Invalid parameter value\n");
return 0;
}
pid = getpid();
if (pool && (pool_pid != pid)) {
LOG(L_ERR, "flat_get_connection: Inherited open database connections, this is not a good idea\n");
return 0;
}
pool_pid = pid;
id = new_flat_id(dir, table);
if (!id) return 0;
ptr = pool;
while (ptr) {
if (cmp_flat_id(id, ptr->id)) {
DBG("flat_get_connection: Connection found in the pool\n");
ptr->ref++;
free_flat_id(id);
return ptr;
}
ptr = ptr->next;
}
DBG("flat_get_connection: Connection not found in the pool\n");
ptr = flat_new_connection(id);
if (!ptr) {
free_flat_id(id);
return 0;
}
ptr->next = pool;
pool = ptr;
return ptr;
}
/*
* Release a connection, the connection will be left
* in the pool if ref count != 0, otherwise it
* will be delete completely
*/
void flat_release_connection(struct flat_con* con)
{
struct flat_con* ptr;
if (!con) return;
if (con->ref > 1) {
/* There are still other users, just
* decrease the reference count and return
*/
DBG("flat_release_connection: Connection still kept in the pool\n");
con->ref--;
return;
}
DBG("flat_release_connection: Removing connection from the pool\n");
if (pool == con) {
pool = pool->next;
} else {
ptr = pool;
while(ptr) {
if (ptr->next == con) break;
ptr = ptr->next;
}
if (!ptr) {
LOG(L_ERR, "flat_release_connection: Weird, connection not found in the pool\n");
} else {
/* Remove the connection from the pool */
ptr->next = con->next;
}
}
flat_free_connection(con);
}
/*
* Close and reopen all opened connections
*/
int flat_rotate_logs(void)
{
struct flat_con* ptr;
ptr = pool;
while(ptr) {
if (flat_reopen_connection(ptr)) {
return -1;
}
ptr = ptr->next;
}
return 0;
}
|