implement db_postgres connection timeout

rfuchs/db_postgres-timeout
Richard Fuchs 12 years ago
parent 582fceebdc
commit 07de80180d

@ -92,3 +92,112 @@
/** @} */
--- a/modules/db_postgres/pg_con.c
+++ b/modules/db_postgres/pg_con.c
@@ -39,6 +39,7 @@
#include "pg_con.h"
#include "pg_uri.h"
#include "pg_sql.h"
+#include "pg_mod.h"
#include "../../mem/mem.h"
#include "../../dprint.h"
@@ -237,7 +238,9 @@
struct pg_con* pcon;
struct pg_uri* puri;
char* port_str;
- int ret;
+ int ret, i = 0;
+ const char *keywords[10], *values[10];
+ char to[16];
pcon = DB_GET_PAYLOAD(con);
puri = DB_GET_PAYLOAD(con->uri);
@@ -251,6 +254,8 @@
if (puri->port > 0) {
port_str = int2str(puri->port, 0);
+ keywords[i] = "port";
+ values[i++] = port_str;
} else {
port_str = NULL;
}
@@ -260,12 +265,26 @@
pcon->con = NULL;
}
- pcon->con = PQsetdbLogin(puri->host, port_str,
- NULL, NULL, puri->database,
- puri->username, puri->password);
+ keywords[i] = "host";
+ values[i++] = puri->host;
+ keywords[i] = "dbname";
+ values[i++] = puri->database;
+ keywords[i] = "user";
+ values[i++] = puri->username;
+ keywords[i] = "password";
+ values[i++] = puri->password;
+ if (pg_timeout > 0) {
+ snprintf(to, sizeof(to)-1, "%d", pg_timeout + 3);
+ keywords[i] = "connect_timeout";
+ values[i++] = to;
+ }
+
+ keywords[i] = values[i] = NULL;
+
+ pcon->con = PQconnectdbParams(keywords, values, 1);
if (pcon->con == NULL) {
- ERR("postgres: PQsetdbLogin ran out of memory\n");
+ ERR("postgres: PQconnectdbParams ran out of memory\n");
goto error;
}
--- a/modules/db_postgres/km_pg_con.c
+++ b/modules/db_postgres/km_pg_con.c
@@ -45,6 +45,9 @@
{
struct pg_con* ptr;
char *ports;
+ int i = 0;
+ const char *keywords[10], *values[10];
+ char to[16];
LM_DBG("db_id = %p\n", id);
@@ -66,6 +69,8 @@
if (id->port) {
ports = int2str(id->port, 0);
+ keywords[i] = "port";
+ values[i++] = port_str;
LM_DBG("opening connection: postgres://xxxx:xxxx@%s:%d/%s\n", ZSW(id->host),
id->port, ZSW(id->database));
} else {
@@ -74,8 +79,24 @@
ZSW(id->database));
}
- ptr->con = PQsetdbLogin(id->host, ports, NULL, NULL, id->database, id->username, id->password);
- LM_DBG("PQsetdbLogin(%p)\n", ptr->con);
+ keywords[i] = "host";
+ values[i++] = id->host;
+ keywords[i] = "dbname";
+ values[i++] = id->database;
+ keywords[i] = "user";
+ values[i++] = id->username;
+ keywords[i] = "password";
+ values[i++] = id->password;
+ if (pg_timeout > 0) {
+ snprintf(to, sizeof(to)-1, "%d", pg_timeout + 3);
+ keywords[i] = "connect_timeout";
+ values[i++] = to;
+ }
+
+ keywords[i] = values[i] = NULL;
+
+ pcon->con = PQconnectdbParams(keywords, values, 1);
+ LM_DBG("PQconnectdbParams(%p)\n", ptr->con);
if( (ptr->con == 0) || (PQstatus(ptr->con) != CONNECTION_OK) )
{

Loading…
Cancel
Save