Skip to content

Commit

Permalink
PG: Skip broken connections (for a while)
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso committed Oct 30, 2019
1 parent 6a7719f commit 576a917
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
32 changes: 30 additions & 2 deletions src/providers/postgres/qgspostgresconn.cpp
Expand Up @@ -144,6 +144,9 @@ Oid QgsPostgresResult::PQoidValue()

QMap<QString, QgsPostgresConn *> QgsPostgresConn::sConnectionsRO;
QMap<QString, QgsPostgresConn *> QgsPostgresConn::sConnectionsRW;
QSet<QString> QgsPostgresConn::sBrokenConnectionsCache;
QMutex QgsPostgresConn::sBrokenConnectionsCacheMutex;

const int QgsPostgresConn::GEOM_TYPE_SELECT_LIMIT = 100;

QgsPostgresConn *QgsPostgresConn::connectDb( const QString &conninfo, bool readonly, bool shared, bool transaction )
Expand Down Expand Up @@ -218,7 +221,20 @@ QgsPostgresConn::QgsPostgresConn( const QString &conninfo, bool readOnly, bool s
, mTransaction( transaction )
, mLock( QMutex::Recursive )
{
QgsDebugMsg( QStringLiteral( "New PostgreSQL connection for " ) + conninfo );

{
QMutexLocker locker( &sBrokenConnectionsCacheMutex );
if ( sBrokenConnectionsCache.contains( conninfo ) )
{
QgsDebugMsg( QStringLiteral( "Skipping broken PostgreSQL connection: " ) + conninfo );
mRef = 0;
return;
}
else
{
QgsDebugMsg( QStringLiteral( "New PostgreSQL connection for " ) + conninfo );
}
}

// expand connectionInfo
QgsDataSourceUri uri( conninfo );
Expand Down Expand Up @@ -286,7 +302,20 @@ QgsPostgresConn::QgsPostgresConn( const QString &conninfo, bool readOnly, bool s
++i;
bool ok = QgsCredentials::instance()->get( conninfo, username, password, PQerrorMessage() );
if ( !ok )
{
{
QMutexLocker locker( &sBrokenConnectionsCacheMutex );
// Insert the connInfo in the cache of broken/ignored connections
QgsPostgresConn::sBrokenConnectionsCache.insert( conninfo );
}
QTimer::singleShot( 5000, [ = ]()
{
QgsDebugMsgLevel( QStringLiteral( "Removing broken connection from cache: %1" ).arg( conninfo ), 4 );
QMutexLocker locker( &sBrokenConnectionsCacheMutex );
QgsPostgresConn::sBrokenConnectionsCache.remove( conninfo );
} );
break;
}

PQfinish();

Expand Down Expand Up @@ -360,7 +389,6 @@ QgsPostgresConn::QgsPostgresConn( const QString &conninfo, bool readOnly, bool s
PQexecNR( QStringLiteral( "SET application_name='QGIS'" ) );
}


PQsetNoticeProcessor( mConn, noticeProcessor, nullptr );
}

Expand Down
4 changes: 4 additions & 0 deletions src/providers/postgres/qgspostgresconn.h
Expand Up @@ -435,6 +435,10 @@ class QgsPostgresConn : public QObject
static QMap<QString, QgsPostgresConn *> sConnectionsRW;
static QMap<QString, QgsPostgresConn *> sConnectionsRO;

//! Temporary cache for broken connections, to avoid GUI freezing by multiple credentials requests
static QSet<QString> sBrokenConnectionsCache;
static QMutex sBrokenConnectionsCacheMutex;

//! Count number of spatial columns in a given relation
void addColumnInfo( QgsPostgresLayerProperty &layerProperty, const QString &schemaName, const QString &viewName, bool fetchPkCandidates );

Expand Down

0 comments on commit 576a917

Please sign in to comment.