Skip to content

Commit

Permalink
issue FIWARE#1412 re-connecting to postgres when connection is lost
Browse files Browse the repository at this point in the history
  • Loading branch information
cfreyfh committed Aug 24, 2023
1 parent 73b966d commit a3347de
Showing 1 changed file with 70 additions and 20 deletions.
90 changes: 70 additions & 20 deletions src/lib/orionld/troe/pgConnectionGet.cpp
Expand Up @@ -92,22 +92,44 @@ PgConnection* pgConnectionGet(const char* db)
// Search for a free but already connected PgConnection in the pool
for (int ix = 0; ix < poolP->items; ix++)
{
PgConnection* cP = poolP->connectionV[ix];
PgConnection* cP = poolP->connectionV[ix];

if ((cP == NULL) || (cP->busy == true))
continue;
if ((cP == NULL) || (cP->busy == true))
continue;

if (cP->connectionP != NULL)
{
// Great - found a free and already connected item - let's use it !
cP->busy = true;

sem_post(&poolP->poolSem);
sem_post(&poolP->queueSem);

cP->uses += 1;
return cP;
}
if (cP->connectionP != NULL)
{
// check if we are still connected
ConnStatusType pgStatus = PQstatus(cP->connectionP);
if (pgStatus != CONNECTION_OK)
{
LM_W(("Connection of item %d is lost, trying to re-connect...", ix));
// try to re-connect
PQreset(cP->connectionP);
// get status again
pgStatus = PQstatus(cP->connectionP);

// if still no connection
if (pgStatus != CONNECTION_OK)
{
// we free this pointer that it can be used in the next call of pgConnectionGet
free(poolP->connectionV[ix]);
poolP->connectionV[ix] = NULL;
LM_W(("Connection failed, pointer of item %d was re-set to NULL (%p)", ix, poolP->connectionV[ix]));
// this time no success finding a connection that is working, try in the next loop
continue;
}
}

// Great - found a free and already connected item - let's use it !
cP->busy = true;

sem_post(&poolP->poolSem);
sem_post(&poolP->queueSem);

cP->uses += 1;
return cP;
}
}

//
Expand Down Expand Up @@ -160,12 +182,40 @@ PgConnection* pgConnectionGet(const char* db)

if (cP->connectionP == NULL) // Virgin connection
{
cP->connectionP = pgConnect(_db);
if (cP->connectionP == NULL)
{
cP->busy = false; // So the slot can be used again!
LM_RE(NULL, ("Database Error (unable to connect to postgres(%s))", _db));
}
cP->connectionP = pgConnect(_db);
if (cP->connectionP == NULL)
{
char* errMsg = PQerrorMessage(cP->connectionP);
cP->busy = false; // So the slot can be used again!
LM_RE(NULL, ("Database Error (unable to connect to postgres(%s)): %s", _db, errMsg));
}
else
{
// check if we are connected
ConnStatusType pgStatus = PQstatus(cP->connectionP);
if (pgStatus != CONNECTION_OK)
{
sem_wait(&poolP->poolSem);
sem_wait(&poolP->queueSem);

// find the connection pointer in the pool and free it
for (int ix = 0; ix < poolP->items; ix++)
{
if (poolP->connectionV[ix] == cP)
{
free(poolP->connectionV[ix]);
poolP->connectionV[ix] = NULL;
break;
}
}
sem_post(&poolP->poolSem);
sem_post(&poolP->queueSem);

// get PG error message for log file
char* errMsg = PQerrorMessage(cP->connectionP);
LM_RE(NULL, ("Database Connection could not be established (%s): %s ", _db, errMsg));
}
}
}

cP->uses += 1;
Expand Down

0 comments on commit a3347de

Please sign in to comment.