/src/opensips/db/db_pool.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (C) 2001-2005 iptel.org |
3 | | * Copyright (C) 2007-2008 1&1 Internet AG |
4 | | * |
5 | | * This file is part of opensips, a free SIP server. |
6 | | * |
7 | | * opensips is free software; you can redistribute it and/or modify |
8 | | * it under the terms of the GNU General Public License as published by |
9 | | * the Free Software Foundation; either version 2 of the License, or |
10 | | * (at your option) any later version |
11 | | * |
12 | | * opensips is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | | * GNU General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU General Public License |
18 | | * along with this program; if not, write to the Free Software |
19 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
20 | | */ |
21 | | |
22 | | /** |
23 | | * \file db/db_pool.c |
24 | | * \brief Functions for managing a pool of database connections. |
25 | | */ |
26 | | |
27 | | #include "../dprint.h" |
28 | | #include "db_pool.h" |
29 | | |
30 | | |
31 | | /* The head of the pool */ |
32 | | static struct pool_con* db_pool = 0; |
33 | | |
34 | | |
35 | | /* |
36 | | * Search the pool for a connection with |
37 | | * the identifier equal to id, NULL is returned |
38 | | * when no connection is found |
39 | | */ |
40 | | struct pool_con* pool_get(const struct db_id* id) |
41 | 0 | { |
42 | 0 | struct pool_con* ptr; |
43 | |
|
44 | 0 | if (!id) { |
45 | 0 | LM_ERR("invalid parameter value\n"); |
46 | 0 | return 0; |
47 | 0 | } |
48 | | |
49 | 0 | ptr = db_pool; |
50 | 0 | while (ptr) { |
51 | 0 | if (cmp_db_id(id, ptr->id)) { |
52 | 0 | ptr->ref++; |
53 | 0 | return ptr; |
54 | 0 | } |
55 | 0 | ptr = ptr->next; |
56 | 0 | } |
57 | | |
58 | 0 | return 0; |
59 | 0 | } |
60 | | |
61 | | |
62 | | /* |
63 | | * Insert a new connection into the pool |
64 | | */ |
65 | | void pool_insert(struct pool_con* con) |
66 | 0 | { |
67 | 0 | if (!con) return; |
68 | | |
69 | 0 | con->next = db_pool; |
70 | 0 | db_pool = con; |
71 | 0 | } |
72 | | |
73 | | |
74 | | /* |
75 | | * Release connection from the pool, the function |
76 | | * would return 1 when if the connection is not |
77 | | * referenced anymore and thus can be closed and |
78 | | * deleted by the backend. The function returns |
79 | | * 0 if the connection should still be kept open |
80 | | * because some other module is still using it. |
81 | | * The function returns -1 if the connection is |
82 | | * not in the pool. |
83 | | */ |
84 | | int pool_remove(struct pool_con* con) |
85 | 0 | { |
86 | 0 | struct pool_con* ptr; |
87 | |
|
88 | 0 | if (!con) return -2; |
89 | | |
90 | 0 | if (con->ref > 1) { |
91 | | /* There are still other users, just |
92 | | * decrease the reference count and return |
93 | | */ |
94 | 0 | LM_DBG("connection still kept in the pool\n"); |
95 | 0 | con->ref--; |
96 | 0 | return 0; |
97 | 0 | } |
98 | | |
99 | 0 | LM_DBG("removing connection from the pool\n"); |
100 | |
|
101 | 0 | if (db_pool == con) { |
102 | 0 | db_pool = db_pool->next; |
103 | 0 | } else { |
104 | 0 | ptr = db_pool; |
105 | 0 | while(ptr) { |
106 | 0 | if (ptr->next == con) break; |
107 | 0 | ptr = ptr->next; |
108 | 0 | } |
109 | 0 | if (!ptr) { |
110 | 0 | LM_ERR("weird, connection not found in the pool\n"); |
111 | 0 | return -1; |
112 | 0 | } else { |
113 | | /* Remove the connection from the pool */ |
114 | 0 | ptr->next = con->next; |
115 | 0 | } |
116 | 0 | } |
117 | | |
118 | 0 | return 1; |
119 | 0 | } |