From 838ec5745c5198692997e0a1cb8a147682cba210 Mon Sep 17 00:00:00 2001 From: Maxim Mamontov Date: Wed, 25 May 2011 14:35:43 +0300 Subject: [PATCH] 'retries' parameter added to PostgreSQL storage plugin --- .../conf-available.d/store_postgresql.conf | 8 ++++++- .../conf-available.d/store_postgresql.conf | 8 ++++++- .../store/postgresql/postgresql_store.cpp | 24 +++++++++++++++---- .../store/postgresql/postgresql_store.h | 1 + 4 files changed, 35 insertions(+), 6 deletions(-) diff --git a/projects/stargazer/inst/freebsd/etc/stargazer/conf-available.d/store_postgresql.conf b/projects/stargazer/inst/freebsd/etc/stargazer/conf-available.d/store_postgresql.conf index 86c76f44..5132c8fc 100644 --- a/projects/stargazer/inst/freebsd/etc/stargazer/conf-available.d/store_postgresql.conf +++ b/projects/stargazer/inst/freebsd/etc/stargazer/conf-available.d/store_postgresql.conf @@ -22,4 +22,10 @@ # Value: any, supported by database # Default: 123456 password = 123456 - \ No newline at end of file + + # Number of tries to reconnect + # Parameter: optional + # Value: positive integer + # Default: 3 + # retries = 3 + diff --git a/projects/stargazer/inst/linux/etc/stargazer/conf-available.d/store_postgresql.conf b/projects/stargazer/inst/linux/etc/stargazer/conf-available.d/store_postgresql.conf index 86c76f44..5132c8fc 100644 --- a/projects/stargazer/inst/linux/etc/stargazer/conf-available.d/store_postgresql.conf +++ b/projects/stargazer/inst/linux/etc/stargazer/conf-available.d/store_postgresql.conf @@ -22,4 +22,10 @@ # Value: any, supported by database # Default: 123456 password = 123456 - \ No newline at end of file + + # Number of tries to reconnect + # Parameter: optional + # Value: positive integer + # Default: 3 + # retries = 3 + diff --git a/projects/stargazer/plugins/store/postgresql/postgresql_store.cpp b/projects/stargazer/plugins/store/postgresql/postgresql_store.cpp index fd0ebf5e..4bf72c0e 100644 --- a/projects/stargazer/plugins/store/postgresql/postgresql_store.cpp +++ b/projects/stargazer/plugins/store/postgresql/postgresql_store.cpp @@ -78,7 +78,8 @@ POSTGRESQL_STORE::POSTGRESQL_STORE() user("stg"), password("123456"), version(0), - connection(NULL) + connection(NULL), + retries(3) { pthread_mutex_init(&mutex, NULL); } @@ -117,6 +118,15 @@ for(i = settings.moduleParams.begin(); i != settings.moduleParams.end(); ++i) { password = *(i->value.begin()); } + if (s == "retries") + { + if (str2x(*(i->value.begin()), retries)) + { + strError = "Invalid 'retries' value"; + printfd(__FILE__, "POSTGRESQL_STORE::ParseSettings(): '%s'\n", strError.c_str()); + return -1; + } + } } clientEncoding = "KOI8"; @@ -138,7 +148,8 @@ if (PQstatus(connection) != CONNECTION_OK) { strError = PQerrorMessage(connection); printfd(__FILE__, "POSTGRESQL_STORE::Connect(): '%s'\n", strError.c_str()); - return 1; + // Will try to connect later + return 0; } if (PQsetClientEncoding(connection, clientEncoding.c_str())) @@ -153,7 +164,12 @@ return CheckVersion(); //----------------------------------------------------------------------------- int POSTGRESQL_STORE::Reset() const { -PQreset(connection); +for (int i = 0; i < retries && PQstatus(connection) != CONNECTION_OK; ++i) + { + struct timespec ts = {1, 0}; + nanosleep(&ts, NULL); + PQreset(connection); + } if (PQstatus(connection) != CONNECTION_OK) { @@ -166,7 +182,7 @@ if (PQsetClientEncoding(connection, clientEncoding.c_str())) { strError = PQerrorMessage(connection); printfd(__FILE__, "POSTGRESQL_STORE::Reset(): '%s'\n", strError.c_str()); - return 1; + return -1; } return CheckVersion(); diff --git a/projects/stargazer/plugins/store/postgresql/postgresql_store.h b/projects/stargazer/plugins/store/postgresql/postgresql_store.h index a32b155e..8169137e 100644 --- a/projects/stargazer/plugins/store/postgresql/postgresql_store.h +++ b/projects/stargazer/plugins/store/postgresql/postgresql_store.h @@ -152,6 +152,7 @@ private: MODULE_SETTINGS settings; mutable pthread_mutex_t mutex; mutable int version; + int retries; PGconn * connection; }; -- 2.44.2