16#ifndef QGSCONNECTIONPOOL_H
17#define QGSCONNECTIONPOOL_H
25#include <QCoreApplication>
33#include <QElapsedTimer>
35#define CONN_POOL_EXPIRATION_TIME 60
36#define CONN_POOL_SPARE_CONNECTIONS 2
84 for (
const Item &item : std::as_const(
conns ) )
86 qgsConnectionPool_ConnectionDestroy( item.c );
100 T
acquire(
int timeout,
bool requestMayBeNested )
102 const int requiredFreeConnectionCount = requestMayBeNested ? 1 : 3;
106 if ( !
sem.tryAcquire( requiredFreeConnectionCount, timeout ) )
115 sem.acquire( requiredFreeConnectionCount );
117 sem.release( requiredFreeConnectionCount - 1 );
123 if ( !
conns.isEmpty() )
126 if ( !qgsConnectionPool_ConnectionIsValid( i.
c ) )
128 qgsConnectionPool_ConnectionDestroy( i.
c );
129 qgsConnectionPool_ConnectionCreate(
connInfo, i.
c );
134 if (
conns.isEmpty() )
137 QMetaObject::invokeMethod(
expirationTimer->parent(),
"stopExpirationTimer" );
147 qgsConnectionPool_ConnectionCreate(
connInfo,
c );
165 if ( !qgsConnectionPool_ConnectionIsValid( conn ) )
167 qgsConnectionPool_ConnectionDestroy( conn );
179 QMetaObject::invokeMethod(
expirationTimer->parent(),
"startExpirationTimer" );
191 for (
const Item &i : std::as_const(
conns ) )
193 qgsConnectionPool_ConnectionDestroy( i.c );
197 qgsConnectionPool_InvalidateConnection(
c );
207 QObject::connect(
expirationTimer, SIGNAL( timeout() ), parent, SLOT( handleConnectionExpired() ) );
211 parent->moveToThread( qApp->thread() );
218 QTime now = QTime::currentTime();
222 for (
int i = 0; i <
conns.count(); ++i )
225 toDelete.append( i );
229 for (
int j = toDelete.count() - 1; j >= 0; --j )
231 int index = toDelete[j];
232 qgsConnectionPool_ConnectionDestroy(
conns[index].
c );
233 conns.remove( index );
236 if (
conns.isEmpty() )
271template <
typename T,
typename T_Group>
281 for ( T_Group *group : std::as_const(
mGroups ) )
302 typename T_Groups::iterator it =
mGroups.find( connInfo );
305 it =
mGroups.insert( connInfo,
new T_Group( connInfo ) );
307 T_Group *group = *it;
315 while ( !feedback->isCanceled() )
317 if ( T conn = group->acquire( 300, requestMayBeNested ) )
320 if ( timeout > 0 && timer.elapsed() >= timeout )
327 return group->acquire( timeout, requestMayBeNested );
335 typename T_Groups::iterator it =
mGroups.find( qgsConnectionPool_ConnectionToName( conn ) );
336 Q_ASSERT( it !=
mGroups.end() );
337 T_Group *group = *it;
340 group->release( conn );
353 if (
mGroups.contains( connInfo ) )
354 mGroups[connInfo]->invalidateConnections();
Extends QApplication to provide access to QGIS specific resources such as theme paths,...
Template that stores data related to a connection to a single server or datasource.
void invalidateConnections()
QgsConnectionPoolGroup(const QgsConnectionPoolGroup &other)=delete
T acquire(int timeout, bool requestMayBeNested)
Try to acquire a connection for a maximum of timeout milliseconds.
QgsConnectionPoolGroup(const QString &ci)
Constructor for QgsConnectionPoolGroup, with the specified connection info.
QgsConnectionPoolGroup & operator=(const QgsConnectionPoolGroup &other)=delete
void initTimer(QObject *parent)
~QgsConnectionPoolGroup()
void onConnectionExpired()
Template class responsible for keeping a pool of open connections.
QMap< QString, T_Group * > T_Groups
void invalidateConnections(const QString &connInfo)
Invalidates all connections to the specified resource.
virtual ~QgsConnectionPool()
T acquireConnection(const QString &connInfo, int timeout=-1, bool requestMayBeNested=false, QgsFeedback *feedback=nullptr)
Try to acquire a connection for a maximum of timeout milliseconds.
void releaseConnection(T conn)
Release an existing connection so it will get back into the pool and can be reused.
Base class for feedback objects to be used for cancellation of something running in a worker thread.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
#define CONN_POOL_SPARE_CONNECTIONS
#define CONN_POOL_EXPIRATION_TIME