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
81 for (
const Item &item : std::as_const(
conns ) )
83 qgsConnectionPool_ConnectionDestroy( item.c );
99 T
acquire(
int timeout,
bool requestMayBeNested )
101 const int requiredFreeConnectionCount = requestMayBeNested ? 1 : 3;
105 if ( !
sem.tryAcquire( requiredFreeConnectionCount, timeout ) )
114 sem.acquire( requiredFreeConnectionCount );
116 sem.release( requiredFreeConnectionCount - 1 );
122 if ( !
conns.isEmpty() )
125 if ( !qgsConnectionPool_ConnectionIsValid( i.
c ) )
127 qgsConnectionPool_ConnectionDestroy( i.
c );
128 qgsConnectionPool_ConnectionCreate(
connInfo, i.
c );
133 if (
conns.isEmpty() )
136 QMetaObject::invokeMethod(
expirationTimer->parent(),
"stopExpirationTimer" );
146 qgsConnectionPool_ConnectionCreate(
connInfo,
c );
164 if ( !qgsConnectionPool_ConnectionIsValid( conn ) )
166 qgsConnectionPool_ConnectionDestroy( conn );
178 QMetaObject::invokeMethod(
expirationTimer->parent(),
"startExpirationTimer" );
190 for (
const Item &i : std::as_const(
conns ) )
192 qgsConnectionPool_ConnectionDestroy( i.c );
196 qgsConnectionPool_InvalidateConnection(
c );
206 QObject::connect(
expirationTimer, SIGNAL( timeout() ), parent, SLOT( handleConnectionExpired() ) );
210 parent->moveToThread( qApp->thread() );
217 QTime now = QTime::currentTime();
221 for (
int i = 0; i <
conns.count(); ++i )
224 toDelete.append( i );
228 for (
int j = toDelete.count() - 1; j >= 0; --j )
230 int index = toDelete[j];
231 qgsConnectionPool_ConnectionDestroy(
conns[index].
c );
232 conns.remove( index );
235 if (
conns.isEmpty() )
270 template <
typename T,
typename T_Group>
280 for ( T_Group *group : std::as_const(
mGroups ) )
301 typename T_Groups::iterator it =
mGroups.find( connInfo );
304 it =
mGroups.insert( connInfo,
new T_Group( connInfo ) );
306 T_Group *group = *it;
314 while ( !feedback->isCanceled() )
316 if ( T conn = group->acquire( 300, requestMayBeNested ) )
319 if ( timeout > 0 && timer.elapsed() >= timeout )
326 return group->acquire( timeout, requestMayBeNested );
334 typename T_Groups::iterator it =
mGroups.find( qgsConnectionPool_ConnectionToName( conn ) );
335 Q_ASSERT( it !=
mGroups.end() );
336 T_Group *group = *it;
339 group->release( conn );
352 if (
mGroups.contains( connInfo ) )
353 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
QgsConnectionPoolGroup cannot be copied.
T acquire(int timeout, bool requestMayBeNested)
Try to acquire a connection for a maximum of timeout milliseconds.
QgsConnectionPoolGroup(const QString &ci)
QgsConnectionPoolGroup & operator=(const QgsConnectionPoolGroup &other)=delete
QgsConnectionPoolGroup cannot be copied.
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