flat_pool.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <unistd.h>
00026 #include "../../dprint.h"
00027 #include "flat_pool.h"
00028 #include "flat_id.h"
00029
00030
00031
00032 static struct flat_con* pool = 0;
00033
00034
00035
00036
00037
00038
00039 static int pool_pid;
00040
00041
00042
00043
00044
00045
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
00096
00097
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
00107
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
00128 ptr->next = con->next;
00129 }
00130 }
00131
00132 flat_free_connection(con);
00133 }
00134
00135
00136
00137
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 }