Nicer API for network timeouts

This commit is contained in:
Nyall Dawson 2019-01-30 05:59:37 +10:00
parent dcb6104f47
commit f1040efc03
5 changed files with 52 additions and 5 deletions

View File

@ -234,6 +234,24 @@ for the constructor of this class.
bool useSystemProxy() const;
%Docstring
Returns whether the system proxy should be used.
%End
static int timeout();
%Docstring
Returns the network timeout length, in milliseconds.
.. seealso:: :py:func:`setTimeout`
.. versionadded:: 3.6
%End
static void setTimeout( int time );
%Docstring
Sets the maximum timeout ``time`` for network requests, in milliseconds.
.. seealso:: :py:func:`timeout`
.. versionadded:: 3.6
%End
signals:

View File

@ -321,7 +321,7 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WindowFlags fl, const QList<QgsOpti
connect( mBtnMoveHelpDown, &QAbstractButton::clicked, this, &QgsOptions::moveHelpPathDown );
//Network timeout
mNetworkTimeoutSpinBox->setValue( mSettings->value( QStringLiteral( "/qgis/networkAndProxy/networkTimeout" ), 60000 ).toInt() );
mNetworkTimeoutSpinBox->setValue( QgsNetworkAccessManager::timeout() );
leUserAgent->setText( mSettings->value( QStringLiteral( "/qgis/networkAndProxy/userAgent" ), "Mozilla/5.0" ).toString() );
// WMS capabilities expiry time
@ -1357,7 +1357,7 @@ void QgsOptions::saveOptions()
mSettings->setValue( QStringLiteral( "help/helpSearchPath" ), helpPaths );
//Network timeout
mSettings->setValue( QStringLiteral( "/qgis/networkAndProxy/networkTimeout" ), mNetworkTimeoutSpinBox->value() );
QgsNetworkAccessManager::setTimeout( mNetworkTimeoutSpinBox->value() );
mSettings->setValue( QStringLiteral( "/qgis/networkAndProxy/userAgent" ), leUserAgent->text() );
// WMS capabiltiies expiry time

View File

@ -255,8 +255,7 @@ QNetworkReply *QgsNetworkAccessManager::createRequest( QNetworkAccessManager::Op
timer->setObjectName( QStringLiteral( "timeoutTimer" ) );
connect( timer, &QTimer::timeout, this, &QgsNetworkAccessManager::abortRequest );
timer->setSingleShot( true );
const int timeout = s.value( QStringLiteral( "/qgis/networkAndProxy/networkTimeout" ), 60000 ).toInt();
timer->start( timeout );
timer->start( timeout() );
connect( reply, &QNetworkReply::downloadProgress, timer, [timer] { timer->start(); } );
connect( reply, &QNetworkReply::uploadProgress, timer, [timer] { timer->start(); } );
@ -384,7 +383,7 @@ void QgsNetworkAccessManager::restartTimeout( QNetworkReply *reply )
Q_ASSERT( !timer->isActive() );
QgsDebugMsg( QStringLiteral( "Restarting network reply timeout" ) );
timer->setSingleShot( true );
timer->start( QgsSettings().value( QStringLiteral( "/qgis/networkAndProxy/networkTimeout" ), 60000 ).toInt() );
timer->start( timeout() );
}
}
@ -609,6 +608,16 @@ void QgsNetworkAccessManager::setupDefaultProxyAndCache( Qt::ConnectionType conn
setCache( newcache );
}
int QgsNetworkAccessManager::timeout()
{
return QgsSettings().value( QStringLiteral( "/qgis/networkAndProxy/networkTimeout" ), 60000 ).toInt();
}
void QgsNetworkAccessManager::setTimeout( const int time )
{
QgsSettings().setValue( QStringLiteral( "/qgis/networkAndProxy/networkTimeout" ), time );
}
//
// QgsNetworkRequestParameters

View File

@ -398,6 +398,22 @@ class CORE_EXPORT QgsNetworkAccessManager : public QNetworkAccessManager
*/
bool useSystemProxy() const { return mUseSystemProxy; }
/**
* Returns the network timeout length, in milliseconds.
*
* \see setTimeout()
* \since QGIS 3.6
*/
static int timeout();
/**
* Sets the maximum timeout \a time for network requests, in milliseconds.
*
* \see timeout()
* \since QGIS 3.6
*/
static void setTimeout( int time );
signals:
/**

View File

@ -634,6 +634,10 @@ void TestQgsNetworkAccessManager::fetchTimeout()
if ( QgsTest::isTravis() )
QSKIP( "This test is disabled on Travis CI environment" );
QgsNetworkAccessManager::setTimeout( 2000 );
QCOMPARE( QgsNetworkAccessManager::timeout(), 2000 );
QgsNetworkAccessManager::setTimeout( 1000 );
QObject context;
//test fetching from a blank url
bool gotRequestAboutToBeCreatedSignal = false;