Skip to content

Commit

Permalink
Add connect timeout for Postgres NOTIFY Listener
Browse files Browse the repository at this point in the history
  • Loading branch information
MorriganR authored and nyalldawson committed May 5, 2021
1 parent c4346f9 commit dcf2049
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/providers/postgres/qgspostgreslistener.cpp
Expand Up @@ -26,6 +26,8 @@
#include <sys/select.h>
#endif

const int PG_CONNECT_TIMEOUT = 30;

extern "C"
{
#include <libpq-fe.h>
Expand Down Expand Up @@ -59,12 +61,14 @@ QgsPostgresListener::~QgsPostgresListener()
void QgsPostgresListener::run()
{
PGconn *conn = nullptr;
QString connectString = mConnString;

conn = PQconnectdb( mConnString.toUtf8() );
connectString += QStringLiteral( " connect_timeout=%1" ).arg( PG_CONNECT_TIMEOUT );
conn = PQconnectdb( connectString.toUtf8() );

if ( PQstatus( conn ) != CONNECTION_OK )
{
QgsDataSourceUri uri( mConnString );
QgsDataSourceUri uri( connectString );
QString username = uri.username();
QString password = uri.password();

Expand All @@ -74,13 +78,11 @@ void QgsPostgresListener::run()

if ( QgsCredentials::instance()->get( mConnString, username, password, PQerrorMessage( conn ) ) )
{
if ( !username.isEmpty() )
uri.setUsername( username );

if ( !password.isEmpty() )
uri.setPassword( password );
uri.setUsername( username );
uri.setPassword( password );
connectString = uri.connectionInfo( false );
connectString += QStringLiteral( " connect_timeout=%1" ).arg( PG_CONNECT_TIMEOUT );

QString connectString = uri.connectionInfo( false );
conn = PQconnectdb( connectString.toUtf8() );
if ( PQstatus( conn ) == CONNECTION_OK )
QgsCredentials::instance()->put( mConnString, username, password );
Expand All @@ -90,6 +92,7 @@ void QgsPostgresListener::run()

if ( PQstatus( conn ) != CONNECTION_OK )
{
PQfinish( conn );
QgsDebugMsg( QStringLiteral( "LISTENer not started" ) );
return;
}
Expand Down

0 comments on commit dcf2049

Please sign in to comment.