Skip to content

Commit

Permalink
Add mechanism for temporarily disabling all network caching
Browse files Browse the repository at this point in the history
to QgsNetworkAccessManager, and add a checkbox in the network
logger panel to allow users to temporarily disable the network cache

This can be VERY useful when debugging QGIS network activity, or when
using QGIS to test server side changes.

This is a transient setting only, and is forgotten as soon as QGIS
is closed. That's by design -- we don't want users to accidentally
leave this enabled and cause unnecessary server load.
  • Loading branch information
nyalldawson committed Jan 19, 2021
1 parent 0b2c951 commit 36f68e4
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
Expand Up @@ -250,6 +250,7 @@ need to be handled on the main thread. See in-depth discussion in the documentat
for the constructor of this class.
%End


bool useSystemProxy() const;
%Docstring
Returns whether the system proxy should be used.
Expand Down
13 changes: 13 additions & 0 deletions src/app/devtools/networklogger/qgsnetworkloggerpanelwidget.cpp
Expand Up @@ -27,6 +27,7 @@
#include <QMessageBox>
#include <QScrollBar>
#include <QToolButton>
#include <QCheckBox>

#include <nlohmann/json.hpp>

Expand Down Expand Up @@ -217,4 +218,16 @@ QgsNetworkLoggerPanelWidget::QgsNetworkLoggerPanelWidget( QgsNetworkLogger *logg

settingsMenu->addAction( mActionShowSuccessful );
settingsMenu->addAction( mActionShowTimeouts );

mToolbar->addSeparator();
QCheckBox *disableCacheCheck = new QCheckBox( tr( "Disable cache" ) );
connect( disableCacheCheck, &QCheckBox::toggled, this, [ = ]( bool checked )
{
// note -- we deliberately do NOT store this as a permanent setting in QSettings
// as it is designed to be a temporary debugging tool only and we don't want
// users to accidentally leave this enabled and cause unnecessary server load...
QgsNetworkAccessManager::instance()->setCacheDisabled( checked );
} );

mToolbar->addWidget( disableCacheCheck );
}
10 changes: 10 additions & 0 deletions src/core/network/qgsnetworkaccessmanager.cpp
Expand Up @@ -127,7 +127,10 @@ QgsNetworkAccessManager *QgsNetworkAccessManager::instance( Qt::ConnectionType c
sMainNAM = nam;

if ( !nam->mInitialized )
{
nam->setupDefaultProxyAndCache( connectionType );
nam->setCacheDisabled( sMainNAM->cacheDisabled() );
}

return nam;
}
Expand Down Expand Up @@ -249,6 +252,13 @@ QNetworkReply *QgsNetworkAccessManager::createRequest( QNetworkAccessManager::Op
}
#endif

if ( sMainNAM->mCacheDisabled )
{
// if caching is disabled then we override whatever the request actually has set!
pReq->setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::AlwaysNetwork );
pReq->setAttribute( QNetworkRequest::CacheSaveControlAttribute, false );
}

static QAtomicInt sRequestId = 0;
const int requestId = ++sRequestId;
QByteArray content;
Expand Down
25 changes: 25 additions & 0 deletions src/core/network/qgsnetworkaccessmanager.h
Expand Up @@ -409,6 +409,30 @@ class CORE_EXPORT QgsNetworkAccessManager : public QNetworkAccessManager
*/
void setupDefaultProxyAndCache( Qt::ConnectionType connectionType = Qt::BlockingQueuedConnection );

#ifndef SIP_RUN

/**
* Returns TRUE if all network caching is disabled.
*
* \see setCacheDisabled()
* \note Not available in Python bindings.
* \since QGIS 3.18
*/
bool cacheDisabled() const { return mCacheDisabled; }

/**
* Sets whether all network caching should be disabled.
*
* If \a disabled is TRUE then all caching will be disabled, causing all requests
* to be retrieved from the network regardless of the request's attributes.
*
* \see cacheDisabled()
* \note Not available in Python bindings.
* \since QGIS 3.18
*/
void setCacheDisabled( bool disabled ) { mCacheDisabled = disabled; }
#endif

/**
* Returns whether the system proxy should be used.
*/
Expand Down Expand Up @@ -655,6 +679,7 @@ class CORE_EXPORT QgsNetworkAccessManager : public QNetworkAccessManager
QStringList mNoProxyURLs;
bool mUseSystemProxy = false;
bool mInitialized = false;
bool mCacheDisabled = false;
static QgsNetworkAccessManager *sMainNAM;
// ssl error handler, will be set for main thread ONLY
std::unique_ptr< QgsSslErrorHandler > mSslErrorHandler;
Expand Down

0 comments on commit 36f68e4

Please sign in to comment.