From b2140fdbaac1cca9110efb322a9a2a1f25a7035f Mon Sep 17 00:00:00 2001 From: Jan Klimke Date: Mon, 9 Apr 2018 10:16:52 +0200 Subject: [PATCH 1/3] added a connection reset if postgres database connection failing --- be-postgres.c | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/be-postgres.c b/be-postgres.c index 476b5c58..67e2feae 100644 --- a/be-postgres.c +++ b/be-postgres.c @@ -182,6 +182,11 @@ int be_pg_getuser(void *handle, const char *username, const char *password, char if (PQresultStatus(res) != PGRES_TUPLES_OK) { _log(LOG_DEBUG, "%s\n", PQresultErrorMessage(res)); + if(PQstatus(conf->conn) == CONNECTION_BAD){ + //try to reinitiate the database connection + PQreset(conf->conn); + } + goto out; } if ((nrows = PQntuples(res)) != 1) { @@ -194,7 +199,7 @@ int be_pg_getuser(void *handle, const char *username, const char *password, char } if ((v = PQgetvalue(res, 0, 0)) == NULL) { goto out; - } + } value = (v) ? strdup(v) : NULL; @@ -233,6 +238,12 @@ int be_pg_superuser(void *handle, const char *username) if (PQresultStatus(res) != PGRES_TUPLES_OK) { fprintf(stderr, "%s\n", PQresultErrorMessage(res)); issuper = BACKEND_ERROR; + //try to reset connection if failing because of database connection lost + if(PQstatus(conf->conn) == CONNECTION_BAD){ + //try to reinitiate the database connection + PQreset(conf->conn); + } + goto out; } if ((nrows = PQntuples(res)) != 1) { @@ -296,6 +307,13 @@ int be_pg_aclcheck(void *handle, const char *clientid, const char *username, con if (PQresultStatus(res) != PGRES_TUPLES_OK) { fprintf(stderr, "%s\n", PQresultErrorMessage(res)); match = BACKEND_ERROR; + + //try to reset connection if failing because of database connection lost + if(PQstatus(conf->conn) == CONNECTION_BAD){ + //try to reinitiate the database connection + PQreset(conf->conn); + } + goto out; } if (PQnfields(res) != 1) { @@ -413,8 +431,8 @@ int addKeyValue(char **keywords, char **values, char *key, char *value, n++; // In case of not zero-terminated dictionary - keywords[n] = '\0'; - values[n] = '\0'; + keywords[n] = 0; + values[n] = 0; return n; } From d311c1b39debcb424c62dfc5968a22c264bda629 Mon Sep 17 00:00:00 2001 From: Jan Klimke Date: Mon, 9 Apr 2018 10:21:39 +0200 Subject: [PATCH 2/3] added a log message when trying to reconnect to postgres database due to connection error --- be-postgres.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/be-postgres.c b/be-postgres.c index 67e2feae..724d0649 100644 --- a/be-postgres.c +++ b/be-postgres.c @@ -183,6 +183,7 @@ int be_pg_getuser(void *handle, const char *username, const char *password, char if (PQresultStatus(res) != PGRES_TUPLES_OK) { _log(LOG_DEBUG, "%s\n", PQresultErrorMessage(res)); if(PQstatus(conf->conn) == CONNECTION_BAD){ + _log(LOG_NOTICE, "Noticed a postgres connection loss. Trying to reconnect ...\n"); //try to reinitiate the database connection PQreset(conf->conn); } @@ -240,6 +241,7 @@ int be_pg_superuser(void *handle, const char *username) issuper = BACKEND_ERROR; //try to reset connection if failing because of database connection lost if(PQstatus(conf->conn) == CONNECTION_BAD){ + _log(LOG_NOTICE, "Noticed a postgres connection loss. Trying to reconnect ...\n"); //try to reinitiate the database connection PQreset(conf->conn); } @@ -310,6 +312,7 @@ int be_pg_aclcheck(void *handle, const char *clientid, const char *username, con //try to reset connection if failing because of database connection lost if(PQstatus(conf->conn) == CONNECTION_BAD){ + _log(LOG_NOTICE, "Noticed a postgres connection loss. Trying to reconnect ...\n"); //try to reinitiate the database connection PQreset(conf->conn); } From 4eedf76ee0d3710434d7c615c259d5cdb773c320 Mon Sep 17 00:00:00 2001 From: Jan Klimke Date: Mon, 9 Apr 2018 14:01:51 +0200 Subject: [PATCH 3/3] fixed interface definition for memcached --- be-memcached.c | 2 +- be-memcached.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/be-memcached.c b/be-memcached.c index 9ab76673..aadecaeb 100644 --- a/be-memcached.c +++ b/be-memcached.c @@ -139,7 +139,7 @@ void be_memcached_destroy(void *handle) } } -char *be_memcached_getuser(void *handle, const char *username, const char *password, int *authenticated) +int be_memcached_getuser(void *handle, const char *username, const char *password, int *authenticated) { struct memcached_backend *conf = (struct memcached_backend *)handle; diff --git a/be-memcached.h b/be-memcached.h index 0be0e1c8..b6b886aa 100644 --- a/be-memcached.h +++ b/be-memcached.h @@ -31,7 +31,7 @@ void *be_memcached_init(); void be_memcached_destroy(void *conf); -char *be_memcached_getuser(void *conf, const char *username, const char *password, int *authenticated); +int be_memcached_getuser(void *conf, const char *username, const char *password, int *authenticated); int be_memcached_superuser(void *conf, const char *username); int be_memcached_aclcheck(void *conf, const char *clientid, const char *username, const char *topic, int acc); #endif /* BE_MEMCACHED */